Data Sheet
35
ETHERIO24TCPDATASHEET
//********************************************************************
// HandelClientComm()
// This method runs in the client thread.
// It will handle data coming from the client and loop until
// the client disconnects.
//********************************************************************
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//********************************************************
// Blocks until a client sends a message
//********************************************************
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
System.Diagnostics.Debug.WriteLine("catch");
//********************************************************
// A socket error has occured
//********************************************************
break;
}
if (bytesRead == 0)
{
//********************************************************
// The client has disconnected from the server
//********************************************************
System.Diagnostics.Debug.WriteLine("Client Disconnected");
break;
}
//************************************************************
// Message has successfully been received
//************************************************************
ASCIIEncoding encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0,
bytesRead));
}
tcpClient.Close();
}
©
2013ElexolPtyLtd Revision1.3