-
Winsock Problem
I have a server Application that takes connections on Port X lets say.
When I am terminating the Application I am executing the following code :
and I am looping waiting for the winsock to close
WHen I try to run the application Again. It always gives me the error Port in USe....
I have to restart for it to clear the port ... Any ideas How I can explicitely free the Port I am connecting to ?
-
Code:
Private Sub Form_Unload()
Winsock1.Close
End
End Sub
i think not sure
-
That is what I did ... but it is not working
-
it looks like your using index's. did you close all the winsocks?
-
Yes in fact index 0 Winsock is the only one listening to the specified port...
the others are accepting connections on other ports...
-
How soon after stopping the application do you try to restart it?
There is a period of time that the port will be in state TIME_WAIT. The TIME_WAIT value is set to 2 times the maximum segment lifetime, which is related to the time-to-live (TTL) value of IP datagrams within the network. Simply put, this allows TCP to ensure that connections are properly closed. The side initiating the close must hang around long enough for the other side to send its final packet.
You can check the port's state by doing a netstat from a command prompt.
-
hmmm yeah .. you are right ... i noticed that sometimes after leaving it a while before restarting it it did nt generate any errors , but I thouht that when I ask it to close it would directly close ... and by the way the winsock i am takling about is a Server winsock ( listening ) ...
and by the way the api function Listen exists right ??