PC Review


Reply
Thread Tools Rate Thread

Asynchronous Socket Events

 
 
Borco
Guest
Posts: n/a
 
      8th Aug 2005
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

 
Reply With Quote
 
 
 
 
Jerod Venema
Guest
Posts: n/a
 
      9th Aug 2005
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.

 
Reply With Quote
 
Clark
Guest
Posts: n/a
 
      9th Aug 2005
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



"Jerod Venema" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asynchronous Socket ckkwan@my-deja.com Microsoft Dot NET Framework 1 6th Feb 2008 06:12 AM
Asynchronous Client Socket ASP.NET Adam Microsoft Dot NET 1 14th May 2004 09:45 PM
Asynchronous socket problem Matthew Groch Microsoft C# .NET 3 11th Mar 2004 12:36 AM
Asynchronous Socket Problem =?Utf-8?B?Um9iZXJ0?= Microsoft C# .NET 2 6th Mar 2004 03:06 PM
Asynchronous socket programming Rob Microsoft Dot NET 0 16th Jan 2004 07:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.