View Single Post
Old May 21st, 2007, 1:16 PM   #1
surf3rb0y
Newbie
 
Join Date: Dec 2006
Posts: 10
Rep Power: 0 surf3rb0y is on a distinguished road
Asynchronous Network communication

I am trying to open 2 tcp connections to a server both asynchronous connections. My problem is when i setup the async call back only one of them works and the other gives me an error. Is there an easy way to setup two async call backs that do not conflict with one another.

Surferboy


Private Sub CommandRecieveCallBack(ByVal arone As IAsyncResult)
        Try
            If CommandSocket.Connected = True Then
                Dim bytes() As Byte = CType(arone.AsyncState, Byte())
                Dim numbytes As Int32 = CommandSocket.EndReceive(arone)
                If numbytes = 0 Then
                    CommandSocket.Shutdown(SocketShutdown.Both)
                    CommandSocket.Close()

                    Dim dlgone As New NoParamsDelegate(AddressOf DisConnectedUI)
                    Me.Invoke(dlgone)
                Else
                    Dim Recv As String = ASCII.GetString(bytes, 0, numbytes)
                    ' clear the array

                    Array.Clear(bytes, 0, bytes.Length)
                    ' show changes in UI
                    Dim dlgone As New OneStringDelegate(AddressOf DisplayRecieveData)
                    Dim args() As Object = {Recv}
                    Me.Invoke(dlgone, args)
                    ' Begin Recieving again
                    CommandSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf CommandRecieveCallBack, bytes)
                End If
            End If

        Catch ex As Exception

            System.Console.WriteLine("Disconnected")

        End Try


    End Sub

Private Sub CommandConnectCallBack(ByVal arone As IAsyncResult)
        Try
            CommandSocket.EndConnect(arone)
            'SendCommandData()
            Dim dlgone As New NoParamsDelegate(AddressOf ConnectedUI)
            Me.Invoke(dlgone)
            ' begin recieve
            ' create buffer
            Dim bytes(17000) As Byte
            CommandSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf CommandRecieveCallBack, bytes)
        Catch ex As SocketException
            MessageBox.Show("The Server was either down or not responding.  Please double check your server information that is under EtherNet->Configure.")
            Console.WriteLine("Failed in ConnectCallBack")
        End Try

    End Sub
surf3rb0y is offline   Reply With Quote