Asynchronous Socket Events

B

Borco

We have an application that is using sockets in a multithreaded
environment. We are seeing a situation arise where the Connected event
is firing and the DataArrival event fires so quickly afterwards that we
cannot complete our connection processing logic and are forced to drop
the data and therefore the entire connection. Is there an effective way
to have the data arrival event queued until the connection can signal
complete?

TIA
 
J

Jerod Venema

Can you describe in a little more detail what exactly you're doing?
Generally, you don't start receiving data until you explicitly do a
..BeginReceive when you use the async sockets. I have a wrapper for
AsyncTCP that I wrote...uses a "Connect" function, that does this:

Public Sub Connect(ByVal ipAddress As String, ByVal port As
Integer)
' Create socket and connect
If p_sw Is Nothing And p_LogFile <> "" Then
p_sw = New IO.StreamWriter(p_LogFile, True)
p_sw.AutoFlush = True
End If
p_socket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
p_socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.KeepAlive, 0)
Dim endPoint As New IPEndPoint(Net.IPAddress.Parse(ipAddress),
port)
p_socket.Connect(endPoint)

WriteToLog("Connection at " & Time)
RaiseEvent Connected()
End Sub

And then I start listening for data using this:

Public Sub StartReceive()
' set up one receive packet
Dim packet As New SocketPacket(p_socket)
p_socket.BeginReceive(packet.Buffer, 0, packet.Buffer.Length,
SocketFlags.None, New AsyncCallback(AddressOf EndReceive), packet)
p_rcvsWaitingCount += 1
End Sub 'StartReceive

Which will then call your EndReceive method. This way, you shouldn't
end up with any sort of data arriving (at least, not getting processed)
before you're ready.
 
C

Clark

Jerod

I'm reading up on async sockets now. Maybe this is a stupid question, but
why didn't you use p_Socket.BeginConnect instead of Connect? Wouldn't
BeginConnect be better as it wouldn't hang your app if a connection couldn't
be made? I am definitely a newby with this but I'm planning on using
BeginConnect, BeginSend, and BeginReceive to keep my app from hanging under
any conditions. Am I missing something?

Clark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top