JSON deserialization works locally but not when from remote server - c#

Weird problem here.
I have two applications.
Server app
A Unity3D application which captures the frames from the game, converts to JSON and sends to the connected client.
Client app
Connects to server and receives data via TCP. Received data is converted to a string and then deserialized from JSON to an object.
Server send:
public void Send(byte[] dataToSend)
{
var message = new MessageData();
message.Bytes = dataToSend;
var json = JsonConvert.SerializeObject(message);
var bytesToSend = Encoding.ASCII.GetBytes(json);
clientSocket.Send(bytesToSend, 0, bytesToSend.Length, SocketFlags.None);
}
Client Receive:
public void StartReceiving(TcpClient client)
{
if (!client.Connected) return;
try
{
var stream = client.GetStream();
if (!stream.DataAvailable) return;
var buffer = new byte[500000];
var bytes = stream.Read(buffer, 0, buffer.Length);
var json = Encoding.ASCII.GetString(buffer);
var message = JsonConvert.DeserializeObject<MessageData>(json);
//raise image received event
ImageReceivedEventArgs args = new ImageReceivedEventArgs();
args.Bytes = message.Bytes;
args.Time = DateTime.Now;
OnImageReceived(args);
}
catch (Exception exception)
{
}
}
MessageData is just a class with a byte array property called "Bytes".
Now, for some reason, this all works absolutely fine locally and the JSON comes out perfectly. However, put my server application onto the server and my JSON gets messed up.
Here is the error that I am getting:
Unexpected character encountered while parsing value: Q. Path '', line 0, position 0.
If it's any sort of a hint, my server application is running on an Amazon EC2 Instance.
What's up?

Related

Connecting to Serial->Ethernet converter box via Telnet, getting strange response (no matter what I send)

I'm trying to communicate with a device that has a Serial COM port that goes out (using RS232 Protocol), which I've hooked up to a converter box that converts the connection to Ethernet.
I've successfully connected to the converter box (which has its own IP) and I've successfully communicated with the device by sending it commands over telnet by using PuTTY, to which it has responded with various data that I've been able to parse.
I've established a connection by creating a new TcpClient, and I'm able to send strings to the device, but every time I get a response back, it's always "??\u0003" which I've researched and found that \uhhhh is a Unicode escape protocol. This confuses me because I have used ASCII encoding for everything.
public string TcpConnect(string cmd)
{
var client = new TcpClient();
//cmd = "ping";
Console.WriteLine("Connecting to server");
client.Connect("169.254.20.40", 23); //22 = ssh, 23 = telnet, 80 = http
Console.WriteLine("CONNECTED SUCCESSFULLY");
Stream tcpStream = client.GetStream();
ASCIIEncoding A = new ASCIIEncoding();
byte[] enable = A.GetBytes("enable" + Environment.NewLine);
byte[] connect = A.GetBytes("connect line 1" + Environment.NewLine);
byte[] ba = A.GetBytes(cmd);
Console.WriteLine("Transmitting.....");
tcpStream.Write(enable, 0, enable.Length);
tcpStream.Write(connect, 0, connect.Length);
tcpStream.Write(ba, 0, ba.Length);
byte[] responseBytes = new byte[4096];
int numBytesRead = tcpStream.Read(responseBytes, 0, responseBytes.Length);
var message = A.GetString(responseBytes, 0, numBytesRead);
Console.WriteLine("\nServer Closed.");
return message;
}
If I were to pass in "$TEAA4B9\r\n" as the message, I would expect something along the lines of "$TEA,086,040,031,000,3798" which is nowhere close to what I'm getting (which is ??\u0003)
Nevermind figured it out
Just kidding! Here's what I did: I added a "System.Threading.Thread.Sleep("1000") before the "tcpStream.Read" line at the bottom, and now it outputs the data I need. The device was outputting garbage on the first line (perhaps a handshake, not sure) and that's all that was being read before it was being stored into "message" and returned (not enough time was spent reading before it moved on to the next line of code)

After receiving string as memory stream and encoding, more chars are added

