Author : Unknown
Page : << Previous 5 Next >>
printf("\taddr.sa_data string is %d bytes long;\n",
sizeof ( addr.sa_data ) );
printf("\taddr.sa_data string is:");
for ( cnt = 0: cnt < sizeof (addr.sa_data); cnt++)
printf(" %x", addr.sa_data[cnt]);
printf("\n");
/* Now "advertise" the port number assigned to the
socket. In this example, this port number must be
used as the second command line parameter when
starting up the client process. */
/* Note the use of the pointer nptr, with a different
mapping of the allocated memory, to point to the
generic address structure. */
nptr = (struct sockaddr_in *) &addr; /* port # */
printf("\n\tnetwork server: server has port number: %d\n",
ntohs ( nptr -> sin_port ) );
/* Mark the socket as a "listen-only" or passive socket */
if ( listen ( sock, 5 ) < 0 ) {
printf("network server bind failure %d\n", errno);
perror("network server");
close (sock);
exit(4);
}
/* Debug output: information contained in myname structure
(the Internet socket). */
printf("Server has set up client socket with values:\n");
printf("\tInternet address is %lx\n", myname.sin_addr.s_addr);
printf("\tPort number used is %d\n", myname.sin_port);
printf("\tInternet family ID is %d\n", myname.sin_family);
printf("\tValues are filled in after connection request ");
printf("is accepted.");
/* Set up "infinite loop" to listen for clients. Since the
structure "myname" is bound to the listen socket, the
socket structure name and socket length parameter
values are omitted from the accept call. The bound values
are used. */
while (1) {
if ( ( new_sd = accept ( sock, 0, 0 ) ) < 0 ) {
printf("network server accept failure %d\n", errno);
perror("network server");
close (sock);
exit(5);
}
/* Fork child process to handle client service request */
if ( ( fork() ) == 0 ) { /* Child process */
int pid;
pid = getpid(); /* PID of child process */
close (sock); /* Do not need listen socket in child. */
/* Find out who the client is. Note the use of the
generic address structure addr to hold information
about the (connected) client. */
if ((rc = getpeername( new_sd, &addr, &adrlen )) < 0) {
printf("network server %d getpeername failure %d\n",
pid, errno);
perror("network server");
close(new_sd);
exit(6);
}
/* Just for grins, "announce" the client. Note that,
since pointer nptr is of type struct sockaddr_in,
the field names as defined in the structure template
sockaddr_in can be used to access values in the addr
generic structure. */
printf("\n\tnetwork server %d:", pid);
printf(" client socket from host %s\n",
inet_ntoa ( nptr -> sin_addr ) );
printf("\t has port number %d\n",nptr -> sin_port);
/* Now find all names associated with the client; this
is the reason for the /etc/hosts file lookup
declarations. */
if (( hp = gethostbyaddr (&nptr -> sin_addr,4,AF_INET))
!= NULL ) {
printf ("\tfrom hostname: %s\n\twith aliases: ",
hp -> h_name );
while ( *hp -> h_aliases )
printf ("\n\t\t\t%s", *hp -> h_aliases++ );
printf("\n\n");
}
else {
printf("network server %d ", pid);
printf("gethostbyaddr failure %d\n", h_errno);
perror("network server");
}
/* Exchange data with client. Clear buffer first. */
do {
/* Take your pick, depending on system pedigree.
The System V function has not been tested as of
this edition. */
bzero( buf, sizeof(buf)); /* zero buf, BSD call. */
/* memset (buf,0,sizeof(buf)); /* zero buf, S5. */
/* Read message from remote client; if message length
= 0, quit. */
if (( cnt = read (new_sd, buf, sizeof(buf))) < 0 ) {
printf("network server %d ", pid);
printf("socket read failure &d\n", errno);
perror("network server");
close(new_sd);
exit(7);
}
Page : << Previous 5 Next >>