views:

21

answers:

1

In a VB6 Application, I am looking for a way to determine the Local Port for the duplex socket.

I have the remote Ip and Port, but I need to know an print out the local port as well.

+1  A: 

Just query the Winsock control's LocalPort property.

The LocalPort property is used both to set the send/listen port and to retrieve it once the connection has been established. Even if you specify port 0, in which case the control will select a random port, after a connection is established, this property will contain that port number.

raven
Assuming TCP, it is usually a bad idea not to set LocalPort to 0 before connecting. Otherwise after disconnecting you won't be able to reconnect until ~ 4 minutes elapse. Search on "TIME-WAIT" for more details.
Bob Riemersma
Yes, I have run into that 4 minute issue myself. 2*ML where ML is set in the registry. The ML value is 4 minutes by default, but can be lowered to 15 seconds. I do not believe you can set it to a value of less than 15 seconds, giving a reconnect wait of 30 seconds min. But the advice about the LocalPort to 0 avoids that until you run out of the 5000 sockets per user limit that windows has.
Q Boiler