I built an app which receiving and sending data to server by the code below, and I noticed It's adding some chars to the string I send as MemoryStream when I'm getting the string back. Here's the code and the debugging information:
Client:
while (true)
{
if (stream.DataAvailable)
{
while ((i = stream.Read(ByteBuffer, 0, ByteBuffer.Length)) != 0)
{
ms.Write(ByteBuffer, 0, ByteBuffer.Length);
if (stream.DataAvailable)
continue;
else
break;
}
ToReturn = Encoding.ASCII.GetString(ms.ToArray());
return ToReturn;
}
}
}
Server:
MemoryStream response = new MemoryStream();
response = Protocol.ProcessRequest(dataRecieved, ClientAddr);
#endregion
Console.WriteLine("Trying to send back response." + Encoding.ASCII.GetString(response.ToArray()));
stream.Flush();
response.WriteTo(stream);
I've checked with the debugger and what printed with the console:
the sent information is just fine, for example:
response.Id^Name^Type^SubType^Description^AddedBy^AddedDT^IsSpecial^Amount#1^VGA cable^cable^display^Very old and common display cable.^Aviv^14/01/2019 22:04:34^False^3345#2^HDMI cable^cable^display^newer and better display cable. can pass network, audio and info.^Aviv^14/01/2019 22:05:30^False^4793
but the info received on the other side of the socket (the client) was:
Id^Name^Type^SubType^Description^AddedBy^AddedDT^IsSpecial^Amount#1^VGA cable^cable^display^Very old and common display cable.^Aviv^14/01/2019 22:04:34^False^3345#2^HDMI cable^cable^display^newer and better display cable. can pass network, audio and info.^Aviv^14/01/2019 22:05:30^False^4793alse^4
-with these (alse^4) few chars at the end. can anyone tell me what's the encoding problem? Thanks.
AGAIN: the output from the server is fine
//ms.Write(ByteBuffer, 0, ByteBuffer.Length);
ms.Write(ByteBuffer, 0, i);

Trouble with sending using TCP File Transfer Client

