String doesn't contain character even after checking with if-statement - c#

I am trying to read sensor data into Unity. For that I am using a TCP Server on a ESP32 by sending the data in json.
I am now trying to parse the received data into a serializable object.
Currently I am reading data from the server until I receive the final "}" bracket as a very rudimentary check for valid json as a starting point.
Now this is where I can't find my error. I am starting a Thread in the class which runs in the background and is constantly reading the server for new values.
But somehow I can't successfully concatenate the string which should be checked for the "}" character.
My code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net.Sockets;
using System.Threading;
[Serializable]
public class SensorData
{
public int capacity;
}
public class MugSensorRead : MonoBehaviour
{
TcpClient client = new TcpClient();
const string IP = "192.168.137.50";
const int PORT = 5000;
public SensorData sensorData;
public int capacity;
bool _threadRunning;
Thread _thread;
// Use this for initialization
void Start ()
{
client.Connect (IP, PORT);
_thread = new Thread (updateSensorData);
_thread.Start ();
}
// Update is called once per frame
void Update ()
{
capacity = sensorData.capacity;
}
void updateSensorData()
{
_threadRunning = true;
while (_threadRunning)
{
NetworkStream stream = client.GetStream ();
string jsonMsg = "";
bool validJson = false;
while (!validJson)
{
byte[] inStream = new byte[1024];
stream.Read (inStream, 0, 1024);
string jsonData = System.Text.Encoding.ASCII.GetString (inStream);
jsonMsg = string.Concat (jsonMsg, jsonData);
if (jsonMsg.Contains("}"))
{
validJson = true;
//This part here is executed, but when I print(jsonMsg), it just prints the character "{" which gets transmitted in the first segment
}
}
sensorData = JsonUtility.FromJson<SensorData> (jsonMsg);
}
_threadRunning = false;
}
void OnDisable()
{
if (_threadRunning)
{
_threadRunning = false;
_thread.Join ();
}
}
}
Can you spot my error? I'm just not able to see, where my code fails.

Your error is that you are not checking how many bytes the read returned and you are adding 0's as part of the string content.
This should be replaced:
while (!validJson)
{
byte[] inStream = new byte[1024];
stream.Read (inStream, 0, 1024);
string jsonData = System.Text.Encoding.ASCII.GetString (inStream);
jsonMsg = string.Concat (jsonMsg, jsonData);
if (jsonMsg.Contains("}"))
{
validJson = true;
//This part here is executed, but when I print(jsonMsg), it just prints the character "{" which gets transmitted in the first segment
}
}
This would be the correct code:
while (!validJson)
{
byte[] inStream = new byte[1024];
int bytesRead = stream.Read (inStream, 0, 1024);
string jsonData = System.Text.Encoding.ASCII.GetString (inStream, 0, bytesRead);
jsonMsg = string.Concat (jsonMsg, jsonData);
if (jsonMsg.Contains("}"))
{
validJson = true;
//This part here is executed, but when I print(jsonMsg), it just prints the character "{" which gets transmitted in the first segment
}
}
0's in the array get converted to string end bytes that's why you don't see the '}' char, because there are string end characters before.

Related

NetworkStream.Write data seems not to arrive at receiving socket

