Problem with Socket Communications

deejross

Member
Joined
May 10, 2006
Messages
8
Programming Experience
3-5
Well, to make a long story short, I have written a client/server app that loads a few entries into a listbox and when a user clicks on one of those items, information about that item is sent to the client. It all works just fine until you try clicking quickly between items. My code looks something like this:

VB.NET:
SERVER: (uses BeginReceive for async)

Sub onIncommingData(bytes() as byte, bytesRead as integer)
    Dim data as string = ASCII.GetString(bytes, 0, bytesRead)

    Select Case data
        Case "01"
            'do something
        Case "02"
            'do something else
    End Select
End Sub


CLIENT:

Sub Listbox1_Click(...blah...)
    Dim bytes(1023) as byte

    sock.Send(ASCII.GetBytes("01"))
    sock.Receive(bytes, 0, bytes.Length)
    'load information from received data
End Sub


As I said, this works just fine until you start clicking quickly between items in the listbox. It always gets stuck at the Receive line and you have to kill the process. I have tried adding ReceiveTimeout, but when it times out, it also closes the connection, which might as well, kill the app because without the connection, it's useless. Is there anyway to force the socket to stop Receiving and continue execution without disconnecting the socket?


Thanks in advance for your help

BTW, I'm using Visual Studio 2005
 
I havent really looked at the code so I might be way off but do you do any form of threading, seeing as you have clients and a server? If you're using TCP/IP to communicate between your client and server, rather user the TCPClient and TCPListener classes? This way you dont have to work with bytes and everything is handled nice and easily. I've used them myself and in ten lines of code, ALL the communication for both the client and server is handled.

If you dont handle this through threading then you'll get lots of problems because the client and server are both waiting for different replies and things like that.

I havent really explained any of this well but I think that your packet order could be getting mixed up or I am completely on the wrong track.
 
Well, the client uses the TCPClient for sure, but it uses the TCPClient's .Client (which is a socket object) to communicate. Otherwise, I would have to use network streams, which I played around with, but worked no different that regular Send/Recieve (that I could tell anyways). You still had to work with bytes and all.

As for threading, the server is multi-threaded (i think), but the client is not because it should only use one socket. for the server to be truely multi-threaded, would I need to actually create a new thread for every call to BeginReceive? Because right now, you call BeginReceive and it continues execution until data is received, at which point an event is fired.

My original thought is that the packets are getting mixed up, or that the packets are running together, so the server doesn't have time to respond. Since I can't actually debug it, it's impossible to tell. However, I did try a packet monitor which showed that right when it freezes, the client sent a request for something, but the server didn't respond to it at all.
 
Back
Top