Opening Sockets on localhost

Alex Tweedly alex at tweedly.net
Wed Mar 16 06:13:46 EST 2005


Dan Shafer wrote:

> Alex.....
>
> I got it. IOW, opening a socket doesn't work unilaterally. The 
> "server" has to have a listener on that port first. Right?

Right. When you open a TCP socket  (i.e. open socket to "host:port"), a 
TCP connection is formed. A packet is sent from your machine's TCP stack 
to his, and he sends a reply (assuming some application has done "accept 
connection" on the correct port). If there is no-one listening on that 
port, the remote machine will either ignore the incoming packet, or send 
back a "reject" packet.

When (if) your host receives a positive acknowledgment, then the 
connection is successful (from your point of view); your TCP stack then 
sends him back a third packet - and only when he receives it does he 
consider the connection complete.  (He can't consider it complete until 
then, because there could be a problem getting his packets to you - the 
only way he knows that has succeeded is when you reply).  You may hear 
this referred to as "TCP's three-way handshake".

Note that when you open a UDP socket (i.e. open datagram socket to 
"host:port"), things are completely different. UDP (datagram) is 
"connectionless", so the open socket does not cause any network 
activity, and will generally succeed provided the "host:port" have valid 
values.

> I'm going to figure out this server stuff one of these days.

And once you do, you'll realize how simple it all was to begin with, and 
wonder why no-one ever wrote a decent explanation of it in the first 
place. It's because most of us can't write decent explanations of 
anything :-)

-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.7.3 - Release Date: 15/03/2005



More information about the use-livecode mailing list