Click here to Skip to main content
15,667,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I like to do LIFO , so here is small addendum.

I need to verify that the "address" in question is either the remote BT device address or local BT device address.

Logically since it works as expected on first clear boot/ initial run it should be the remote address - the bluetooth devices are not yet communicating hence the local device address is in reality not available. Not until "connect".

BTW "address reuse" is no good for AF_BLUETOOTH.

I am using socket to access bluetooth device on local PC .
Since it is a (C++) work in progress I leave the SUCCESSFUL connection without closing it.
Subsequent rerun of the code generates "address already in use " error.
Which is actually good for making the code bulletproof by recovering from the error in code.
The question is HOW?

My so far working code has ONLY
allocate socket - which allays passes OK
bind - to bind socket to bluetooth device - using address - passes OK on first run.

So how do I reset " the address " if my only working code is - "socket(..."?
In other words all I have is allocated socket to work with.

I do not want to switch to another BT device - just release current address for reuse - in code.

What I have tried:

Closing socket file descriptor - did not work.
Rebooting PC works but it is a unacceptable hack.
Updated 10-Feb-20 7:29am
KarstenK 6-Feb-20 11:51am    
show the complete relevant code for diagnosis. Maybe your work flow is wrong at some step.
Richard MacCutchan 6-Feb-20 12:26pm    
Sockets tend to stay around for a fixed time after their creator has terminated. I think it is about 9 minutes. The Linux documentation should tell you.
Vaclav_ 6-Feb-20 13:50pm    
I am looking at two options in
setsockopt(FileDescriptor_socket, SOL_SOCKET, SO_LINGER | SO_REUSEADDR,
(const char*) &lin, sizeof(int));

Looks as SO_LINGER works at "completion of running program" - pretty vague or directly when exit() is processed. Not tested.
SO_REUSEADDR is specifically to allow bind() to reuse the address -that is where the "address already in use " is generated. So far no success.
The man specifically states "for AF_INET " so I am not sure if it will work for AF_BLUTTOTH.
Stefan_Lang 7-Feb-20 2:26am    
Hint: if you wish to reply to Richards comment, you should use the [Reply] button at the top of the comment. That will notify him of your response. Otherwise the only way for him to notice your reply is coming back to this topic for no reason...

So how do I reset " the address " if my only working code is - "socket(..."?

If your code doesn't properly close the socket, your only real option is to restart the machine.
Share this answer
This is not a solutions, but
1. I am tired to squint to read "comment'
2. So far it looks as "the problem" was binding the socket to WRONG "address"
But my code is broken and I need to recover from that (IDE quit big time ) before I can get back to "fixing" the bind() problem.
Share this answer
Richard Deeming 11-Feb-20 9:43am    
Poor eyesight is not an excuse to post a comment as a "solution".

If you believe the font size of the comments is too small, and you don't want to use your browser's "zoom" tools or an extension to override the site's CSS[^], you can request an increased font size in the Bugs and Suggestions[^] forum.

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