I have a Socket Client and I was wondering what would be the correct approach to read the incoming data from it.
Currently I am using the follow function:
private void _ReadResponsePackets()
{
while (_socket.Connected)
{
try
{
byte[] bytes = new byte[1500];
_socket.Receive(bytes);
if (bytes.Length > 0)
LoginserverPackets.Enqueue(bytes);
}
catch (Exception ex)
{
_log.Write(ErrorType.ERROR, "[LOGINCLIENT] " + ex.ToString(), true);
}
}
}
That is called by it is own thread:
Thread _readDataThread = new Thread(_ReadResponsePackets);
_readDataThread.Start();
As you can see I am merelly reading and pilling up the packets received on a queue list to be further threaded but my question here is:
- Should I use a Thread.Sleep in my
read function or leave it as it is ? - Would it impact more on the
performance or memory usage as it is
or using Thread.Sleep ?
Any other toughts ?
Please check the following link, the socket will be in blocking mode, so you don’t need to use Thread.Sleep. Listener