DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with sockets

Clients

The client side of the remote login service was shown in ``Manipulating byte order''. One can see the separate, asymmetric roles of the client and server clearly in the code. The server is a passive entity, listening for client connections, while the client process is an active entity, initiating a connection when invoked.

Let us consider more closely the steps taken by the client remote login process. As in the server process, the first step is to locate the service definition for a remote login:

   sp = getservbyname("login", "tcp");
   if (sp == NULL) {
   	fprintf(stderr,
   		"rlogin: tcp/login: unknown service\n");
   	exit(1);
   }
Next the destination host is looked up with a gethostbyname call:
   hp = gethostbyname(argv[1]);
   if (hp == NULL) {
   	fprintf(stderr, "rlogin: %s: unknown host\n", argv[1]);
   	exit(2);
   }
With this done, all that is required is to establish a connection to the server at the requested host and start up the remote login protocol. The address buffer is cleared, then filled in with the Internet address of the foreign host and the port number at which the login process resides on the foreign host:
   memset((char *)&server, 0, sizeof(server));
   memcpy((char *) &server.sin_addr, hp->h_addr, hp->h_length);
   server.sin_len = sizeof(server);
   server.sin_family = hp->h_addrtype;
   server.sin_port = sp->s_port;
A socket is created, and a connection initiated. Note that connect implicitly performs a bind call, since s is unbound.
   s = socket(hp->h_addrtype, SOCK_STREAM, 0);
   if (s < 0) {
   	perror("rlogin: socket");
   	exit(3);
   }
    ...
   if (connect(s, (struct sockaddr *)&server,
    sizeof(server)) < 0) {
   	perror("rlogin: connect");
   	exit(4);
   }
The details of the remote login protocol will not be considered here.
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004