I've been messing about with my socket class, and i realised something. If I bind and listen on a port (23 in this case), then close the socket, and try to rebind and relisten, it doesn't work - i get error code #98 from bind(), which isn't listed as a valid return error code for bind().
I close the socket properly, as far as i'm aware:
void CBaseSocket::Disconnect()
{
if(m_sock != INVALID_SOCKET)
{
#ifdef WIN32
shutdown(m_sock,SD_SEND);
#else
shutdown(m_sock,SHUT_WR);
#endif
closesocket(m_sock);
m_sock = INVALID_SOCKET;
}
}
I only get this problem under linux, it workd fine under windows (2000). I'm running Red Hat 9.
It doesn't seem to matter how long i wait before i rebind, i have to exit the application and wait for a while (about 2 - 5 mins), then it works fine - I assume the OS is doing random housekeeping and it cleans up my socket for me.
Heres the code i use to listen:
bool CBaseSocket::Listen(int nPort)
{
sockaddr_in sockAddr;
// Create Socket //
m_sock = socket(PF_INET,SOCK_STREAM,0);
if(m_sock == INVALID_SOCKET)
{
m_strError = "Fatal Error: socket() Failed! " + WSAErrorAsString(Error);
return false;
}
// Setup Address //
memset(&sockAddr,0,sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sockAddr.sin_port = htons(nPort);
// Bind socket to To Address //
if(bind(m_sock,(sockaddr*)&sockAddr,sizeof(sockaddr_in)) == SOCKET_ERROR)
{
m_strError = "Fatal Error: bind() Failed! " + WSAErrorAsString(Error);
closesocket(m_sock);
m_sock = INVALID_SOCKET;
return false;
}
// Listen on Socket //
if(listen(m_sock,5) == SOCKET_ERROR)
{
m_strError = "Fatal Error: listen() Failed! " + WSAErrorAsString(Error);
closesocket(m_sock);
m_sock = INVALID_SOCKET;
return false;
}
return true;
}
Anyone had this problem? Is there something i'm missing that windows is more tolerant about (e.g. unbind()?) I checked the HawkNL source, and it listens and closes sockets the same way as me.
Cheers,
Steve
[edited by - Evil Steve on February 23, 2004 10:01:51 PM]