Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Mar : Re: Closing a TIdTCPClient Connection

www.cryer.info
Managed Newsgroup Archive

Re: Closing a TIdTCPClient Connection

Subject:Re: Closing a TIdTCPClient Connection
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Tue, 28 Mar 2006 11:53:44

"Dennis" <marianndkc@home3.gvdnet.dk> wrote in message
news:44296371@newsgroups.borland.com...

> I just had the impression that they were there for a reason.

They are, but for very specific purposes that you don't need most of the
time.

If the machine has multiple networks installed, the BoundIP is used to
specify which network to bind the socket to.

If the machine is behind a firewall, the BoundPort is used to bind the
socket to a specific local port that the firewall allows to pass through it.
For a client socket, however, bound ports are not usually necessary.
Specifying 0 for the BoundPort allows the socket to pick a random port for
itself.  99% of the time, that is all that is needed.  Bound ports are more
important for server sockets instead.  That is not to say that clients never
need to be bound to specific ports, but it is not a common thing to do.

Keep in mind that a socket is identified by the IP/Port combination of
*both* the local endpoint that it is bound to, and the remote socket that it
is connected to.  Two or more sockets connecting to the same remote IP/Port
must be bound to different IP/Port combos locally.  Two or more sockets
connecting to different remote IP/Port combos can be bound to the same
IP/Port combo locally.

You are getting an "Already in Use" error because you are trying to
establishing a new socket for the same IP/Port combos on both the local and
remote endpoint that match the existing socket that is the OS is holding
open after your code closed it earlier.  At the very least, by letting the
client socket choose its own random port for the local side of the
connection, the IP/Port combination for the local endpoint changes, and the
new socket no longer matches the existing socket, thus no "Already in Use"
error.


Gambit

Replies:

none

In response to:

www.cryer.info
Managed Newsgroup Archive