[comp.lang.c] Streams

jpullman@dcsc.dla.mil (John M. Pullman II) (05/09/90)

   Greetings Experts!

   I am posting this for a young lady who has been beating her head
   against the wall over this problem for tooooo long. I hope that
   someone will be able to help her. Thanks! 

    Hi:
    The following two files one for receiving stream and one for sending            stream are copy from network programming of Sun's manual with minor             modification.  However, the program only offer one way communication.           What I want is to be able to communicate two ways.  How can I do?               Is there any way I can find out the host port.  Thanks. Yen 

/*  file 1 */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#define  TRUE 1
/****************************************************************/
/*        This the receive stream                               */
/*  USAGE : stream_receive                                      */
/*  Type in the above line before send the message from the     */
/*  the other machine so that the other machine can see the port*/
/*  number to send the message to.                              */
/*                                                              */
/*  This program create a socket and then begins an infinite    */
/*  loop.  Each time through the loop, it accepts a connection  */
/*  and prints out messages from it.  when the connection break,*/
/*  or a termination message comes through, the program accepts */
/*  a new connection.                                           */
/*                                                              */  
/****************************************************************/

main()
{
   int                     sock, length;
   struct  sockaddr_in     server;
   char                    buf[1024];
   int                     rval,msgsock;

   /* create socket from which to read */
   sock = socket(AF_INET, SOCK_STREAM,0);
   if (sock < 0) {
      perror("opening stream socket");
      exit(1);
   }
   
   /* create name with wild card */
   server.sin_family = AF_INET;
   server.sin_addr.s_addr = INADDR_ANY;
   server.sin_port = 0;

   if (bind(sock, (struct sockaddr *)&server, sizeof server) < 0) {
      perror("binding datagram socket");
      exit(1);
   }
   /* find assigned port value and print it out */
   length = sizeof server;
   if (getsockname(sock, (struct sockaddr *)&server, &length) < 0) {
      perror("getting socket name");
      exit(1);
   }
   
   printf("socket port #%d\n", ntohs(server.sin_port));

   /*  start accepting connections */
   listen(sock, 10) ;
   do { 
       msgsock = accept(sock, (struct sockaddr *)0, (int *)0);
       if (msgsock == -1)
          perror("accept");
       else do {
                bzero(&buf[0], 1024);
                if ((rval = read(msgsock, buf, 1024)) < 0)
                   perror("reading stream message");
                if (rval == 0)
                   printf("ending connection\n");
                else {
                   printf("%s\n", buf);
                   /* if not shell command then transfer data, otherwise,
                      execute the shell command***insert codes here  */
                   system(buf); }
                } while (rval != 0);
       close(msgsock);
       } while (TRUE);
   exit(0);
}


/*  file 2 */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#define  SIZE 1024
/*********************************************************/
/*             this is the send message                  */
/*  USAGE : inter_send   machine_address   port_number   */
/*********************************************************/

main(argc,argv)
   int argc;
   char *argv[];
{
   int sock, nread;
   char buf[1024];
   struct sockaddr_in  name;
   struct hostent *hp, *gethostbyname();

  /* create socket on which to send */
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
     perror("open stream socket");
     exit(1);
  }
   
  /*  get the host name from the command line */
  name.sin_family = AF_INET;
  hp = gethostbyname(argv[1]);

  if (hp == 0) {
     fprintf(stderr, "%s: unknown host0", argv[1]);
     exit (2);
  }

  bcopy((char *)hp->h_addr, (char *)&name.sin_addr, hp->h_length);
  name.sin_port   = htons(atoi(argv[2]));
  
  if (connect(sock,
     (struct sockaddr *)&name, sizeof name) < 0) {
     perror("connecting stream socket");
     exit(1);
  }

  while ( (nread = read(0, buf, SIZE)) > 0)
     if (write(sock, buf, nread) < 0)
        perror("writing a stream socket");

  close(sock);
  exit(0);
}