I have written a program that is using Async Socket,

And the program will send a lot of packet to the socket, and i used
BeginSend and EndSend for sending the data.
But the werid thing is that, sometime, the EndSend return 0, so it
implies that 0 byte of data have been sent, so the my program will try
to do the BeginSend again and try to send the data again, but the
result is that the other side will receive 2 same packets. So what is
wrong with it?

Thanks

Re: Question about return 0 in Socket::EndSend in C# by Peter

Peter
Tue Jul 10 03:37:43 CDT 2007

On Mon, 09 Jul 2007 23:40:30 -0700, <linuxfedora@yahoo.com.hk> wrote:

> [...]
> But the werid thing is that, sometime, the EndSend return 0, so it
> implies that 0 byte of data have been sent, so the my program will try
> to do the BeginSend again and try to send the data again, but the
> result is that the other side will receive 2 same packets. So what is
> wrong with it?

You should not get 0 bytes as a return value for EndSend() unless you
actually called BeginSend() with 0 bytes. If you're using a
connection-oriented socket (eg TCP), no data should be sent at all in this
case, though you will get the successful completion with 0 bytes as the
return value. With a connectionless socket (eg UDP), a 0 length datagram
will be sent.

You don't say what kind of socket you're using. Usually one uses SendTo()
instead of Send() with UDP, but only UDP supports the idea of "packets".
So your post is self-contradictory, making it difficult to know exactly
what it is you're doing.

If you are actually using UDP, then keep in mind that having the same
datagram received twice is a known, correct possible occurrence. With
UDP, you can get a given datagram once, not at all, or multiple times.

If you cannot figure out how it is that you're calling BeginSend() with 0
bytes, you might post the code here to see if someone can help you
identify the problem. Post only a concise-but-complete example of the
code that reliably reproduces the problem. Don't include a bunch of stuff
that has nothing to do with the bug you're dealing with.

Pete

Re: Question about return 0 in Socket::EndSend in C# by linuxfedora

linuxfedora
Tue Jul 10 20:14:15 CDT 2007

On 7 10 , 4 37 , "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Mon, 09 Jul 2007 23:40:30 -0700, <linuxfed...@yahoo.com.hk> wrote:
> > [...]
> > But the werid thing is that, sometime, the EndSend return 0, so it
> > implies that 0 byte of data have been sent, so the my program will try
> > to do the BeginSend again and try to send the data again, but the
> > result is that the other side will receive 2 same packets. So what is
> > wrong with it?
>
> You should not get 0 bytes as a return value for EndSend() unless you
> actually called BeginSend() with 0 bytes. If you're using a
> connection-oriented socket (eg TCP), no data should be sent at all in this
> case, though you will get the successful completion with 0 bytes as the
> return value. With a connectionless socket (eg UDP), a 0 length datagram
> will be sent.
>
> You don't say what kind of socket you're using. Usually one uses SendTo()
> instead of Send() with UDP, but only UDP supports the idea of "packets".
> So your post is self-contradictory, making it difficult to know exactly
> what it is you're doing.
>
> If you are actually using UDP, then keep in mind that having the same
> datagram received twice is a known, correct possible occurrence. With
> UDP, you can get a given datagram once, not at all, or multiple times.
>
> If you cannot figure out how it is that you're calling BeginSend() with 0
> bytes, you might post the code here to see if someone can help you
> identify the problem. Post only a concise-but-complete example of the
> code that reliably reproduces the problem. Don't include a bunch of stuff
> that has nothing to do with the bug you're dealing with.
>
> Pete

I found that it should be the program logic bug only. Thanks.