you have to find a way to tell your code what you are receiving, like a protocol. In first place Tcp communication is a transport of bytes. these bytes have a meaning. Either they are the characters of a string or some encoded numbers, like 4 bytes make a 32bit integer.
you can set the very first byte of your tcp-Message as a code which tells you what you are getting like:
TcpListener server = new TcpListener(IPAddress.Any, Port);
Console.Write("Waiting for Client connection... ");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("\nClient Connected....!");
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
int data = stream.Read(buffer, 0, client.ReceiveBufferSize);
switch (buffer[0]):
{
case 1:
string ch = Encoding.Unicode.GetString(buffer, 0, data);
Console.WriteLine(String.Format("Received string: {0}", ch));
break;
case 2:
int i=0;
for (int x=1;x<5;x++){
i=i<<8;
i+=buffer[x];
}
Console.WriteLine(String.Format("Received int: {0}", i));
break;
}
client.close();
of course you can fill an array of ints by repeating that little loop for each int. so you should check in advance how many ints are encoded in that tcp-message. and of course you need to insert the first byte when sending the message.