Question about return 0 in Socket::EndSend in C#

L

linuxfedora

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
 
P

Peter Duniho

[...]
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
 
L

linuxfedora

[...]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top