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 / CLR / August 2004

Tip: Looking for answers? Try searching our database.

Socket.BeginReceive without callback?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lucvdv - 17 Aug 2004 10:04 GMT
Something isn't clear (to me) in the documentation.
In the "Remarks" section for the Socket.BeginReceive method, it says:

   You can create a callback method that implements the AsyncCallback
   delegate and pass its name to the BeginReceive method.

Does this "can" imply that the use of a callback is optional?

If no callback is specified, can you also omit the State parameter from the
BeginReceive call? (just pass Nothing or NULL for the last two parameters)

Basically, I would want use a worker thread that initializes a read on a
socket, and then uses WaitHandle.WaitAny on an array of handles, one of
which corresponds to the read operation.
Not all handles will correspond to sockets, each of them just signals some
action the worker thread has to do.

It seems to me that with a single WaitAny, my code would become simpler and
I'd save the extra thread the runtime uses for the callback.
Lucvdv - 17 Aug 2004 13:21 GMT
> Something isn't clear (to me) in the documentation.
> In the "Remarks" section for the Socket.BeginReceive method, it says:
[quoted text clipped - 3 lines]
>
> Does this "can" imply that the use of a callback is optional?

It compiles and runs, but [my code?] doesn't work.

Related question: also in the documentation,

   The Connected property gets the connection state of the Socket as of
   the last I/O operation. When it returns false, the Socket was either
   never connected, or is no longer connected.

Could it be that this is true only for connection state changes made
locally, or that it goes wrong after BeginRead has been called without a
callback?

If the connection (TCP, stream) is closed by the other side, Connected
remains True indefinitely.  Read attempts keep returning immediately with 0
bytes read, but without any indication of an error and without adjusting
the state of the Connected property.

I'm including a VB code snippet, running in a worker thread: after the
connection has been closed by the other side, it just keeps printing
"Empty" until the thread is killed.

ar = m_Socket.BeginReceive(RecvData, 0, RecvData.GetUpperBound(0), _
    SocketFlags.None, Nothing, Nothing)
WaitHandles = New WaitHandle() {ar.AsyncWaitHandle, [...]}

Do
   Select Case WaitHandle.WaitAny(WaitHandles, 100, False)
       Case 0  ' Data received
           If m_Socket.Connected Then
               Dim Bytes As Integer = m_Socket.EndReceive(ar)
               If Bytes > 0 Then
                   Dim s As String = Encoding.ASCII.GetString(RecvData, _
                                     0, Bytes)
                   Debug.WriteLine(Bytes & ";" & s)
                   [...]
               Else
                   Debug.WriteLine("Empty")
               End If
               [restart BeginReceive & re-initialize WaitHandles array]
           End If
       [Other cases]
   End Select
Loop While m_Socket.Connected
Eugene Mayevski - 17 Aug 2004 17:56 GMT
Hello!
You wrote  on Tue, 17 Aug 2004 11:04:10 +0200:

L> Basically, I would want use a worker thread that initializes a read on a
L> socket, and then uses WaitHandle.WaitAny on an array of handles, one of
L> which corresponds to the read operation.

What would you use as a WaitHandle? You can use Select method on a socket
and this will work (tried just yesterday).

With best regards,
Eugene Mayevski
Lucvdv - 19 Aug 2004 12:05 GMT
> Hello!
> You wrote  on Tue, 17 Aug 2004 11:04:10 +0200:
[quoted text clipped - 4 lines]
>
> What would you use as a WaitHandle?

The AsyncWaitHandle member of the IASyncResult returned by BeginReceive.

WaitAny is a static/shared member of WaitHandle in case that's what you
meant, you don't need a separate instance.

This is aproximately how my VB code is structured:

Try
   WaitHandles = New WaitHandle() {WaitHandle1, WaitHandle2}
   Do
       Select Case WaitHandle.WaitAny(WaitHandles, msecTimeOut)
           Case 0:
               ' WaitHandle1 got signaled
           Case 1:
               ' WaitHandle2 got signaled
           Case WaitTimeout:
               ' Some background processing, if necessary
       End Select
   Loop
Catch ex as ThreadAbortException
   ' We're being asked to terminate
   ' The only way the endless loop ever gets broken
End Try

> You can use Select method on a socket
> and this will work (tried just yesterday).

Only one of the handles I'm waiting for belongs to a socket, another
indicates a certain action has been requested by the UI thread, etc.

Rate this thread:







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.