cdutoit

cdutoit
- Name: [not set]
- Favorite Languages: [not set]
- Website: [not set]
- Location: [not set]
- About Me: [not set]
-
C# Tutorial - Simple Threaded TCP Server
07/25/2010 - 16:49
Hi,
I am trying to send an acknowledgement back to the client once they have sent a message to the server.
I where should I add the code below to the HandleClientComm to get it working?
byte[] buffer = encoder.GetBytes("Hello Client! Message Received!");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();HandleClientComm
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
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
}
tcpClient.Close();
}The listener on the client does not pick anything up if I just add it below the
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));Not sure what I am missing?
Recent Comments