• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 575
  • Last Modified:

VB6: Winsock Address in use

I am making a 2-way file transfer program, with 2 winsocks, one for files and one for information. The problem is after the client connects, I invoke the close method for both, but if i try to connect again, it doesn't work. But heres the weird thing: If i step through the program, it works! But with normal F5 operation, i always get that error. Is there some way to close the port that I should know? If you need my code I can post it.
1 Solution
This is common.  I have incremented the port to avoid this.  Set up a port variable and each time you open the port increment it and use that variable as port number.  You don't need a lot of numbers. Say you start with 0, Take it to say, 5, then start over at 0 again.  This has to do with the way Windows reclaims the resources involved with allocating ports.  It seems to do this when it gets around to it rather than when you close the port...
It's all to do with Windows hanging onto the port for 5 (or 10?) minutes and then it releases it. The recommendation is for your CLIENT always to use Port 0 which tells Winsock to choose the next available port:

   frmMain.sckClient.LocalPort = 0 ' Force random choice of LOCAL socket.
   frmMain.sckClient.Connect 123.456.789.111, 5000

However, you SERVER should always listen on your selected port:

   frmMain.sckServer(0).LocalPort = 5000

(extracts from my Client/Server programs)
rlbondAuthor Commented:
Ok that worked

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now