Socket still used after close

Programming, for all ages and all languages.
Post Reply
icealys6
Posts: 5
Joined: Wed Aug 06, 2014 9:20 pm

Socket still used after close

Post 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?
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: socket programming

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: socket programming

Post by bluemoon »

You may also tell the system to SO_REUSEPORT, read the manual for detail and consequence.
icealys6
Posts: 5
Joined: Wed Aug 06, 2014 9:20 pm

Re: Socket still used after close

Post 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.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Socket still used after close

Post by sortie »

SO_REUSEPORT is what you wish to use. Can you confirm if that makes your problem go away?
icealys6
Posts: 5
Joined: Wed Aug 06, 2014 9:20 pm

Re: Socket still used after close

Post 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.
icealys6
Posts: 5
Joined: Wed Aug 06, 2014 9:20 pm

Re: Socket still used after close

Post 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.
Post Reply