添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
473,537 Members | 2,805 Online

Async Socket , BeginReceive question .

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 :

mySocket.BeginReceive(param1,param2,param3.....);
mySocket.BeginReceive(param1,param2,param3......);
mySocket.BeginReceive(param1,param2,param3......);

myThread.wait()
and If the Client Socket send my 3 async Message, what will happened ?

BeginReceive1, that receive message 1 ||| BeginReceive2, that receive message 2 ||| BeginReceive3, that receive message 3

BeginReceive1, that receive Message 1
BeginReceive2, that receive Message 2
BeginReceive3, that receive Message 3

The 3 SendMessage is listen at the same time from server ?

The server listen the Message1 with BeginReceive1,
server listen the Message 2 with BeginReceive2 when BeginReceive1 has finish to listen the Message 1
server listen the Message 3 with BeginReceive3 when BeginReceive 2 has finish to listen the Message 2 .

Thank all ,
Nov 15 '05 # 1
2 9217
"dream machine" <[email protected]> wrote in message news:<O3**************@tk2msftngp13.phx.gbl>...
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 :

<code>
mySocket.BeginReceive(param1,param2,param3.....);
mySocket.BeginReceive(param1,param2,param3......);
mySocket.BeginReceive(param1,param2,param3......);

myThread.wait()
<code>

and If the Client Socket send my 3 async Message, what will happened ?

This is somewhat of a trick question. Your last scenario is
correct, the three BeginReceive() methods will "stack up", and process
incoming data serially - one after the other. The first BeginReceive()
method will process whatever data is available on the socket first,
then the second BeginReceive() method will process any remaining data
(or new data on the socket), and finally, the third one will process.

The problem is that there is no control as to which BeginReceive()
method will process what data. TCP is not a message-oriented protocol,
it is stream-oriented. The three messages that are sent are not
guaranteed to be taken out of the socket buffer in the same format by
each of the three BeginReceive() methods that they were sent. Its
quite possible that the first BeginReceive() method will grab all of
message1, plus part of message2, then the second BeginReceive() method
grabs the end of message2 plus all of message3, leaving the last
BeginReceive() method never to complete, waiting for data. This would
make a nightmare situation trying to put all of the data back
together.

It is better to either mark your messages with boundaries or send
the message size before the actual message. That way the other end
knows when each message starts and stops, and can process the received
data properly. Hope this helps out some.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Nov 15 '05 # 3

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

Similar topics

2407
by: Ben | last post by: I've written a fair amount of sockets code using the Winsock2 API, but I am having some trouble converting to the .Net Sockets API, specifically asynchronous sockets. What I have is a form that is both a client and a server. When the form starts I create a listening socket and call Socket.BeginAccept(). When a client connects my accept function...
13684
by: Mark Smith | last post by: Dear Readers, I am in the process of implementing a non-blocking ASYNC TCP client using the .Net socket class. I am a little confused by the MSDN example. I have pasted the code here. private static void ReceiveCallback( IAsyncResult ar ) { try { // Retrieve the state object and the client socket // from the asynchronous state object.
11650
by: mscirri | last post by: The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of 1024 with all values set to Null arrive. Other than examine a block of 1024 to see if the entire block is null, is there any other way to determine...
8037
by: Eric | last post by: I need to send an object between nodes on a network. Each node currently communicates fine with sending strings around but I can't figure out how to deserialize objects using the same basic infrastructure. I'm currently using MS's basic server example for my code: public static void AcceptCallback(IAsyncResult ar) // Signal the main...
1286
by: Terry Olsen | last post by: I'm trying to use Async Socket calls to create a tcp server. It goes into the listening correctly, but when I connect, it crashes with the following exception: An unhandled exception of type 'System.InvalidOperationException' occurred in system.dll Additional information: AcceptCallback There is no source code available for the current...
11001
by: Dinsdale | last post by: I am trying to write a Tcp "Server" that opens a class that wraps a tcp socket when a new connection is made (Listener.AcceptSocket()). Everything is going swimmingly except when I try to close the socket during a read and I get the following error: <error_msg> An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in...
2522
by: rockdale | last post by: Hi, all Did not find a .Net Communication forum to post. I am soing a client/server message system and using C#.net socket and Async mode. Pretty much as described in article http://www.developerfusion.co.uk/show/3997/3/
4482
Uniquely Identifying Multiple/Concurrent Async Tasks
by: Frankie | last post by: It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under consideration here is that it is supplied to the AsyncOperationManager.CreateOperation(userSuppliedState) method... with userSuppliedState being, more...
8034
by: ColoradoGeiger | last post by: I have a fairly standard server application that I am using asynchronous socket calls to make connections, send and receive data, and all of that jazz. No blocking methods was my goal in writing this and it's gone very well so far. But coming from the blocking world, I want to have some timeouts on my receive routines and I after reading
7534
by: Oralloy | last post by: Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
7687
by: jinu1996 | last post by: In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
7277
by: Hystou | last post by: Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
7644
by: tracyyun | last post by: Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
5826
by: agi2029 | last post by: Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
3347
by: TSSRALBI | last post by: Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
3343
by: adsilva | last post by: A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1759
by: 6302768590 | last post by: Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
582
by: bsmnconsultancy | last post by: In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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