Sign in to follow this  

SO_REUSEADD successful but bind fails.

This topic is 3456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been facing a problem with UDP socket programming. I have to reuse port number for two different IP address. I am actually using SO_REUSEADD. I have no problem with debug version but with release version the bind is failing for 2nd IP address with the error code 10048. It's the error code for WSAEADDRINUSE. I am trying to with WinXP machine using Visual C++ 6.0. The application which we are using needs to connect to Primary and Backup server. The UDP connection should be established with both Primary and Backup server so that if the Primary server goes down, the client can still communicate with backup server and retrieves all information. I can’t use different port for both Primary and Backup serer as we are allowing multiple clients on a PC. Also we are not handling this on the server side since it’s a redundant system. Primary Server (Listening Port : 6000) Backup Server (Listen on 6000) Client1 (Listening Port 6003: Local Port: 6002 so that the server knows it should send it to 6003, just add one and send the response back) Client2 (Listening Port 6005: Local Port: 6002 so that the server knows it should send it to 6003, just add one and send the response back) I have a mechanism to select both listening and local port on the client. I bind with the specific portnumber and close the socket after identifying the port numbers. The setsockopt is successful for SO_REUSEADDR but bind fails for backup server. It’s strange that this issue never happens in debug version but release version fails to bind for the backup server. Here is the code to handle this scenario. WSADATA WsaData; int err = WSAStartup(MAKEWORD(1,1), &WsaData); if ( err != 0 ) { return (kOpenInvalidDev); } if ((mSocket = socket(PF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) { return (kOpenDevError); } int optval; if(!mIsServer) { if (setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, (char *) &optval, sizeof (optval)) == SOCKET_ERROR) { close(); // close. return (kOpenDevError); } } if (setsockopt(mSocket, SOL_SOCKET, SO_BROADCAST, (char *) &optval, sizeof (optval)) == SOCKET_ERROR) { close(); // close. return (kOpenDevError); } else if (mLocalAddr.sin_port != 0) { if (bind(mSocket, (struct sockaddr*)&mLocalAddr, sizeof(mLocalAddr)) == SOCKET_ERROR) { return (kOpenDevBusy); } } }

Share this post


Link to post
Share on other sites
Optimization should not change the behavior of your program. Even with a compiler as old and outdated as VC 6.0, that is usually not the case. It's more likely there is a bug in your code that is not provoked in debug mode.

However, UDP is connectionless. If you want to "connect" to two different machines, just open a single UDP socket on your end, and use sendto() to send messages to the two different machines. There is no need for two sockets, and in fact, it may hurt performance and reliability.

Share this post


Link to post
Share on other sites

This topic is 3456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this