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
|
|
|
Mario Charest
|
RE: bind error on TCP/IP connection
|
Mario Charest
01/19/2009 1:43 PM
post20337
|
RE: bind error on TCP/IP connection
Have you tried SO_REUSEPOR?
> -----Original Message-----
> From: Thomas Allen [mailto:community-noreply@qnx.com]
> Sent: January-19-09 1:40 PM
> To: ostech-core_os
> Subject: 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
>
>
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post20335
>
|
|
|
Sean Boudreau(deleted)
|
Re: bind error on TCP/IP connection
|
Sean Boudreau(deleted)
01/19/2009 1:44 PM
post20338
|
Re: bind error on TCP/IP connection
On Mon, Jan 19, 2009 at 01:40:15PM -0500, Thomas Allen wrote:
> 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.
>
Set the SO_REUSEADDR socket option (setsockopt) before the bind.
Regards,
-seanb
|
|
|
Thomas Allen
|
Re: bind error on TCP/IP connection
|
Thomas Allen
01/19/2009 1:51 PM
post20340
|
Re: bind error on TCP/IP connection
Yes, I have set SO_REUSEADDR to 1 just before the bind (see code in original post). I will do I check that it is
actually getting set.
|
|
|
Thomas Allen
|
Re: bind error on TCP/IP connection
|
Thomas Allen
01/19/2009 2:04 PM
post20344
|
Re: bind error on TCP/IP connection
> Yes, I have set SO_REUSEADDR to 1 just before the bind (see code in original
> post). I will do I check that it is actually getting set.
I just validated it by doing a getsockopt on SO_REUSEADDR just before the bind and it is definetly set to 1. Any other
things I could try?
Thanks
Thom
|
|
|
Robert Craig
|
RE: bind error on TCP/IP connection
|
Robert Craig
01/19/2009 2:07 PM
post20345
|
RE: bind error on TCP/IP connection
Hi Thomas:
Is the client application that's using the socket closing it
when it's completed?
(Just as an aside, the Networking Project
http://community.qnx.com/sf/projects/networking would be the best place
for future questions like this.)
Robert.
-----Original Message-----
From: Thomas Allen [mailto:community-noreply@qnx.com]
Sent: Monday, January 19, 2009 2:05 PM
To: ostech-core_os
Subject: Re: bind error on TCP/IP connection
> Yes, I have set SO_REUSEADDR to 1 just before the bind (see code in
> original post). I will do I check that it is actually getting set.
I just validated it by doing a getsockopt on SO_REUSEADDR just before
the bind and it is definetly set to 1. Any other things I could try?
Thanks
Thom
_______________________________________________
OSTech
http://community.qnx.com/sf/go/post20344
|
|
|
Thomas Allen
|
Re: RE: bind error on TCP/IP connection
|
Thomas Allen
01/19/2009 2:19 PM
post20347
|
Re: RE: bind error on TCP/IP connection
Ok I have found my bug. When I was setting the SO_REUSEADDR I was using socket level IPPROTO_TCP instead of SOL_SOCKET.
setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&c_opt_on, sizeof(OPT_TYPE));
Now with the correctly set all works.
Thanks for the help and pointing me at the SO_REUSEADDR.
|
|
|
Mario Charest
|
RE: bind error on TCP/IP connection
|
Mario Charest
01/19/2009 2:15 PM
post20346
|
RE: bind error on TCP/IP connection
> -----Original Message-----
> From: Thomas Allen [mailto:community-noreply@qnx.com]
> Sent: January-19-09 2:05 PM
> To: ostech-core_os
> Subject: Re: bind error on TCP/IP connection
>
> > Yes, I have set SO_REUSEADDR to 1 just before the bind (see code in
> original
> > post). I will do I check that it is actually getting set.
>
>
> I just validated it by doing a getsockopt on SO_REUSEADDR just before
> the bind and it is definetly set to 1. Any other things I could try?
>
Did you see my post about SO_REUSEPORT?
> Thanks
> Thom
>
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post20344
>
|
|
|
|