*BSD News Article 98177


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!news.ecn.uoknor.edu!feed1.news.erols.com!howland.erols.net!europa.clark.net!dispatch.news.demon.net!demon!mail2news.demon.co.uk!beckley.demon.co.uk!iwta
From: iwta@beckley.demon.co.uk (Ian W Taylor)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Listening at a socket
Date: Sat, 21 Jun 97 09:21:17 GMT
Organization: Glencarn Ltd.
Message-ID: <866884877snz@beckley.demon.co.uk>
References: <5oblqi$4re@ui-gate.utell.co.uk>
Reply-To: iwta@beckley.demon.co.uk
X-Mail2News-User: iwta@beckley.demon.co.uk
X-Mail2News-Path: beckley.demon.co.uk
X-Newsreader: Demon Internet Simple News v1.30
Lines: 48
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:43266

In article <5oblqi$4re@ui-gate.utell.co.uk>
           brian@awfulhak.org, "Brian Somers" writes:

> For example, if I run an ftp server, I have my ftpd
> listening on *.21.  If I currently have 2 established
> connections from the same remote machine, I'll have two
> additional ftpd processes running (forked from the first).
> When a packet is received from the other machine destined
> for port 21, how does the OS decide which process to deliver
> the packet to ?

This is what happens at each client end...

	The client program opens a socket (see 'man socket') and connects 
	(see 'man connect') to the 'Well Known Port' on the server.  In
	this case the Well Known Port is 21.

	The clients port number is allocated by the OS and is ,say ,
	client1:4576

At the server end, ftpd will ...

	Opens a socket and binds (see 'man bind') it to the well known port
	(ie.  server:21).  Any other process attempting to bind to this port
	will then be rejected.

	It then listen's (see 'man listen') on the well known port, and will
	be suspended until the clients request to talk to the server arrives.

	When it does ftpd is awoken by the OS, and accepts (see 'man accept')
	the call.  The accept() returns a new socket with a port number 
	allocated by the OS (eg. server:2376).  The server can then fork a 
	child process to serve the clients requests, and can go back to 
	listen on port 21.

So, in this case, client:4576 communicates with server:2376
whilst a second client, on client:4586, is serviced by a second forked
copy of ftpd using, say, server:2423.  Meanwhile the initial ftpd sleeps
on server:21 listening for a new client.

Hope this simplified explanation helps.  It is normally inetd that listens
on the 'Well Known Port' numbers, and will spawn ftpd children upto a
max concurrency.

-- 
Regards,
   Ian T.