none
The IAsyncResult object was not returned from the corresponding asynchronous method on this class RRS feed

  • Question

  • Hello,
    during a BeginReceive on which act two threads simultaneously launched on a server with two clients connected, at the time of the send of one client of the callback client BeginReceive returns the wrong socket...

    Below is the code.

    Private Sub ActiveRecevingData(ByVal clientSocketSD As Socket)
    
            While (True)
                Try
                    RequestReceived_SD.Reset()
    clientSocketSD.workSocket.BeginReceive(clientSocketSD.buffer, 0, ClientState.BufferSize, 0, New AsyncCallback(AddressOf SD_manager), clientSocketSD)
                    RequestReceived_SD.WaitOne()
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End While
    End Sub
    
    Private Sub SD_manager(ByVal ar As IAsyncResult)
    
    SyncLock SD_manager_SL
    Dim clientConnected As Socket = CType(ar.AsyncState, socket)
    End SyncLock
    End Sub
    The cycle has two threads waiting for two clients (A and B) on RequestReceived_SD.
    When client A makes a BeginSend to the server, the server passes control to the callback SD_manager, but returns the client socket B .....
    And this creates an exception in the next EndReceive server ...
    Ii've also found out last night that the server always receives the call from the client who has been connected as last, even if the call comes from the first client that has been connected.

    ie:

    If clients connect to the server in the following sequence:

    client A
    client B

    and then the client A sends a BeginSend to the server, the server is as if instead received the call from the client B (the last connected).
    In fact, in the procedures SD_manager_SL clientConnected has the Client B ip address instead of having the one of the Client A ...

    Do you have any idea or suggestion?

    Thanks.
    Genko
    Monday, July 13, 2015 2:09 PM

Answers