Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Asynchronous Socket Events

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Borco - 08 Aug 2005 22:05 GMT
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
Jerod  Venema - 09 Aug 2005 18:11 GMT
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.
Clark - 09 Aug 2005 21:42 GMT
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

> 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
[quoted text clipped - 33 lines]
> end up with any sort of data arriving (at least, not getting processed)
> before you're ready.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.