Page 1 of 1

Socket still used after close

Posted: Wed Aug 06, 2014 9:26 pm
by icealys6
hi , i'm trying to create an IRC server but something strange happens. after i debug the program i press the X to exit the program and it exits with a long signed integer. Then when I try to run the program again the bind function gives me an error 10048 indicating that the socket is open still. So I ran netstat -a -n -o to view which process ID is open under that socket. I find out what it is and open task manager and find out that there is no process under that process ID. What the heck is wrong?

Re: socket programming

Posted: Wed Aug 06, 2014 10:55 pm
by thepowersgang
When a TCP connection closes in a particular way, the connection stays in a state called "TIME_WAIT", which prevents the port from being reused until all possible in-flight traffic has been received or discarded. This is to stop existing in-flight traffic from adding to network congestion (if the packets hit a closed socket, they would generate a TCP RST response) and from confusing the network stack.

Take a look at the state diagram in RFC793 (Page 22) if you're still confused.

Re: socket programming

Posted: Thu Aug 07, 2014 12:03 am
by bluemoon
You may also tell the system to SO_REUSEPORT, read the manual for detail and consequence.

Re: Socket still used after close

Posted: Thu Aug 07, 2014 8:13 am
by icealys6
I don't think that is the issue because I tried to change it to UDP and i still get the same problem with bind. And when i look at netstat i can see port 6667 being in use with UDP. I looked up the PID in task manager that corresponds to port 6667 and that PID doesn't exist. Its saying that an instance of scvhost is using up the port but when i look for the PID its not there.

Re: Socket still used after close

Posted: Thu Aug 07, 2014 9:30 am
by sortie
SO_REUSEPORT is what you wish to use. Can you confirm if that makes your problem go away?

Re: Socket still used after close

Posted: Thu Aug 07, 2014 11:52 am
by icealys6
OK well... i used setsockopt function and i used SO_REUSEADDR. it still didn't fix the issue. I also checked netstat and it sais its still listening on that port. when i try to taskkill the PID that corresponds to the open socket it tells me that process could not be found.

Re: Socket still used after close

Posted: Fri Aug 08, 2014 6:28 pm
by icealys6
SO_REUSEADDR was able to make my program work. Why does windows keep the socket listening after i exit my program? It doesn't make any sense to me , i thought that windows frees all handles associated with the process when the process ends...but windows seems to think that the process is still running when it isn't.