Click to See Complete Forum and Search --> : [RESOLVED] Socket/Port remains open after app crashes
tariqc
August 13th, 2009, 05:04 PM
I'm having this problem with a networked app in vb.net.
If the program exits normally the port closes fine, however, if it crashes sometimes it remains open and I am unable to close it. The next time I run the program it is unable to open the port because it is already in use. Here's the exception:
10048: Only one usage of each socket address (protocol/network address/port) is normally permitted
Any ideas on how I can reclaim this port, or how I can close it?
I tried using the reuse address socket option
SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1)
but that just opens the port again and does not close the old one. see this netstat output:
TCP PCISERVER:14000 PCISERVER.cpwpci.cpwgroup.net:0 LISTENING 4320
[System]
TCP PCISERVER:14000 PCISERVER.cpwpci.cpwgroup.net:0 LISTENING 5808
[System]
TCP PCISERVER:14001 PCISERVER.cpwpci.cpwgroup.net:0 LISTENING 5808
[System]
TCP PCISERVER:14001 PCISERVER.cpwpci.cpwgroup.net:0 LISTENING 4320
[System]
As you can see once the program exits the ports remain opened by the System process.
I don't know of a way to stop them from listening besides a restart.
henky@nok.co.id
August 13th, 2009, 09:07 PM
Well, this is C/C++ forum. IMO, you may post the question to VB .NET forum.
tariqc
August 14th, 2009, 12:13 PM
Oops... I'm sorry. I didn't realize - my first time posting here. I thought it was a general networking forum didn't see the parent topic.
Richard.J
August 14th, 2009, 12:27 PM
I do think this is the right forum.
Using the REUSEADDR option is the right way to go. There is no other reason to use this option than in a server that might have crashed. Please not that using this option in an application if an instance of it is already runnning may lead to undefined behaviour.
tariqc
August 14th, 2009, 12:40 PM
well i reposted in VB.Net anyways. Although I do believe it to be a general windows programming question.
When I use reuse address it just starts listening on the same port and keeps listening on the original port. Note that both ports have the name number. See the netstat output in my original post. You see how there are two entries for 14000 & 14001 these are both attached to the process [System]. These two entries were created after the program has crashed twice. The real problem is that I am unable to connect with a client when this happens.
Richard.J
August 14th, 2009, 02:13 PM
if netstat says "listening", then the first process is most likely still alive. A crashed process may leave a socket in state "TIME_WAIT". What does the task manager tell you? Do you have 2 instances of your application?
tariqc
August 14th, 2009, 02:23 PM
No the application is no longer running I checked, it is not shown to be running in task manager.
i ran netstat -b which shows the attached processes.
It shows the System process attached to these ports after crashing, not the application process.
Richard.J
August 14th, 2009, 02:44 PM
can TCPView help? Maybe you are suffering from a specialty of VB.net?
http://technet.microsoft.com/en-us/sysinternals/bb897437.aspx
tariqc
August 14th, 2009, 03:37 PM
I attached the results from tcpview it shows the process as non-existent.
http://www.codeguru.com/forum/attachment.php?attachmentid=25275&d=1250278510
Richard.J
August 14th, 2009, 05:00 PM
well, then I can't help any further. I don't like VB, I think that has been a language for children way back in the early 80's ;-)
I don't know what the OS is doing with VB applications. I can tell that a 'normal' application that crashes may leave the socket in an undefined state, but the REUSEADDR option solves this problem. I have used it myself.
tariqc
August 14th, 2009, 06:34 PM
lol, yeah. I don't like it either. Its easy to code in, however. The other programmer here knows VB so I used it in case he had to change my code. Supposedly all languages in .net are the same. I'm not about to rewrite the whole app in VC++. This might only be happening because I just started running it as a service. I'll figure it out. Thanks for the help though.
henky@nok.co.id
August 14th, 2009, 10:56 PM
I do think this is the right forum.
well, then I can't help any further. I don't like VB, I think that has been a language for children way back in the early 80's ;-)
That's why I told to tariqc that this is C/C++ forum. I don't think people
who's looking at here knows much about VB .NET framework. He may
post the same question to VB .NET forum.
tariqc
August 15th, 2009, 12:48 PM
Thanks for the help guys... I did post in VB.net so hopefully someone on there will know something. I'll keep this thread updated in case anyone is curious to know the outcome.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.