coop@panther.cerc.wvu.wvnet.edu (Cooperate) (11/20/90)
Hi, I know that server program is listening on port 6000 for display 0. Can sombody explain to me how two clients communicate with one server on the same display ? Thanks.
mouse@LARRY.MCRCIM.MCGILL.EDU (11/21/90)
> I know that server program is listening on port 6000 for display 0. For TCP, yes. And 6001 for display 1, 6002 for display 2, etc. (This "port 6000 + display number" is a TCP thing.) > Can sombody explain to me how two clients communicate with one server > on the same display ? This is really not an X question but a TCP question. The key is to realize that each end knows the (machine,port) pair identifying the other end, and that a full identification of the connection requires not two but four pieces of information: the machine and port addresses for both ends. Thus, for example, if we have machine "server" as the server and "client" as the client, the X server (my examples assume display 0) will establish a listening endpoint at (server,6000)[%]. When a request comes in from the client, say port 1234[$], a connection will be created whose four pieces of information are (server,6000,client,1234). Because all four of these pieces of information are present in each packet that's sent on the behalf of this connection, each end can uniquely identify the connection. If another client connects, say port 2345 from machine client2, another connection (server,6000,client2,2345) is created, but there is no confusion among the apparent three endpoints present on the server machine: each one responds to only its own kind of incoming packets. The listening endpoint gets connection-attempt packets, the first connection gets packets identifying themselves as belonging to a (server,6000,client,1234) connection, and the other connection gets (server,6000,client2,2345) packets. (Other packets, say a packet claiming to belong to a (server,6000,othermachine,1194) connection, will elicit an error reply (in this case a packet with the RST bit set) and won't be sent to any of the three endpoints.) [%] When I give a machine name, like "server" or "client", you should note that what is actually present is the address of the machine, not the name. (Some machines have multiple addresses; while listening endpoints can have "wildcard" machine addresses because the machine address is always on the machine the endpoint is on, connections cannot: when a connection is established to a machine with multiple addresses the connection is always made to some specific one of its addresses.) [$] These numbers (the 1234 and the 2345 and 1194 I use later) are typically chosen more or less at random by the TCP implementation on the machine that sends the connection request. A good implementation will promise that only some small range of port numbers (say, 1024-2047) will be assigned automatically this way. On a more-or-less-BSD UNIX machine, you can see this in action: run "netstat -n -a" and examine the output (save it somewhere, it's probably long). Notice that some sockets are in a LISTEN state; these are the listening endpoints. Others, with remote addresses shown, correspond to connections in progress. (The state column has many possible values; the above is a vastly simplified description of how TCP works.) der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu
black@westford.ccur.com (Sam Black) (11/21/90)
> > I know that server program is listening on port 6000 for display 0. Can > sombody explain to me how two clients communicate with one server on the same > display? Thanks. > When a client opens the listener socket (port 6000+display), the server makes an "accept" system call, which switches the server's end of the socket to another port. Thus, if the listener is file descriptor 0, and this is the first client, the "accept" call will open file descriptor 1 as a new socket to the client. The client does not have to do anything special; its end of the socket remains as is. - sam ------------------------------------------------------------------------------- Once you remove the absurdity from human existence, there isn't much left. __________ / _______/__ ...!{decvax,uunet}!masscomp!black /__/______/ / black@westford.ccur.com Concurrent /_________/ Computer Corporation -------------------------------------------------------------------------------