C# TCP socket with session

Posted by Zé Carlos on Stack Overflow See other posts from Stack Overflow or by Zé Carlos
Published on 2010-03-16T19:05:41Z Indexed on 2010/03/16 19:11 UTC
Read the original article Hit count: 314

Filed under:
|

Is there any way of dealing with sessions with sockets in C#?

Example of my problem:
I have a server with a socket listening on port 5672.

TcpListener socket = new TcpListener(localAddr, 5672);  
socket.Start();  
Console.Write("Waiting for a connection... ");  

// Perform a blocking call to accept requests.  
TcpClient client = socket.AcceptTcpClient();  
Console.WriteLine("Connected to client!");

And i have two clients that will send one byte. Client A send 0x1 and client B send 0x2. From the server side, i read this data like this:

Byte[] bytes = new Byte[256];
String data = null;

NetworkStream stream = client.GetStream();
while ((stream.Read(bytes, 0, bytes.Length)) != 0)
{
     byte[] answer = new ...
     stream.Write(answer , 0, answer.Length);
}  

Then client A sends 0x11. I need a way to know that this client is the same that sent "0x1" before.

© Stack Overflow or respective owner

Related posts about c#

Related posts about sockets