I have to develop a simple TCP File Transfer Client as part of an exercise/demonstration but I am having trouble with data delivery.
I am already provided with the TCP File server which will receive the incoming files and I have to code a Client that will connect to and send a file to the server. Thus far I have been successful as far as converting the data of the selected file into to a format ready to transferring and successfully opening a connection and then sending of the data, however even encounter a problem on server that is receiving - I am not allowed to change the code of the server and should thus change my client code in such a way that the data sent can be interpreted by the server. Here is the code I use (some is left out which is simple overhead, like input-box to get IP address etc.):
Link for TCP Server VS Solution (if you prefer): https://www.mediafire.com/?682owf9wtdzmxac
TCP File Transfer Client Sending method:
private void TransferFile(string _sFileName, string _sIPAdress)
{
//Convert data for transfer
Stream strmfilestream = File.OpenRead(_sFileName);
Byte[] bFileBuffer = new Byte[strmfilestream.Length];
//Open TCP/IP Connection
TcpClient tcpClientSocket = new TcpClient(_sIPAdress,8080);
NetworkStream nsNetworkStream = tcpClientSocket.GetStream();
nsNetworkStream.Write(bFileBuffer,0,bFileBuffer.GetLength(0));
nsNetworkStream.Close();
}
*Note: _sFileName is just the full file path + File name from a OpenFileDialog.
Here is the Load Method of the server to get things going:
if (!Directory.Exists(#"C:\TCPFileServer"))
Directory.CreateDirectory(#"C:\TCPFileServer");
//Get Ip address of server host machine
IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
lblServerIP.Text = IPHost.AddressList[5].ToString();
lstSockets = new ArrayList();
Thread thdListener = new Thread(new ThreadStart(listenerThread));
thdListener.IsBackground = true; //This will enabe the thread to terminate when application is closed
thdListener.Start();
Here is the listener thread method:
public void listenerThread()
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, 8080);
tcpListener.Start();
while (true)
{
Socket handlerSocket = tcpListener.AcceptSocket();
if (handlerSocket.Connected)
{
this.Invoke((Action)(() => lstConnections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + " connected.")));
lock (this)
{
lstSockets.Add(handlerSocket);
}
ThreadStart thdsHandler = new ThreadStart(handlerThread);
Thread thdHandler = new Thread(thdsHandler);
thdHandler.Start();
}
}
}
And then lasty here is the handler thread method:
public void handlerThread()
{
try
{
int iBlockSize = 1024 * 3000; //3mb block size
Byte[] dataByte = new Byte[iBlockSize];
Byte[] rcvdData = new Byte[128000 * 1024];//128mb File Limit
Socket handlerSocket = (Socket)lstSockets[lstSockets.Count - 1];
NetworkStream networkStream = new NetworkStream(handlerSocket);
int i = 0;
int iRcvdBytes = 0;
while (true)
{
//Read from socket and store to buffer 'dataByte'
iRcvdBytes = networkStream.Read(dataByte, 0, iBlockSize);
dataByte.CopyTo(rcvdData, i);//Copy recieved bytes,from buffer, to another byte array
i += iRcvdBytes;
if (iRcvdBytes == 0) break;
}
//Get the File name length, BitConvertor occupies the first 4 bytes
int iFileNameLength = BitConverter.ToInt32(rcvdData, 0);
//Get the file name using length as the size and 4 as the offset
string sFileName = Encoding.ASCII.GetString(rcvdData, 4, iFileNameLength);
Stream fileStream = File.Open("C:\\TCPFileServer\\" + sFileName, FileMode.Create);
//Populate raw File on local machine
fileStream.Write(rcvdData, 4 + iFileNameLength, i - 4 - iFileNameLength);
fileStream.Close();
//Update BRS Net Files Server Log
this.Invoke((Action)(() => lstConnections.Items.Add(sFileName + ": Transfered.")));
//Close Connection
networkStream.Close();
handlerSocket = null; //Clear socket
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Now I have debugged and as far I can determine, the first time I can see a problem is when we are trying to determine the file name length where the code reads int iFileNameLength = BitConverter.ToInt32(rcvdData, 0); - When debugging this variable is always determined as '0', which I assume is not correct? I am not sure. The required results that needs to be achieved is that the Server should receive the file and after the successful transfer, the file name should be displayed in the ListBox.
Here is screen shots showing the problem i'm experiencing:
We can see that bytes are definitely received:
Note that the file name can not be retrieved from using the file name length and offset:
And thus, the following error occurs:
My experience and expertise lie elsewhere and this is the first time I am coding within this paradigm(File transfer over network). I do however have theoretical knowledge about/studied the OSI model and TCP/IP Protocol stack, but never coded anything like this. One note given to me was that the server was coded with the assumption that it will be executed on a specific PC and I am allowed to change code of the Server application if it is absolutely necessary.
Try this. You have to do thinks in reverse on the client and using a List makes things easier.
private void TransferFile(string _sFileName, string _sIPAdress)
{
List<Byte> bFileBuffer = File.ReadAllBytes(_sFileName).ToList();
byte[] bFileName = Encoding.ASCII.GetBytes(_sFileName);
bFileBuffer.InsertRange(0, bFileName);
//Get the File name length, BitConvertor occupies the first 4 bytes
byte[] brcvdDataCount = BitConverter.GetBytes((UInt32)_sFileName.Count());
bFileBuffer.InsertRange(0, brcvdDataCount);
//Open TCP/IP Connection
TcpClient tcpClientSocket = new TcpClient(_sIPAdress, 8080);
NetworkStream nsNetworkStream = tcpClientSocket.GetStream();
nsNetworkStream.Write(bFileBuffer.ToArray(), 0, bFileBuffer.Count);
nsNetworkStream.Close();
}​​
There are two things with the server code you should be aware of
/Get the File name length, BitConvertor occupies the first 4 bytes
int iFileNameLength = BitConverter.ToInt32(rcvdData, 0);
//Get the file name using length as the size and 4 as the offset
string sFileName = Encoding.ASCII.GetString(rcvdData, 4, iFileNameLength);​
1) You need to add a byte count to beginning of upload.
2) The code is using Ascii Encoding which means you can not upload binary data.