C# System.Net,Sockets
I'm having a problem where I cannot figure out what I might be doing wrong.
The story is, I send byte[] application data from TcpClient socket to another TcpClient socket. It all works great, until I add a custom pseudo Athentication test before the application data is sent, after which the send and receive which originally worked, fails.
I say fails, but what actually occurs is that it only appears to get 3 bytes all set to 0 at the read socket.
The Authenticate method does the following.
The server end sends 1 byte of data (0-85), the client receives it, treats it as an int, multiplies it by 3 and sends a byte back to server. Server checks the value, and sends another byte back set to 1.
All of that seems to work fine, but together the data sent by cliend after authentication does not appear to be received, just 3 bytes set to 0.
I want the socket to remain open over the lifetime of the program, so I cannot dispose the stream, as that will also dispose the socket.
Here is the full code for both client and server, in the hope someone might see my mistake, or an issue I have missed.
The code is intentionally without error checking for brevity, and is very basic to just show the problem.
Note, that if both Authenticate methods simply return true, then the code works exactly how I'd expect it.
Server.
class Program
{
static Random rnd = new Random(Guid.NewGuid().GetHashCode());
static void Main(string[] args)
{
Process p = Process.Start(#"C:\Users\Teddy\Documents\visual studio 2015\code\Readissue\TheClient\bin\Debug\TheClient.exe");
Console.Title = "Server";
TcpListener lis = new TcpListener(
new IPEndPoint(
IPAddress.Any, 4000
));
lis.Start();
TcpClient cli = lis.AcceptTcpClient();
NetworkStream ns = cli.GetStream();
if (Authenticate(cli, ns))
{
Console.WriteLine("Good!");
// This condition is met
}
else
{
Console.WriteLine("Bad!");
Console.ReadLine();
return;
}
// Wait until Carrier class of client
// Sends data
while (!ns.DataAvailable)
{
Thread.Sleep(100);
}
Console.WriteLine("DataAvailable");
byte[] buffer = new byte[2048];
//bytesread is always the value of 3.
int bytesread = ns.Read(buffer, 0, buffer.Length);
string sdata = Encoding.ASCII.GetString(buffer).Substring(0, bytesread);
Console.WriteLine(sdata);
Console.ReadLine();
p.Kill();
p.Close();
}
private static bool Authenticate(TcpClient cli, NetworkStream ns)
{
//return true;
byte[] rcv = new byte[1];
int isnd = rnd.Next(0, 85);
byte[] snd = new byte[1] { (byte)isnd };
//Sends a random number
//and waits for response
ns.Write(snd, 0, snd.Length);
while (!ns.DataAvailable)
{
Thread.Sleep(10);
}
// Expects response to be
// random number x 3
int br = ns.Read(rcv, 0, rcv.Length);
int ircv = rcv[0];
int iok;
if (ircv == (isnd * 3))
{
// Confirm random number x 3
iok = 1;
byte[] bok = new byte[1] { (byte)iok };
ns.Write(bok, 0, snd.Length);
return true;
}
else
{
iok = 0;
byte[] bok = new byte[1] { (byte)iok };
ns.Write(bok, 0, snd.Length);
return false;
}
}
class Carrier
{
public double PointX { get; set; }
public double PointY { get; set; }
public string Comment { get; set; }
public Carrier(byte[] bytes)
{
string[] tmpStrings = Encoding.ASCII.GetString(bytes)
.Split('|');
PointX = Convert.ToDouble(tmpStrings[0]);
PointY = Convert.ToDouble(tmpStrings[1]);
Comment = tmpStrings[2];
}
}
}
Client
class Program
{
static void Main(string[] args)
{
Console.Title = "Client";
IPEndPoint EP = new IPEndPoint(
IPAddress.Parse("192.168.1.100"), 4000
);
TcpClient cli = new TcpClient();
cli.Connect(EP);
if (!cli.Connected)
{
Console.WriteLine("Not connected!");
return;
}
Console.WriteLine("Connected!");
NetworkStream ns = cli.GetStream();
if (Authenticate(cli, ns))
{
Console.WriteLine("Good!");
// This condition is met
}
else
{
Console.WriteLine("Bad!");
return;
}
// Send data to server
Carrier carrier = new Carrier();
string stringtosend = carrier.ToString();
byte[] bytestosend = Encoding.ASCII.GetBytes(stringtosend);
ns.Write(bytestosend, 0, bytestosend.Length);
Console.WriteLine("Data sent!");
Console.ReadLine();
}
private static void UseClient(TcpClient cli, NetworkStream ns)
{
Console.WriteLine(ns.CanRead);
}
private static bool Authenticate(TcpClient client, NetworkStream ns)
{
//return true;
byte[] rcv = new byte[1];
while (!ns.DataAvailable)
{
Thread.Sleep(10);
}
int br = ns.Read(rcv, 0, rcv.Length);
int ircv = rcv[0];
int result = ircv * 3;
byte[] snd = BitConverter.GetBytes(result);
ns.Write(snd, 0, snd.Length);
while (!ns.DataAvailable)
{
Thread.Sleep(10);
}
br = ns.Read(rcv, 0, rcv.Length);
int iok = rcv[0];
if (iok == 1)
{
return true;
}
return false;
}
}
class Carrier
{
public double PointX { get; set; }
public double PointY { get; set; }
public string Comment { get; set; }
public Carrier()
{
PointX = 1.00;
PointY = 2.00;
Comment = "A longer comment string";
}
public override string ToString()
{
return PointX.ToString() + "|"
+ PointY.ToString() + "|"
+ Comment;
}
}
So as I suspected, the problem was in the Authenticate method on the client side.
I was sending an int instead of a single byte. The offending line of code was.
byte[] snd = BitConverter.GetBytes(result);
Which should have been.
byte[] snd = new byte[1] { (byte)result };
Thanks jdweng for finding the error.
PS, thanks to the down-voters for your interest, please accept my sincere pity.

Unity3D WWW Error C#

I am working in Unity trying to figure out the WWW class and access API from online-go.com
I get an error in the Debug.Log though. Additionally, the Debug on Line 58 just returns a blank string. I don't think I am fully understanding how to use WWW since this is the first time I am using it.
Necessary data rewind wasn't possible
UnityEngine.Debug:Log(Object)
<LoadWWW>c__Iterator0:MoveNext() (at Assets/OGS.cs:60)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Net;
using System.Text;
//using System.Net.httpclient;
public class OGS : MonoBehaviour {
string generateAPIClient = "http://beta.online-go.com/developer";
string APIKey = "0c63a59dd17ec69a48af5d9dc8b4e956";
string requestUserToken = "oauth2/access_token";
string clientID = "";
string clientSecret = "";
string baseURL = "http://online-go.com/";
string url = "";
string username;
string password;
string POST;
List<Settings> settings;
// Use this for initialization
void Start () {
Debug.Log("Opened");
settings = new List<Settings>();
Load("Settings");
clientID = AssignSetting("clientID");
clientSecret = AssignSetting("clientSecret");
username = AssignSetting("username");
password = AssignSetting("password");
POST = string.Format( "client_id={0}&client_secret={1}&grant_type=password&username={2}&password={3}",
clientID, clientSecret, username, password);
url = baseURL + requestUserToken;
StartCoroutine("LoadWWW");
}
//Assign settings loaded to settings variables
string AssignSetting (string item) {
int position = -1;
for(int i=0;i<settings.Count;i++) {
if(settings[i].name == item){return settings[i].value;}
}
return string.Empty;
}
IEnumerator LoadWWW() {
byte[] byteArray = GetBytes(POST);
Dictionary<string,string> headers = new Dictionary<string,string>();
headers.Add("Content-Type", "application/x-www-form-urlencoded");
WWW text = new WWW(url, byteArray, headers);
yield return text;
byteArray = text.bytes;
string POSTResponse = GetString(byteArray);
Debug.Log(POSTResponse);
Debug.Log(text.responseHeaders);
Debug.Log(text.error);
}
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
private bool Load(string fileName)
{
// Handle any problems that might arise when reading the text
try
{
string line;
// Create a new StreamReader, tell it which file to read and what encoding the file
// was saved as
StreamReader theReader = new StreamReader(Application.dataPath + "/Resources/" + fileName + ".txt");
// Immediately clean up the reader after this block of code is done.
// You generally use the "using" statement for potentially memory-intensive objects
// instead of relying on garbage collection.
// (Do not confuse this with the using directive for namespace at the
// beginning of a class!)
using (theReader)
{
// While there's lines left in the text file, do this:
do
{
line = theReader.ReadLine();
if (line != null)
{
// Do whatever you need to do with the text line, it's a string now
// In this example, I split it into arguments based on comma
// deliniators, then send that array to DoStuff()
string[] entries = line.Split(':');
if (entries.Length > 0){
Settings newSetting = new Settings(entries[0], entries[1]);
settings.Add(newSetting);
}
}
}
while (line != null);
// Done reading, close the reader and return true to broadcast success
theReader.Close();
return true;
}
}
// If anything broke in the try block, we throw an exception with information
// on what didn't work
catch (Exception e)
{
Console.WriteLine("{0}\n", e.Message);
return false;
}
}
}
necessary data rewind wasn't possible mainly occurs when redirection is involved during the WWW call.
To fix this, make sure that the URL's you call are not redirecting you to another page in the process. Also it would be a good idea to have some error handling before you use the value.
// wait for the result
yield return text;
// Handle the error if there is any
if (!string.IsNullOrEmpty(text.error)) {
Debug.Log(text.error);
}
// Now do with POSTResponse whatever you want if there were no errors.

NetworkStream cuts off first 4 bytes when reading

I ran into a strange problem. When I'm trying to send the file via a TCP socket, the first 4 bytes of sended information cuts off.
That is sending and receives pieces of code.
Client side
for (var i = 0; i < fileContentByte.Length; i += buffer.Length)
{
var size = (i + buffer.Length > fileContentByte.Length) ? fileContentByte.Length - i : buffer.Length;
clientSocket.Write(fileContentByte, i, size);
}
Server side
using(var file = File.Create("C:\\test\\"+fileName.Substring(0, fileName.IndexOf('\0'))))
while(bytesReceived < numberOfBytes && (count = clientStream.Read(buffer, 0, buffer.Length)) > 0)
{
file.Write(buffer, 0, count);
bytesReceived += count;
}
Here is link on full code - http://pastebin.com/VwTgTxgb
You're doing something very strange here.
First of all, retrieval of file name can be greatly simplified down to Path.GetFileName() call.
Second, are you sure ASCII will suffice?
Third, reading the entire file into memory is OK-ish for a proof-of-concept project, but be ready to switch to streaming operations.
Fourth, your protocol is somewhat wonky. When sending variable-size payload, it is required to first tell the receiving party exactly how much bytes are you going to send. This is exactly what you don't do when sending file name.
Here's a snippet to get you started:
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace FolderSync
{
class Program
{
static void Main()
{
var server = new Server();
server.Start();
new Client().TransmitFile(
new IPEndPoint(IPAddress.Loopback, 35434),
#"f:\downloads\ubuntu-14.04.3-desktop-amd64.iso");
Console.ReadLine();
server.Stop();
}
}
class Server
{
private readonly TcpListener tcpListener;
public Server()
{
tcpListener = new TcpListener(IPAddress.Loopback, 35434);
}
public void Start()
{
tcpListener.Start();
tcpListener.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
}
public void Stop()
{
tcpListener.Stop();
}
private void AcceptTcpClientCallback(IAsyncResult asyncResult)
{
//
// Big fat warning: http://stackoverflow.com/a/1230266/60188
tcpListener.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
using(var tcpClient = tcpListener.EndAcceptTcpClient(asyncResult))
using(var networkStream = tcpClient.GetStream())
using(var binaryReader = new BinaryReader(networkStream, Encoding.UTF8))
{
var fileName = binaryReader.ReadString();
var length = binaryReader.ReadInt64();
var mib = length / 1024.0 / 1024.0;
Console.WriteLine("Receiving '{0}' ({1:N1} MiB)", fileName, mib);
var stopwatch = Stopwatch.StartNew();
var fullFilePath = Path.Combine(Path.GetTempPath(), fileName);
using(var fileStream = File.Create(fullFilePath))
networkStream.CopyTo(fileStream);
var elapsed = stopwatch.Elapsed;
Console.WriteLine("Received in {0} ({1:N1} MiB/sec)",
elapsed, mib / elapsed.TotalSeconds);
}
}
}
class Client
{
public void TransmitFile(IPEndPoint endPoint, string fileFullPath)
{
if(!File.Exists(fileFullPath)) return;
using(var tcpClient = new TcpClient())
{
tcpClient.Connect(endPoint);
using(var networkStream = tcpClient.GetStream())
using(var binaryWriter = new BinaryWriter(networkStream, Encoding.UTF8))
{
var fileName = Path.GetFileName(fileFullPath);
Debug.Assert(fileName != null, "fileName != null");
//
// BinaryWriter.Write(string) does length-prefixing automatically
binaryWriter.Write(fileName);
using(var fileStream = File.OpenRead(fileFullPath))
{
binaryWriter.Write(fileStream.Length);
fileStream.CopyTo(networkStream);
}
}
}
}
}
}

Named pipes async

Im trying to set up a named pipe server and client to send data between two programs.
My issue is that when i data is recived eg. BeginRead command om server triggers after i have serialized an object from the client it triggers the callback like 20 times for the same message. The goal is that the client program will send commands to the server program. And when the server processes tasks it will send status updates back to the client when there is one connected.
Here is my current test program.
class Program
{
static void Main(string[] args)
{
var server = new PipeServer();
server.Init();
var client = new PipeClient();
if (client.Connect())
{
Console.WriteLine("Connected to server.");
}
else
{
Console.WriteLine("Connection failed.");
return;
}
while (true)
{
Console.Write(" \\> ");
string input = Console.ReadLine();
if (string.IsNullOrEmpty(input)) break;
var arr = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int value = 0;
if (arr.Length != 2) break;
if (!int.TryParse(arr[1], out value)) break;
var obj = new PipeObject { Name = arr[0], Value = value };
client.Send(obj);
//string result = f.Deserialize(client) as string;
//Console.WriteLine(result);
}
}
}
internal class PipeServer
{
IFormatter Formatter = new BinaryFormatter();
public NamedPipeServerStream Instance { get; internal set; }
public bool IsConnected { get; internal set; }
byte[] buffer = new byte[65535];
public object Message { get; set; }
StreamReader sr;
StreamWriter sw;
internal PipeServer()
{
IsConnected = false;
}
public void Init()
{
var ps = new PipeSecurity();
ps.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().User, PipeAccessRights.FullControl, AccessControlType.Allow));
ps.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
Instance = new NamedPipeServerStream("Levscan4Pipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous | PipeOptions.WriteThrough, 65535, 65535, ps);
sr = new StreamReader(Instance);
sw = new StreamWriter(Instance);
Instance.BeginWaitForConnection(OnClientConnected, Instance);
Thread t = new Thread(Run);
t.Start();
}
void Run()
{
int index = 0;
if (IsConnected)
{
try
{
Instance.BeginRead(buffer, 0, buffer.Length, OnRead_Completed, Instance);
//index += Instance.Read(buffer, 0, buffer.Length);
//try
//{
// using (var ms = new MemoryStream(buffer))
// {
// Message = Formatter.Deserialize(ms);
// index = 0;
// }
//}
//catch (Exception e)
//{
// Debug.WriteLine(e.Message);
// Debug.WriteLine(e.StackTrace);
//}
}
catch (IOException)
{
IsConnected = false;
Instance.Disconnect();
}
}
Thread.Sleep(Timeout.Infinite);
//Instance.WaitForConnection();
//Thread t = new Thread(Run);
//t.Start();
}
void OnClientConnected(IAsyncResult ar)
{
Instance.EndWaitForConnection(ar);
IsConnected = true;
}
void OnRead_Completed(IAsyncResult ar)
{
var bytes = Instance.EndRead(ar);
Debug.WriteLine("{1} > Read completed - bytes read: {0}".FormatWith(bytes, DateTime.Now.ToString()));
//try
//{
// using (var ms = new MemoryStream(buffer))
// {
// Message = Formatter.Deserialize(ms);
// }
//}
//catch (Exception e)
//{
// Debug.WriteLine(e.Message);
// Debug.WriteLine(e.StackTrace);
//}
}
}
internal class PipeClient
{
IFormatter f = new BinaryFormatter();
public NamedPipeClientStream Instance { get; internal set; }
StreamWriter sw;
StreamReader sr;
public PipeClient()
{
Instance = new NamedPipeClientStream(".", "Levscan4Pipe", PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough);
sr = new StreamReader(Instance);
sw = new StreamWriter(Instance);
}
public bool Connect()
{
try
{
Instance.Connect(5000);
Instance.ReadMode = PipeTransmissionMode.Message;
Instance.WaitForPipeDrain();
return true;
}
catch
{
return false;
}
}
public void Send(object obj)
{
f.Serialize(Instance, obj);
Instance.Flush();
Instance.WaitForPipeDrain();
}
}
Edit
Changed the while loop to a if, to start the BeginRead. This solves the multiple callbacks but i stil dont get a complete message.
If the server is writing to the stream like:
write field 1
write field 2
write field 3
etc.
There is some time between the writes, and the receiver (your program) can be reading the first three fields while the server is still writing the others. The pipe stream doesn't know when the server is finished writing, so it can't buffer everything and send it to you all in one big chunk.
When the server writes everything to a memory stream first and then copies the memory stream to the pipe stream, your program can get it all at once. Maybe. If the server is sending a very large packet, you might read just part of it.
The pipe stream is just a stream of bytes. It doesn't impose any format on the data. It doesn't have any concept of records or anything like that. So you have to treat it like a stream of bytes and do your own composing of records, etc.
If you need to know the size of the record sent from the server, the server has to put that information in the stream for you. Typically, the server will write the length and then the data. The receiver can then read the length, convert it to an integer, and then read that many bytes from the stream. And, yes, it might take multiple reads in order to get all of the bytes. That's just the nature of a byte stream.
The other way to handle this is to have an end-of-record marker. So the server sends its data and your program reads until it finds the byte sequence that signifies the end of the record. You have to be careful, though, because the server could be sending multiple records and your read could grab the end of one record as well as the beginning of the next.
Working with byte streams can be a lot of work because you have to reconstruct records after reading the bytes. It's much easier to use an existing framework (like WCF, as mentioned in one of the comments) if you can.

Send a large file over tcp connection

I need to send a few large files to a computer over the internet. As a result I opened the ports that I plan to use on the router and forward the correspoinding ip addresses. anyways let me show you the classes that I have been working on in order to achieve this. This classes work great with small files but sometimes they fail with large files.
here is the code for the server: (it is a console application)
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication20
{
//server
class Program
{
static Server s;
public static void mm()
{
s = new Server("192.168.0.196");
s.startServer();
Console.Read();
}
static void Main(string[] args)
{
// Thread t = new Thread(new ThreadStart(mm));
// t.Start();
mm();
Console.Read();
s.disconnect();
}
}
class MyTCP
{
protected const int MaxChunkSize = 4096;
protected Int32 port { get; set; }
protected string serverIP { get; set; }
protected TcpClient client { get; set; }
protected static NetworkStream stream { get; set; }
protected void sendData(NetworkStream stream, Byte[] data)
{
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
}
protected String receiveData(NetworkStream stream)
{
// Buffer to store the response bytes.
Byte[] data = new Byte[MaxChunkSize];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
return responseData;
}
protected static Byte[] textToBytes(string text)
{
return System.Text.Encoding.ASCII.GetBytes(text);
}
public virtual void disconnect() { }
public bool isServerConected { get { return client.Connected; } }
}
[Serializable]
public class FileProperties
{
public string FileName { get; set; }
public string DestPath { get; set; }
public double FileSize { get; set; }
public FileAttributes fileAttributes { get; set; }
public System.Security.AccessControl.FileSecurity FileSecurity { get; set; }
public DateTime creationTime { get; set; }
public DateTime lastAccessTime { get; set; }
public DateTime lastWriteTime { get; set; }
}
class Server: MyTCP
{
private System.IO.FileStream _FileStream;
private static TcpListener server;
private static bool disconect;
/// <summary>
/// Constructor
/// </summary>
/// <param name="localAddr">The ip address of the server</param>
/// <param name="port">on what port the server going to be listening to?</param>
/// <param name="autoStartServer">start listening for connections now? you may call the startserver() method latter...</param>
public Server(string localAddr, Int32 port = 13000, bool autoStartServer = false)
{
this.port = port;
this.serverIP = localAddr;
if (autoStartServer)
start();
}
/// <summary>
/// Start listening for connections
/// </summary>
public void startServer()
{
start();
}
public override void disconnect()
{
// Close everything.
stream.Close();
client.Close();
server.Stop();
disconect = true;
}
void start()
{
server = null;
try
{
// TcpListener server = new TcpListener(port);
server = new TcpListener(IPAddress.Parse(serverIP), port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[MaxChunkSize];
String data = null;
// Enter the listening loop.
while (disconect==false)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
// Get a stream object for reading and writing
stream = client.GetStream();
int i;
try
{
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
if (data.ToUpper().Contains("<sendFile>".ToUpper()))
{
receiveFile(bytes);
}
continue;
}
}
catch { }
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
void receiveFile(Byte[] bytes)
{
// send 1
sendData(stream, textToBytes("<1>"));
// receive 2
int length = stream.Read(bytes, 0, bytes.Length);
byte[] tempA = new byte[length];
for (int k = 0; k < length; k++)
tempA[k] = bytes[k];
Stream ms = new MemoryStream(tempA);
FileProperties p = new FileProperties();
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
try
{
p = (FileProperties)x.Deserialize(ms);
if (Directory.Exists(p.DestPath))
{
//send 3
sendData(stream, textToBytes("<3>"));
}
else
{
//send 3
sendData(stream, textToBytes("<no>"));
return;
}
}
catch
{
//send 3
sendData(stream, textToBytes("<no>"));
return;
}
int i;
string temp = Path.Combine(new string[]{ p.DestPath, p.FileName + ".temp"});
_FileStream = new System.IO.FileStream(temp, System.IO.FileMode.Create, System.IO.FileAccess.Write);
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
if (i == 11 & System.Text.Encoding.ASCII.GetString(bytes, 0, i).ToUpper().Equals("</sendFile>".ToUpper()))
{
_FileStream.Close();
Console.WriteLine("D!");
File.SetAttributes(temp, p.fileAttributes);
File.SetAccessControl(temp, p.FileSecurity);
File.SetCreationTime(temp, p.creationTime);
File.SetLastAccessTime(temp, p.lastAccessTime);
File.SetLastWriteTime(temp, p.lastWriteTime);
if(File.Exists(temp.Substring(0, temp.Length - 4)))
File.Delete(temp.Substring(0, temp.Length - 4));
File.Move(temp, temp.Substring(0, temp.Length - 4));
//sendData(stream, textToBytes("<done>"));
Console.WriteLine("Done!");
return;
}
_FileStream.Write(bytes, 0, i);
}
return;
}
}
}
and the code for my client is:
using System;
using System.Net.Sockets;
using System.Windows;
using System.IO;
namespace WpfApplication23sdfd
{
[Serializable]
public class FileProperties
{
public string FileName { get; set; }
public string DestPath { get; set; }
public double FileSize { get; set; }
public FileAttributes fileAttributes { get; set; }
public System.Security.AccessControl.FileSecurity FileSecurity { get; set; }
public DateTime creationTime { get; set; }
public DateTime lastAccessTime { get; set; }
public DateTime lastWriteTime { get; set; }
}
abstract class MyTCP
{
protected const int MaxChunkSize = 4096;
protected Int32 port { get; set; }
protected string serverIP { get; set; }
protected TcpClient client { get; set; }
protected static NetworkStream stream { get; set; }
protected void sendData(NetworkStream stream, Byte[] data)
{
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
}
protected String receiveData(NetworkStream stream)
{
// Buffer to store the response bytes.
Byte[] data = new Byte[MaxChunkSize];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
return responseData;
}
protected static Byte[] textToBytes(string text)
{
return System.Text.Encoding.ASCII.GetBytes(text);
}
public virtual void disconnect() { }
public bool isServerConected { get { return client.Connected; } }
}
//client
class Client: MyTCP
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="serverIP">the ip address of the server</param>
/// <param name="port">through what port is the connection going to be established</param>
public Client(string serverIP, Int32 port = 13000, bool autoConnect = false)
{
this.port = port;
this.serverIP = serverIP;
if (autoConnect)
connect();
}
public bool connect()
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes("connect");
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
try
{
client = new TcpClient(serverIP, port);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
stream = client.GetStream();
return true;
}
catch
{
return false;
}
}
public override void disconnect()
{
// Close everything.
stream.Close();
client.Close();
}
static void ConnectOld(String server, Byte[] data)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
public void sendFile(string file, string destPath = "c:\\")
{
//let server know what you are going to be doing...
sendData(stream, textToBytes("<sendFile>"));
FileProperties p = new FileProperties {
creationTime = File.GetCreationTime(file),
fileAttributes = File.GetAttributes(file),
FileSecurity = File.GetAccessControl(file),
lastAccessTime = File.GetLastAccessTime(file),
lastWriteTime = File.GetLastWriteTime(file),
DestPath = destPath,
FileName = Path.GetFileName(file)
};
// receive 1
if (!receiveData(stream).ToUpper().Contains("<1>".ToUpper()))
{
MessageBox.Show("Error comunicating with server");
return;
}
// send object p to server
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(stream, p); // send 2
//recieve 3
if (!receiveData(stream).ToUpper().Contains("<3>".ToUpper()))
{
MessageBox.Show("Error incorrect parameters sent to server");
return;
}
System.IO.FileStream streamFile = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
while (true)
{
byte[] chunk = new byte[MaxChunkSize];
int index = 0;
// There are various different ways of structuring this bit of code.
// Fundamentally we're trying to keep reading in to our chunk until
// either we reach the end of the stream, or we've read everything we need.
while (index < chunk.Length)
{
int bytesRead = streamFile.Read(chunk, index, chunk.Length - index);
if (bytesRead == 0)
{
break;
}
if (bytesRead < MaxChunkSize)
{
byte[] temp = new byte[bytesRead];
for (var i = 0; i < bytesRead; i++)
temp[i] = chunk[i];
chunk = temp;
}
index += bytesRead;
}
if (index != 0) // Our previous chunk may have been the last one
{
sendData(stream,chunk); // index is the number of bytes in the chunk
}
if (index != chunk.Length) // We didn't read a full chunk: we're done
{
sendData(stream, textToBytes("</sendFile>".ToUpper()));
//receiveData(stream);//wait recall missing to check results
return;
}
}
}
}
}
the way I instantiate the client class is by providing the IP address of the server as:
Client c = new Client("192.168.0.196");
c.sendFile(#"A:\Users\Tono\Desktop\a.mp4");
the server has to be running first before executing that code.
I don't know why is so complicated to send a file over the internet using sockets. I don't konw WCF that's why I been loosing a lot of time creating this classes. Maybe there are already some built in classes that will enable me to send files over the internet to a different computer. I know just the basics of networking thereofore it will be nice if I could do it with a simple class. I don't undersatnd why my classes do not work all the time? if I increase the buffer size will my classes be more efficient? do I have to wait or pause my program a little bit before sending more bytes? It will be nice if someone can tell me what is wrong with this classes. They work nice with small files but with large files sometimes it does not work...
Here is my code to send a large file.
Some tips:
Check your buffer size. If it's too big it will fail.
Socket flags. The partial flag works the best.
Socket timeout is needed due to the transfer time.
Client :
string IPAddress = "";
int Port = 500;
string Filename = #"C:\Users\Ben\Desktop\TT.zip";
int bufferSize = 1024;
byte[] buffer = null;
byte[] header = null;
FileStream fs = new FileStream(Filename, FileMode.Open);
bool read = true;
int bufferCount = Convert.ToInt32(Math.Ceiling((double)fs.Length / (double)bufferSize));
TcpClient tcpClient = new TcpClient(IPAddress, Port);
tcpClient.SendTimeout = 600000;
tcpClient.ReceiveTimeout = 600000;
string headerStr = "Content-length:" + fs.Length.ToString() + "\r\nFilename:" + #"C:\Users\Administrator\Desktop\" + "test.zip\r\n";
header = new byte[bufferSize];
Array.Copy(Encoding.ASCII.GetBytes(headerStr), header, Encoding.ASCII.GetBytes(headerStr).Length);
tcpClient.Client.Send(header);
for (int i = 0; i < bufferCount; i++)
{
buffer = new byte[bufferSize];
int size = fs.Read(buffer, 0, bufferSize);
tcpClient.Client.Send(buffer,size,SocketFlags.Partial);
}
tcpClient.Client.Close();
fs.Close();
Server :
int Port = 500;
TcpListener listener = new TcpListener(IPAddress.Any, Port);
listener.Start();
Socket socket = listener.AcceptSocket();
int bufferSize = 1024;
byte[] buffer = null;
byte[] header = null;
string headerStr = "";
string filename = "";
int filesize = 0;
header = new byte[bufferSize];
socket.Receive(header);
headerStr = Encoding.ASCII.GetString(header);
string[] splitted = headerStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (string s in splitted)
{
if (s.Contains(":"))
{
headers.Add(s.Substring(0,s.IndexOf(":")), s.Substring(s.IndexOf(":") + 1));
}
}
//Get filesize from header
filesize = Convert.ToInt32(headers["Content-length"]);
//Get filename from header
filename = headers["Filename"];
int bufferCount = Convert.ToInt32(Math.Ceiling((double)filesize / (double)bufferSize));
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
while(filesize > 0)
{
buffer = new byte[bufferSize];
int size = socket.Receive(buffer,SocketFlags.Partial);
fs.Write(buffer,0,size);
filesize -= size;
}
fs.Close();
Hope this will help someone.
There are a few issues that I can see immediately. The one that may be causing your program to only work some of the time is the fact that sending via TCP will not guarantee that every send will result in an identically-sized receive on the other side.
Your protocol seems to assume it will, because you're waiting for a read of exactly 11 bytes for the </sendFile>, whereas it could be received in multiple separate reads. E.g.: "[file data...]". If this happens, your code will not correctly finish.
It's also worth noting that the ASCII encoding is 7-bit, and so binary files (such as the MP4) will be received incorrectly (even if you fix the above). If it is binary data, you should not attempt to convert it to a string, but instead write it to file directly from the byte[].
If you wish to contine down this route (rather than using the many existing file transfer systems already available as mentioned in another answer) then you may also want to change your protocol so that instead of delimiting the file with <sendFile>...</sendFile>, you send the file's length initially, which will allow you to send files that may contain one of these special tags.
Sending files / directories over sockets is not trivial. I would recommend using some file transfer library (over sockets, or maybe higher level protocol, i.e. rsync, ftp, http, etc), rather than trying to code the thing from scratch.
After skimming over the code - try sending a large file containing some uniform content (filled with '0's or something). If it would pass through - your xml thing doesn't work.
Basically what you are doing is correct. The main point I could suggest for improvement is setting the MaxChunkSize to a larger value, as much as 65000. This will enable the socket code to handle any fragmentation, which will be much more efficient than any splitting you will do by yourself.
Also, you are aware to the fact that sending a large file should take some time. At a 100 Mbit LAN, the bandwidth would be (theoretically) 12.5 MByte per second. Thus sending a 700 MByte file will still take 56 seconds. Of course that the actual throughput depends and many factors in the network and the computers and network hardware involved, so it would be expected to be slower.
Finally, Wireshark (or any other sniffer program) is the most basic and invaluable tool in the networking programmer toolbox. Use it on both the server and the client to see how the TCP packets are transmitted, to see if you can identify any causes for the slow trasnfer.

Categories