Topic : BSD Sockets
Author : Unknown
Page : << Previous 6  Next >>
Go to page :


         else
            if (cnt == 0) {
               printf("network server received message");
               printf(" of length %d\n", cnt);
               printf("network server closing");
               printf(" client connection...\n");
               close (new_sd);
               continue; /* break out of loop */
            }
            else {

               /*  Print out message received from client.  Send
                   a message back.        */

               printf("network server %d received message",pid);
               printf(" of length %d\n", cnt);
               printf("network server %d  received", pid));
               printf(" the message %s\n", buf);
               bzero (buf, sizeof(buf)); /* zero buf, BSD. */
               /* memset(buf,0,sizeof(buf)); /* zero buf, S5. */
               strcpy(buf, "Message from server to client");
               write (new_sd, buf, sizeof(buf));
            }  /* end of message-print else */

         }  /* end of do loop statement */

         while (cnt != 0);  /* do loop condition */
         exit(0);  /* Exit child process */

      } /* End of if-child-process true condition */

      else      /* Not child process; must be parent process */

         close (new_sd); /* Parent doesn't need work socket. */

   }  /* end of while (1) */

}  /* end of main procedure */


Client Source Listing - vcclient.c

/*  vcclient.c -- TCP network (virtual circuit) client  */

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>    /* sockaddr_in structure */

/* This entry allows the program to look up the name
   of the host and any alias names associated with it.    */

#include <netdb.h>          /* /etc/hosts table entries */

main (argc, argv)
int argc;
char *argv[];

/* Expected command line parameters:

   argv[0] -- name of executable
   argv[1] -- the host name to which connection is desired
   argv[2] -- the port number to be used by the client: the
              value is the port number assigned to the
              server by the server's host system.

   This is not elegant, but is very useful for debugging the
   connectivity code.  For example, if the host name ( argv[1] )
   were speficied as "localhost" both client and server
   could run on the same system but the network connectivity
   code would still be fully exercised.         */

{
   int sock,          /* socket descriptor */
       val,           /* scratch variable */
       cnt;           /* number of bytes I/O */

   struct sockaddr_in myname;  /* Internet socket name (addr) */
   struct sockaddr_in *nptr;   /* pointer to get port number */

   char buf[80];   /* I/O buffer, kind of small  */

   /* For lookup in /etc/hosts file.   */

   struct hostent *hp, *gethostbyaddr();

   /* Check that the user supplied all parameters on the
      command line.  If so, convert argv[2] to integer; copy it
      into the sin_port field of the myname structure.  Use
      the htons function to insure that the value is stored
      in network byte order.                           */

   if ( argc < 3 ) {
      printf("network client failure: required parameters");
      printf(" missing from the command line\n");
      printf("network client: usage");
      printf("[executable-name] [host name] [port number]\n");
      exit(1);
   }

   /* As in UNIX domain, create a client socket to request
      service          */

   if (( sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
      printf("network client socket failure %d\n", errno);
      perror("network client");
      exit(2);
   }

   /* Convert user-supplied port number to integer.  There is
      no consistency check possible in this set-up.        */

   myname.sin_port = htons( atoi(argv[2]) ); /* Server port # */

   myname.sin_family = AF_INET;    /* Internet domain */

   /* For display purposes only, print out the host name and
      the converted port number.                      */

   printf("network client %s will try to connect to host %s\n",
          argv[0], argv[1]);
   printf("network client %s will use port number %d\n",
          argv[0], ntohs ( myname.sin_port ) );

   /* Obtain server host information.  */

   hp = gethostbyname ( argv[1] );

   /* This is mainly debug code; if it becomes necessary to
      insert it (or if it just feels more comfortable to
      have it in place!!!) don't remove it when all is working
      well.  Rather, leave it in place as a comment.      */

   if ( hp == NULL ) {
      printf("network client gethostbyname failure %d\n",
           errno);
      perror("network client");
      close ( sock );
      exit(3);
   }
   else {
      printf("\nServer information obtained via");
      printf(" gethostbyname:\n");
      printf("\tThe official host name is %s\n", hp -> h_name);
      printf("\tThe address type is %d\n", hp -> h_addrtype);
      printf("\tThe length of the address is %d bytes\n",
            hp -> h_length);
      printf("\tThe first host address is %lx\n",
             ntohl ( * (int * ) hp -> h_addr_list[0] ) );
      printf("\tAlias names for the host are:\n");
      while ( *hp -> h_aliases )
         printf( "\t\t%s\n", *hp -> h_aliases++ );
   }

   /* Use either memcpy or bcopy as


Page : << Previous 6  Next >>