C# HttpListener resets connection

I am building a system with on one side an Android app, that uses an HTTPURLConnection to communicate with the other side, which is a C# HttpListener. Over this channel they communicate with XML data.
This works very well, except for some larger data. When I try to communicate that, I see the XML from Android arrives the C# application en the C# application does respond. However, before the data arrives the Android, I get a "Connection reset by peer." exception.
This is the Android code:
URL url = new URL(urlString);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(tel.length());
connection.setReadTimeout(30000);
// write our telegram...
OutputStream output = new BufferedOutputStream(connection.getOutputStream());
output.write(tel.getBytes());
output.flush();
and here is the reply read:
InputStream input = new BufferedInputStream(connection.getInputStream());
if (connection.getResponseCode() == HttpStatus.SC_OK) {
String r = null;
byte cbuf[] = new byte[connection.getContentLength()];
if (input.read(cbuf, 0, connection.getContentLength()) != -1) {
r = new String(cbuf);
}
reply = Telegram.fromString(r);
} else {
throw new ProtocolException("HTTP Error code: "+connection.getResponseCode());
}
and this is the C# code:
httpListener = new HttpListener();
httpListener.Prefixes.Add(String.Format("http://*:{0}/", port);
httpListener.Start();
Turns out, the connection.getContentLength() does not always match the number of bytes read in the input.read(), so the read call waits (and eventually, the server resets as it is done sending I guess).
To fix, I rewrote the receiving side to:
int bufs = 256, read;
ByteArrayOutputStream cbuf = new ByteArrayOutputStream(bufs);
byte[] tempbuf = new byte[bufs];
while ((read = input.read(tempbuf, 0, bufs)) != -1) {
Log.d(PocketApplication.TAG, "Read "+read+" bytes.");
cbuf.write(tempbuf);
}
and it works fine now. Making the size of bufs too large causes it to fail again (e.g. with 1024, the same problem occurs).

EPP always returns greeting

Hey Guys i am starting an EPP client and it only returns the greeting from the server, even when i try send my login command.
here is my code, what is wrong with it?
using (var _tcpClient = new TcpClient(_endpoint.Host, _endpoint.Port))
{
using (var sslStream = new SslStream(_tcpClient.GetStream(), false, ValidateServerCertificate))
{
sslStream.AuthenticateAsClient(_endpoint.Host);
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(requestData);
xdoc.Save("C:/request.xml");
string data = xdoc.InnerXml;
byte[] bytedata = Encoding.UTF8.GetBytes(data);
//Get the request stream.
sslStream.Write(bytedata, 0, bytedata.Length);
// Write the data to the request stream.
sslStream.Flush();
var response = ReadMessage(sslStream);
XResponse = XDocument.Parse(response);
XResponse.Save("C:/response.xml");
}
}
return XResponse;
}
private string ReadMessage(SslStream sslStream)
{
// The first four bytes will be the the content length as a network order (big-endian) 32-bit number.
var lengthBytes = new byte[4];
sslStream.Read(lengthBytes, 0, 4);
Array.Reverse(lengthBytes);
var length = BitConverter.ToInt32(lengthBytes, 0) - 4;
// Create a byte array of the correct size for the response.
var messageBytes = new byte[length];
var returned = 0;
while (returned != length)
{
returned += sslStream.Read(messageBytes, 0, length);
}
return Encoding.UTF8.GetString(messageBytes);
}
Even if i do not write anything with the ssl stream it still returns the greeting.
if you guys could point me in the right direction it would be greatly appreciated.
Each time you send any command or open a connection, you must read and flush the response. I fell into this trap when developing my EPP client.
Connect. Read response (you get the greeting)
Send Login command. Read response (you get the login response)
Send Command. Read Response.
Send Disconnect. Read Response.
I imagine you're connecting, then sending a login command and wondering why the response you just got was the greeting. You missed a step. :)
Upon connection the server will typically reply with a greeting.
Read the greeting response upon connection. Then you can send commands and receive responses as normal.

Categories