473,378 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

confused by socket.BeginReceive

I dont get the point of socket.BeginReceive and socket.EndReceive. As I
understand it, BeginReceive will start a 2nd thread, call the
ReceiveCallback delegate in the 2nd thread, then block until the
socket.EndReceive method called in the 2nd thread receives some data
from the socket.

If the 1st thread will block until the 2nd thread receives some data
from the socket, what is the point of starting up the 2nd thread? I am
aware I can create my own thread and have the two threads behave the
way I want, but what am I missing about BeginReceive and EndReceive?

Is BR ... ER intended for situations when a lot of data is to be
received and EndReceive will return after the first block of data is
returned?

A 2nd related question ... How does the first thread kill the receive
in progress in the 2nd thread? Wait for the current chunk of data to
be received by EndReceive and use a sync object of some sort to signal
it is time for the 2nd thread to end?

3rd question ... the documentation says SetSocketOption -
ReceiveTimeout does not apply when Socket.BeginReceive is used. How do
I implement timeout when using the BeginReceive ... EndReceive pair?

thanks,

-Steve

Nov 17 '05 #1
6 13112
Steve,

I think you are mistaken. When you call BeginReceive, it returns
immediately, and you do not have to wait on the second thread. When the
callback is called, it will be called on the second thread. How you
interact with the first thread is up to you. If you want to have the first
thread wait for the operation to complete, then you can call EndReceive on
the first thread.

As for applying a timeout, you can use an instance of the Timer class in
the System.Timer namespace to do this. After you call BeginReceive, you
start your timer, setting the interval property to the length of time you
want to wait. You also need a flag to indicate if the operation timed out
or not. In the event handler for the callback on the async operation, you
would check the flag. If the flag is set, then do not continue your
processing (since the operation timed out, and there is no need). If the
flag is not set, then disable the timer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Steve Richter" <St************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I dont get the point of socket.BeginReceive and socket.EndReceive. As I
understand it, BeginReceive will start a 2nd thread, call the
ReceiveCallback delegate in the 2nd thread, then block until the
socket.EndReceive method called in the 2nd thread receives some data
from the socket.

If the 1st thread will block until the 2nd thread receives some data
from the socket, what is the point of starting up the 2nd thread? I am
aware I can create my own thread and have the two threads behave the
way I want, but what am I missing about BeginReceive and EndReceive?

Is BR ... ER intended for situations when a lot of data is to be
received and EndReceive will return after the first block of data is
returned?

A 2nd related question ... How does the first thread kill the receive
in progress in the 2nd thread? Wait for the current chunk of data to
be received by EndReceive and use a sync object of some sort to signal
it is time for the 2nd thread to end?

3rd question ... the documentation says SetSocketOption -
ReceiveTimeout does not apply when Socket.BeginReceive is used. How do
I implement timeout when using the BeginReceive ... EndReceive pair?

thanks,

-Steve

Nov 17 '05 #2


