Ignoring Error Code 10054 for UDP

Started by
2 comments, last by hplus0603 19 years, 7 months ago
How everybody, If you send a packet over UDP (using Winsock) to an IP that exists but to a port that is not open on that IP, and then do a recvfrom on the socket I used to send the packet, it will return SOCKET_ERROR and WSAGetLastError will return 10054 (Connection reset). Is there any way to prevent this? I have two clients, connection to each other at the same time. They both start sending each others messages at the same time, and look (using recvfrom), if the other side already responsed. But one clinet will start first and send packets befor the other side has opened the port (causing the 10052 Error). I need to do this, because I am implementing a "both-side-NAT" methode, described on this page: http://www.mindcontrol.org/~hplus/nat-punch.html So my question: How can I prevent my UDP socket to close when I send a message to a port that is not open (and afterwards call recvfrom)? Thanks!!!
Advertisement
your UDP socket will not "close" since it is never really "connected". you might know this already, but...

you don't really 'connect' with UDP sockets. if u call connect() with a UDP socket, it will simply associate a remote host with the socket (nothing actually gets sent over the network), such that when u call send() the operating system will know which host/port to send to.

so, to answer your question, simply ignore the error message until the first time you have successfully sent a packet. the socket will not 'close' if the destination port is not open (you can keep using it to send packets).
Some solutions are just to easy ...
I spend almost a week finding the cause of the Error and trying to fix it.
All I needed to do, was ignoring the error code ;)

Thank you, jorgander!!!
Note that the documentation claims that a socket that has returned an error is "unuseable" after that. (You can look it up on MSDN). Thus, to be really safe, what I do when I get that error is to close the socket and re-open it. For UDP, that's perfectly safe, because there's no way that any other application will tell the difference between the two sockets -- although there is a very small window during which the other end may get a bounced packet because the socket was temporarily down.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement