I'm running a linux application which calls bind for a port created with socket
int sockAddrSize = sizeof (struct sockaddr_in);
m_Socket = socket(AF_INET, SOCK_STREAM, 0);
if (m_Socket < 0)
{
printf ("CTcpServer::Open : socket failed\n");
return -1;
}
memset ((char *) &m_ServerAddress, 0,sizeof (sockaddr_in));
m_ServerAddress.sin_family = AF_INET;
m_ServerAddress.sin_port = htons (Port);
m_ServerAddress.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind (m_Socket, (struct sockaddr *)&m_ServerAddress, sockAddrSize)<0)
{
printf ("Open: bind failed. errno=Unknown\n");
return -1;
}
if (listen (m_Socket,1)<0)
{
printf ("CTcpServer::Open : listen failed. errno=Unknown\n");
return -1;
}
socklen_t sockAddrSize = sizeof (struct sockaddr_in);
sockaddr_in ClientAddress;
m_ListenSocket = accept(m_Socket, (struct sockaddr *) &ClientAddress, &sockAddrSize);
if (m_ListenSocket < 0)
{
printf ("CTcpClient::AcceptConnection : accept failed. errno=Unknown\n");
return -1;
}
After accept the application is blocked till client establishes a connection.
At this point, if I break the application (Ctrl C) and then tries to run it again, the bind fails: errno=98 Address already in use
It happens also if the application quits in case of segmentation fault (BUG in the application)
It seems that the port is still open.
Can you please tell why ? The application was killed - not gracefully. Same scenario under windows works.
Thank you, Zvika