Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
BroadcastCommunity.qnx.com will be offline from May 31 6:00pm until June 2 12:00AM for upcoming system upgrades. For more information please go to https://community.qnx.com/sf/discussion/do/listPosts/projects.bazaar/discussion.bazaar.topc28418
Forum Topic - bind error on TCP/IP connection: Page 1 of 8 (8 Items)
   
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