Click here to Skip to main content
15,886,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am still not sure if I am doing this correctly, especially in correct sequence
and with all correct options.

My understanding is that "socket" can be option to let "bind " to reuse port or address.
It does not.
When I try to access the same address second time , without closing the socket, I expect these options to let "bind" to reuse the address.( per Linux man)

But "bind" will always fail with "address already in use " error.

That is not what I expect when the socket is optioned to reuse the address.

Please understand that I am specifically asking why "reuse address" does not work as expected.

Am I missing some option?

I have no other issues to discuss.

What I have tried:

Here is the code sequence I am using 


FileDescriptor_socket = socket(AF_INET, SOCK_STREAM, 0); //BTPROTO_RFCOMM);
setsockopt(FileDescriptor_socket, SOL_SOCKET,(SO_REUSEPORT | SO_REUSEADDR | SO_DEBUG), (char*) &option,sizeof(option));
bind(...
Posted
Updated 26-Feb-20 10:55am
Comments
Richard MacCutchan 26-Feb-20 3:47am    
Type the command "netstat -a" in a cmd window and you can see whether your port is still in listening mode. If you do not close your socket properly then it will stay in use until it times out. TCP timeout used to be 15 minutes, not sure if it is still the same.

There's bunches of examples out there for you to google on, but basically the sequence is
C
int sfd = socket(/*...*/);
setcokopts( /*...*/);
bind( /*...*/);  /* At this point you have an open socet waiting for connections.*/
                 /* You can then listen on the socket for a new connection e.g. */

If you carefully read the post , the above sequence WORKS  just fine, that is NOT the issue. I do not see how setsockopt and its options allows for address reuse. That IS the  issue. 

I realize that I am NOT properly closing the the socket when I exit() the project during development.  
The issue is when I rerun the project / application again the address remains in use. Irregardless of reason it remains in use - the setsockopt specifically have option to reuse the address - just for bind to reuse it. 
And it does not. bind returns an  error  the "address already in use"  and any further processing is useless. 

Yes, I can try closing the socket, but that would defeat  "address reuse " option.

    
listen(sfd, backlog); /* backlog is the queue length of connections waiting */

for( ;; ) {
   int connection = accept(sfd, /* ... *);
   /* work with connection */
   close(connection);
}

You don't need to call bind() more than once per open socket (sfd). In general, you will fork() or start a new thread to handle the new connection, so you can process the backlog quickly.
 
Share this answer
 
v2
Comments
k5054 25-Feb-20 21:41pm    
Please don't edit other peoples replies with your own comments. It makes it hard to comment and it borders on rudeness.

Please take a look atthis[^] code. This works as one would expect:
07:19:42 $ ./tcp-echo 9999
Server is listening on 9999
^C
07:19:45 $ ./tcp-echo 9999
Server is listening on 9999
^C
07:19:47 $ ./tcp-echo 9999
Server is listening on 9999
^C
07:19:49 $ ./tcp-echo 9999
Server is listening on 9999
^C
07:19:50 $ 

as you can see, this works as expected. Even if I kill -9 the server, the next invocation works.
I've even tried this by binding to a specific address using inet_aton() with no change in behaviour.
Compare this with what you are doing, and see if it provides a clue as to what you are doing wrong. If it doesn't work, then something is holding the port open between invocations. Take a look at what sudo netstat -tlnp has to tell you.
Quote:
SO_REUSEADDR
Indicates that the rules used in validating addresses supplied
in a bind(2) call should allow reuse of local addresses. For
AF_INET sockets this means that a socket may bind, except when
there is an active listening socket bound to the address.

When the listening socket is bound to INADDR_ANY with a spe‐
cific port then it is not possible to bind to this port for
any local address. Argument is an integer boolean flag.

So what address was it bound to before this call, and what address are you trying to bind to now?
 
Share this answer
 
For the definitive information on SO_REUSEADDR, you should get a copy of W. Richard Stevens "UNIX Network Programming". Here's the relevant pages from google books: UNIX Network Programming: The sockets networking API - W. Richard Stevens, Bill Fenner, Andrew M. Rudoff - Google Books[^]
 
Share this answer
 
Thank you very much for the reference. 
That is EXACTLY what is NOT working. 
I do get past bind after first call, when "bind" succeeds. 
It is the next call, after the app is restarted, which fails to reuse the address.
I'll try to access socket etc. using TCP , maybe the bluetooth application is the problem. 

PS I keep getting this "servers are overworked" message after I submit comment.
 
Share this answer
 
Comments
Richard MacCutchan 27-Feb-20 3:47am    
Please don't post comments as Solutions; use the Reply button above the relevant comment, or the Have a Question or Comment? button below the relevan solution. Otherwise your comments will likely be missed.

And the "servers overworked" message just means there is a lot happening at CodeProject central. It will clear itself in time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900