M
Markgoldin
Here is code I am using to create a socket listener:
public void Run()
{
go = true;
Random rand = new Random();
// Buffer for reading data
Byte[] bytes = new Byte[1024];
String data = null;
while (go)
{
TcpClient tcpc = FromAnotherClass.tcpl.AcceptTcpClient(); //accept
connection
data = null;
// Get a stream object for reading and writing
NetworkStream stream = tcpc.GetStream();
// Loop to receive all the data sent by the client.
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
if (data != "")
{
System.Console.WriteLine(data);
}
}
Thread.Sleep(1000 + rand.Next(2000));
}
}
My question is can this code handle connections from multiple clients?
Or should I have a separate listener for each client?
Thanks for help.
public void Run()
{
go = true;
Random rand = new Random();
// Buffer for reading data
Byte[] bytes = new Byte[1024];
String data = null;
while (go)
{
TcpClient tcpc = FromAnotherClass.tcpl.AcceptTcpClient(); //accept
connection
data = null;
// Get a stream object for reading and writing
NetworkStream stream = tcpc.GetStream();
// Loop to receive all the data sent by the client.
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
if (data != "")
{
System.Console.WriteLine(data);
}
}
Thread.Sleep(1000 + rand.Next(2000));
}
}
My question is can this code handle connections from multiple clients?
Or should I have a separate listener for each client?
Thanks for help.