Re: UNbinding a socket

S

Sin

I've got my daemon doing everything it should, except I can't find a clean
way to unbind a port from a socket that's bound and listening. This doesn't
keep the program from working, but it means that if it exits I can't
immediately re-run it (since I have to wait for the port to unbind on it's
own). If I ctrl-c out of it (while it's not in the middle of
communications) it works fine, but otherwise if I try to find any way to
exit gracefully, it doesn't unbind the port.

Anyone have a suggestion? I tried the shutdown feature, but it didn't seem
to have any effect that I could see... and nothing else looks useful (and
all online searches just turn up third-party libraries that I don't care to
use). There must be some simple way to do this...


Had the same problem 2 weeks ago... Check out SO_REUSEADDR... Here's an
example :


// ... open socket ...

BOOL reuse= 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(BOOL))
< 0) {
printf("setsockopt() failed\n");
exit(-1);
}

// ... bind socket ...

Alex.
 
E

Everett Hickey

Sin said:
Had the same problem 2 weeks ago... Check out SO_REUSEADDR... Here's an
example :


// ... open socket ...

BOOL reuse= 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(BOOL))
< 0) {
printf("setsockopt() failed\n");
exit(-1);
}

// ... bind socket ...

Thanks, worked great. Still figuring out why it works though (I
understand what it does, just working on how it does it... hehe).
 
S

Sin

Had the same problem 2 weeks ago... Check out SO_REUSEADDR... Here's an
Thanks, worked great. Still figuring out why it works though (I
understand what it does, just working on how it does it... hehe).

I have no idea. I just know it doesn't behave this way in Windows. Better
ask on a proper socket programming newsgroup I guess... It has nothing to do
with C++...

Alex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top