Nicholas Paldino [.NET/C# MVP] wrote:
Steve,

I think you are mistaken. When you call BeginReceive, it returns
immediately, and you do not have to wait on the second thread. When the
callback is called, it will be called on the second thread. How you
interact with the first thread is up to you. If you want to have the first
thread wait for the operation to complete, then you can call EndReceive on
the first thread.
ok, thanks Nicholas, I just misread the documentation:

"...Your callback method should implement the EndReceive method. When
your application calls BeginReceive, the system will use a separate
thread to execute the specified callback method, and will block on
EndReceive until the Socket reads data or throws an exception. ..."
As for applying a timeout, you can use an instance of the Timer class in
the System.Timer namespace to do this. After you call BeginReceive, you
start your timer, setting the interval property to the length of time you
want to wait. You also need a flag to indicate if the operation timed out
or not. In the event handler for the callback on the async operation, you
would check the flag. If the flag is set, then do not continue your
processing (since the operation timed out, and there is no need). If the
flag is not set, then disable the timer.


what if the 2nd thread stays blocked on socket.EndReceive and does not
return because there is no data to receive? The 2nd thread would
never get to the code that tests the timeout flag.

Can I shutdown the socket in the first thread, which would cause the
socket.EndReceive to return?

thanks,

-Steve

Nov 17 '05 #3
Steve,

Basically, in the callback, you call EndReceive to get the results of
the operation. However, your callback will not be executed until the
operation is complete. When your callback is called, EndReceive will return
immediately with the result.

If you call EndReceive before your callback is called, then the thread
you call it (the initial calling thread, perhaps) on will block until the
operation is complete.

If you shut down the socket in the middle of this operation, it should
cause the operation to complete (even because of an abort), and then your
callback will be fired. However, depending on how abrupt the abort call is,
you might have to wrap the call to EndReceive in a try/catch block to catch
any exceptions that arise.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Steve Richter" <St************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...


Nicholas Paldino [.NET/C# MVP] wrote:
Steve,

I think you are mistaken. When you call BeginReceive, it returns
immediately, and you do not have to wait on the second thread. When the
callback is called, it will be called on the second thread. How you
interact with the first thread is up to you. If you want to have the
first
thread wait for the operation to complete, then you can call EndReceive
on
the first thread.


ok, thanks Nicholas, I just misread the documentation:

"...Your callback method should implement the EndReceive method. When
your application calls BeginReceive, the system will use a separate
thread to execute the specified callback method, and will block on
EndReceive until the Socket reads data or throws an exception. ..."
As for applying a timeout, you can use an instance of the Timer class
in
the System.Timer namespace to do this. After you call BeginReceive, you
start your timer, setting the interval property to the length of time you
want to wait. You also need a flag to indicate if the operation timed
out
or not. In the event handler for the callback on the async operation,
you
would check the flag. If the flag is set, then do not continue your
processing (since the operation timed out, and there is no need). If the
flag is not set, then disable the timer.


what if the 2nd thread stays blocked on socket.EndReceive and does not
return because there is no data to receive? The 2nd thread would
never get to the code that tests the timeout flag.

Can I shutdown the socket in the first thread, which would cause the
socket.EndReceive to return?

thanks,

-Steve

Nov 17 '05 #4


Nicholas Paldino [.NET/C# MVP] wrote:
If you call EndReceive before your callback is called, then the thread
you call it (the initial calling thread, perhaps) on will block until the
operation is complete.
Is this a guarantee? I couldn't find that anywhere in the docs when I
last did async IO.
If you shut down the socket in the middle of this operation, it should
cause the operation to complete (even because of an abort), and then your
I have never seen Thread.Abort actually abort a call on a blocking
socket. I have seen the Close of a socket generate an exception in
EndReceive though.
callback will be fired. However, depending on how abrupt the abort call is,
you might have to wrap the call to EndReceive in a try/catch block to catch
any exceptions that arise.


I don't see it as special. You have to be aware, that the socket
EndReceive is associated with might be invalid when you get invoked (it
was Close()'ed elsewhere) though.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #5


Steve Richter wrote:
I dont get the point of socket.BeginReceive and socket.EndReceive. As I
understand it, BeginReceive will start a 2nd thread, call the


A piece of advice: unless you *really* *really* need non-blocking IO,
don't use it. The potential synchroniazation problems are *huge*.

If you do decide to go for it for some reason, like performance, write a
test that validates that you actually realize your reason (I've seen a
few places where async-IO was a lot slower than blocking).

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #6
Helge Jensen <he**********@slog.dk> wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
If you call EndReceive before your callback is called, then the thread
you call it (the initial calling thread, perhaps) on will block until the
operation is complete.


Is this a guarantee? I couldn't find that anywhere in the docs when I
last did async IO.


It depends exactly what you mean by "operation is complete", but from
the docs:

<quote>
The EndReceive method will block until data is available. If you are
using a connectionless protocol, EndReceive will read the first
enqueued datagram available in the incoming network buffer. If you are
using a connection-oriented protocol, the EndReceive method will read
as much data as is available up to the number of bytes you specified in
the size parameter of the BeginReceive method
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : ...
4
by: Brian Rice | last post by:
I have a socket application that is sending and receiving packets asynchronously. It works great except, when I receive packets that are larger than my receive buffer which then generate several...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
1
by: Marty | last post by:
Hi, I have a socket that always seek for incoming data. Between Point A and Point B, the socket (mySocket is closed and assigned to nothing in another part of my program (happen when a...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
2
by: O.B. | last post by:
In the following code snippet, the thread successfully makes it to the line where it waits for data to be received. Then the client closes the connection. The thread wakes up and returns from the...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.