crseat

RECIEVE

Jan 23rd, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <sys/time.h>
  7. #include <unistd.h>      /* for gethostname() */
  8. #include <arpa/inet.h>   /* for IP address structures and functions */
  9. #include <netdb.h>
  10.  
  11.  
  12. int main(int argc, char *argv[], char *envp[])
  13. {
  14.         int     sock, msgsock;  /* Sockets are integer file descriptors on Linux */
  15.         struct  sockaddr_in name, caller;
  16.         struct  hostent *hp;
  17.         char    buf[2048], chrline[80];
  18.         int     size, length, ret, k, i; /* Process the command line for the buffer size, if given */
  19.         if (argc > 1)
  20.         {
  21.                 size = atoi(argv[1]);
  22.     /* Validate that the given argument is between 1 and sizeof(buf) - 1
  23.      * Set to the default size if argument is invalid */
  24.                 if (size < 1  ||  size > sizeof(buf) - 1)
  25.                         size = sizeof(buf) - 1;
  26.         }
  27.         else
  28.         {
  29.                 size = sizeof(buf) - 1;  /* Default size */
  30.         }
  31.  
  32.  
  33.   /* Create the listen socket. This is a TCP socket, so use SOCK_STREAM
  34.    * Exit if the socket cannot be created */
  35.         sock = socket(AF_INET, SOCK_STREAM, 0);
  36.         if (sock < 0)
  37.         {
  38.                 perror("receiver: socket() failed. ");
  39.                 return (-1);
  40.         }
  41.  
  42.   /* Bind the socket to an IP address and port. We will use the IP address
  43.    * INADDR_ANY, which tells the system to assign the IP address, and the
  44.    * port number 0, which tells the system to assign a random port number.
  45.    *
  46.    * First we have to set the fields in the sockaddr_in object "name" and then
  47.    * we can call bind(). Again, exit if bind() fails. */
  48.         name.sin_family = AF_INET;         /* TCP/IP family */
  49.         name.sin_addr.s_addr = INADDR_ANY; /* INADDR_ANY = assigned by system */
  50.         name.sin_port = htons(0);                /* 0 = assigned by system */
  51.         ret = bind(sock,(struct sockaddr *)&name,sizeof name);
  52.         if (ret < 0)
  53.         {
  54.                 perror("receiver: bind() failed. ");
  55.                 return (-1);
  56.         }
  57.  
  58.   /* In order to use vcsend to send data to this program, we need to know
  59.    * what port number the system just assigned this program. So this segment
  60.    * calls getsockname() to update the sockaddr_in object "name" with the
  61.    * system assigned values and then print that info to the screen. */
  62.         length = sizeof name;
  63.         ret = getsockname(sock, (struct sockaddr *)&name, (socklen_t *)&length);
  64.         if (ret < 0)
  65.         {
  66.                 perror("receiver: getsockname() failed. ");
  67.                 return (-1);
  68.         }
  69.  
  70.         sleep(1);       /* pause for clean screen display */
  71.         printf("\nreceiver: process id: %d ", (int)getpid());
  72.         printf("\nreceiver: IP address: %d.%d.%d.%d",
  73.         (ntohl(name.sin_addr.s_addr) & 0xff000000) >> 24,
  74.         (ntohl(name.sin_addr.s_addr) & 0x00ff0000) >> 16,
  75.         (ntohl(name.sin_addr.s_addr) & 0x0000ff00) >>  8,
  76.         (ntohl(name.sin_addr.s_addr) & 0x000000ff));
  77.         printf("\nreceiver: port number: %hu", ntohs(name.sin_port));
  78.         printf("\n");
  79.  fflush(stdout);
  80.  
  81.   /* Now we will call listen() and wait for a client to connect. The
  82.    * accept() function will block until there is a client or an error. */
  83.  
  84.         listen(sock,5);         /* up to 5 clients can connect. only 1st is accepted */
  85.         k = sizeof caller;
  86.         msgsock = accept(sock, (struct sockaddr *)&caller, (socklen_t *)&k);
  87.  
  88.   /* We only reach this point when there is an error or a client. We can
  89.    * check the value of msgsock (the data socket) to see which has happened */
  90.  
  91.         if (msgsock == -1)
  92.         {
  93.                 perror("receiver: accept() failed. ");
  94.         }
  95.         else
  96.   {
  97.     printf("\nreceiver: Valid connection received.\n");
  98.     printf("receiver: Sending handshake.\n");
  99.           fflush(stdout);
  100.  
  101.                 /* let vcsend know we are ready by sending a single character */
  102.           buf[0]= '0';
  103.           send(msgsock, buf, 1, 0);
  104.  
  105.     printf("receiver: Waiting for client....\n");
  106.  
  107.           do    {
  108.                         bzero(buf,sizeof buf);  /* zero buffer to remove old data */
  109.  
  110.       /* recv() will block until the client sends information, the client
  111.        * closes the connection or an error occurs on the data socket. */
  112.                         ret = recv(msgsock, buf, size, 0);
  113.                         if (ret < 0)
  114.                         {
  115.                                 perror("receiver: recv() failed. ");
  116.                         }
  117.                         if (ret == 0)
  118.                         {
  119.                     printf("        received-->sender has ended connection \n");
  120.                         }
  121.                         else
  122.                         {
  123.                                 printf("        received-->%s \n",buf);
  124.                         }
  125.  
  126.                         //recv(msgsock, chrline, sizeof(chrline) - 1, 0); /*Handshake character*/
  127.                         printf("enter line2> ");
  128.                         /* Read input from user */
  129.                         fgets(chrline, sizeof(chrline) - 1, stdin);
  130.                         printf("FINISHED fgets recieve");
  131.                         /* Check for the period to end the connection and also convert any
  132.                         * newlines into null characters */
  133.                         for (i = 0 ; i < (sizeof(chrline) - 1) ; i++)
  134.                         {
  135.                          printf("entered for loop recieve");
  136.                          if ( (chrline[i] == '\n') || (chrline[i] == '\0') )
  137.                             break;
  138.                         }
  139.                         if (chrline[i] == '\n')
  140.  {
  141.                           printf("entered if loop1 recieve");
  142.                           chrline[i] = '\0';    /* get rid of newline */
  143.                         }
  144.                         length = strlen(chrline);
  145.                         if (chrline[0] == '.')          /* end of stream */
  146.                         {
  147.                           printf("entered if loop 2 recieve");
  148.                           printf("sender: termination requested \n");
  149.                           break; /* Exit out of loop */
  150.                         }
  151.  
  152.                           ret = send(msgsock, chrline, length + 1, 0);
  153.  
  154.                         if (ret < 0)
  155.                         {
  156.                           perror("sender: write() failed. ");
  157.                           break;  /* Exit out of loop */
  158.                         }
  159.                 } while (ret != 0); /* Exit loop only when client ends connection */
  160.         }
  161.  
  162.   /* When we exit the recv() loop, the client has ended the connection, so
  163.    * all that remains is closing the sockets. */
  164.  
  165.         printf("receiver: ending session also and exiting. \n");
  166.         close(msgsock);         /* close data socket */
  167.         close(sock);      /* close listen socket */
  168.  
  169.         return (0);
  170. }  /* end of main */
Advertisement
Add Comment
Please, Sign In to add comment