470,602 Members | 1,677 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,602 developers. It's quick & easy.

SO_REUSEADD successful but bind fails for release version - debug version no problem

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);
}
}
}
Jun 25 '08 #1
1 2317
This issue has been resolved. The issue was with the project settings. The customization was set up Maximum Speed. I have changed it to default and solved the problem. Thanks for the support.
Jun 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by KeithO | last post: by
2 posts views Thread by John Beschler | last post: by
5 posts views Thread by David++ | last post: by
10 posts views Thread by babak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.