You are on page 1of 3

Re: C# socket programming

Re: C# socket programming


Source: http://www.techarchive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/200506/msg02186.html

From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> Date: Mon, 13 Jun 2005 14:51:37 0400 Kuba, I think you are mistaken about how the async Send and Receive messages work. If you call BeginReceive, passing a length of 4, the callback (or waiting on AsyncWaitHandle) will not return until four bytes have been read, or some other error occurs. If you did this asynchronously, you would have one callback for reading the four bytes. At the end of that callback, you would issue the request for the bytes in the message (the length of which you now know), and pass it a callback which would process the message once those bytes are read. If you only want to read chunks of the message at a time, then you would have to reissue the call to BeginReceive, and store the bytes that are read somewhere, until you have enough to process. Hope this helps.

Nicholas Paldino [.NET/C# MVP] mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx "kuba bogaczewicz" <0x6A62@xxxxxxxxx> wrote in message news:1118670251.046464.309650@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx > Hello all, > for my school project I have to write a small peer2peer application > using Sockets, and I've chosen C# for the task. > I've been doing some research on the topic, and I would really > appreciate if someone checked if I got all things right. > > I am going to use asynchronous connections, and here comes some of my > doubts: > > I want to send each message in a scheme: 4 bytes with the size, then > actual message. According to MSDN I got with VS2003 > BeginSend()/EndSend() and BeginReceive/EndReceive will block (in it's Re: C# socket programming 1

Re: C# socket programming > own thread) until the desired amount of data is sent or received. But > I've seen in 'C# Network Programming', that the proper method of > sending would be in a loop checking how much data had been sent, and > resending what is left. Can I trust MSDN on this one it will be > rather painful to do loopsending with asynchronous methods, while > having > receive 4 bytes; > receive actual message; > will be quite easy. > > > does BeginSend() puts byte[] with data in some other system queue? > This question is rather essencial to me, because on one Socket > connection I will be sending possibly a couple of files, and also the > same Socket will have to be able to send 'search messages'. Each file > will be sent one part after another, no flooding socket. But durign > that sending user might want to send a 'search' message, and I will > have to do BeginSend() for this one. Can this somehow mess with byte > order sent through the socket? > if on the other side on the connetion application is constantly doing > BeginRecive; > EndReceive; > can this be making problems with buffors on any side? > > I'd greatly appreciate quick responses, > thank you in advance > > Kuba Bogaczewicz >

FollowUps: Re: C# socket programming From: Helge Jensen Re: C# socket programming From: Sami Vaaraniemi Re: C# socket programming From: kuba bogaczewicz References: C# socket programming From: kuba bogaczewicz Prev by Date: DataGridView Internal Error Beta 2.0 Next by Date: RE: Compare SortedList Previous by thread: C# socket programming Next by thread: Re: C# socket programming Index(es): Re: C# socket programming 2

Re: C# socket programming Date Thread

Re: C# socket programming

You might also like