SetSockOpt with SO_REUSEADDR parameter
Hi,
I write a server application to send message to multiple clients connected
to it. So I create multiple UDP sockets with the same port to send data. At
first, I get the error code " 10048 " and find a solution from MSDN. It tells
me to use SetSockOpt with SO_REUSEADDR. I rewrite the function that creates a
UDP socket:
=======================================
// CUdpSendSocket is derived from CAsyncSocket
void CUdpSendSocket::CreateSocket(UINT nPort)
{
TCHAR szError[256];
BOOL bRet = Create(nPort, SOCK_DGRAM, FD_WRITE);
if (bRet != TRUE) {
wsprintf(szError, "Send Socket Create() failed: %d", GetLastError());
AfxMessageBox(szError);
return;
}
m_nPortNum = nPort;
int flag = -1;
if (!SetSockOpt(SO_REUSEADDR ,&flag, sizeof(int))) {
wsprintf(szError, "SetSockOpt failed to set SO_REUSEADDR:% d",
GetLastError());
AfxMessageBox (szError);
}
}
=======================================
When a client is connecting to server, server will accept the connection and
create a thread that creates a CUdpSendSocket socket to send data.
When the second client is connecting to server, I still get the error code
"10048".
Does I use SetSockOpt with wrong way?
What shall I do?