I’m using the TCP client in WP7. At the moment i’m just using the example code from MSDN, so it should work. But for some reason this particular response is being cut short.
It should reply (followed by a lot of null bytes, from the buffer):
202- multiline response follows\r\ntimestamp=0x00000000 checksum=0x00000000\r\nname=\”FLASH:Flash\xshell.xex\”\r\n.\r\n
But instead it’s returning (and doesn’t have any trailing null bytes):
202- multiline response follows\r\n
My code for getting the response from the TCP server is:
try
{
if (!_isConnected)
Connect();
if (!_isConnected)
return null;
SendTextCommand(command);
string response = "";
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
socketEventArg.UserToken = null;
socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
response = Encoding.ASCII.GetString(e.Buffer);
response = response.Trim('\0');
}
else
throw new Exception(e.SocketError.ToString());
_pausingThread.Set();
});
_pausingThread.Reset();
_socket.ReceiveAsync(socketEventArg);
_pausingThread.WaitOne(TIMEOUT_MILLISECONDS);
return response;
}
catch (Exception ex) { GenerateException(ex.Message); return "123"; }
To fix it, you have to check if the response is multi-line. If it is, then you loop until it finishes with “.\r\n”. Otherwise you read once and you’ve finished. Like so: