.NETGURU
Socket.BeginReceive without callback?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.clr.

Post a new message to this list...

Lucvdv
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.

Reply to this message...
 
    
Lucvdv
On Tue, 17 Aug 2004 11:04:10 +0200, Lucvdv <Click here to reveal e-mail address> wrote:

[Original message clipped]

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

Reply to this message...
 
    
Eugene Mayevski
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

Reply to this message...
 
    
Lucvdv
On Tue, 17 Aug 2004 19:56:54 +0300, "Eugene Mayevski" <Click here to reveal e-mail address>
wrote:

[Original message clipped]

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

[Original message clipped]

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.

Reply to this message...
 
 
System.AsyncCallback
System.Diagnostics.Debug
System.Net.Sockets.Socket
System.Net.Sockets.SocketFlags
System.Text.Encoding
System.Threading.ThreadAbortException
System.Threading.WaitHandle




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification