Thomas Allen
|
bind error on TCP/IP connection
|
Thomas Allen
01/19/2009 1:40 PM
post20335
|
bind error on TCP/IP connection
Hi,
I have an application where I am opening a TCP/IP socket, binding to a port then listening/accepting a connection from a
client.
The problem I have is I want to be able to close the socket and later on create a new socket, bind to the same port and
listen/accept a connection.
What is happening is it works fine the first time. However when I close the socket and later create a new socket, and
then try to bind it to the same port I get and EINVAL (22) error. This docs say this means that the socket is already
bound to an address.
Seems that I am not closing the socket correctly, or should be doing an 'unbind' of some sort???!!!
I am calling close(sock) and have also tried doing a shutdown(sock,2) first.
Opening the socket I am doing the following (and this works the first time)
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&c_opt_on, sizeof(OPT_TYPE));
serverservice.sin_family = AF_INET;
serverservice.sin_port = ntohs(nPort);
serverservice.sin_addr.s_addr = INADDR_ANY;
setsockopt(sock, IPPROTO_TCP, SO_REUSEADDR, (char *)&c_opt_on, sizeof(OPT_TYPE));
bind(sock, (sockaddr *)&m_serverservice, sizeof(m_serverservice));
The bind fails the second time and sets errno to 22.
Any ideas would be appreciated.
Thanks
Thom
|
|
|