Socket still used after close
Socket still used after close
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?
- thepowersgang
- Member
- Posts: 734
- Joined: Tue Dec 25, 2007 6:03 am
- Libera.chat IRC: thePowersGang
- Location: Perth, Western Australia
- Contact:
Re: socket programming
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.
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
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Re: socket programming
You may also tell the system to SO_REUSEPORT, read the manual for detail and consequence.
Re: Socket still used after close
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
SO_REUSEPORT is what you wish to use. Can you confirm if that makes your problem go away?
Re: Socket still used after close
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
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.