3

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 ?

Prix
  • 19,417
  • 15
  • 73
  • 132
  • Have you though about using async sockets? I have a class that uses this is you like to see, but there are many good examples on the net aswell. – Jethro Jun 30 '11 at 21:51
  • @Jethro have not, in fact I dont have much background for socket programming and start it like this to gain some understanding on the area first. Basicly what benefits I would gain in my case from using async sockets ? Also what are your toughts in regards the other questions as well ;) even if I am not using a socket, the infite while and memory consumption is still a question ehhe – Prix Jun 30 '11 at 21:53
  • Take a look at this post on stackoverflow. http://stackoverflow.com/questions/2512356/sync-vs-async-sockets-performance-in-net – Jethro Jun 30 '11 at 21:57
  • I see at least one question per week about sockets in .Net and ask every one of them why not use [WCF](http://msdn.microsoft.com/en-us/netframework/aa663324)? A lot of heavy lifting is done for you so you don't have to worry about the best way to receive data from a socket :) – Jay Jun 30 '11 at 21:58
  • Well you would think that the cpu usage will hit 100% but it doesn't if I remember correctly. Also _socket.Connected doesn't always turn to false when the socket is disconnected and then your cpu will hit 100%, so you must just watch that. – Jethro Jun 30 '11 at 22:01
  • "As per the documentation, the .Connected property reflects the connected state AS OF the last Send/Receive operation " http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/f36ec6c4-a47a-4545-b808-118334eefce0/ – Jethro Jun 30 '11 at 22:03
  • @Jethro that is ok I have a secondary status that I can rely on to verify wether I am or not connected so I will move to that. – Prix Jun 30 '11 at 22:05
  • 1
    @Jay well like I said I just want to gain some understanding before jumping into something very different, I am not really doing something too complicated here and wanted it to be as simple as possible. But my goal is to move into wcf. – Prix Jun 30 '11 at 22:07
  • @Prix, I agree, it's always handy to go deeper and have a better understanding of how things work. :) – Jethro Jun 30 '11 at 22:09
  • I can certainly understand (and hats off to you) for wanting to understand what happens in the black box. IMO, WCF is way more simple than sockets. – Jay Jun 30 '11 at 22:11

1 Answers1

0

Please check the following link, the socket will be in blocking mode, so you don't need to use Thread.Sleep. Listener

Community
  • 1
  • 1
Jethro
  • 5,896
  • 3
  • 23
  • 24