Changeset 1703

Show
Ignore:
Timestamp:
04/30/07 12:52:56 (2 years ago)
Author:
zcutlip
Message:

reader.c & writer.c:
changed path to socket file to /var/tmp/ rather than /usr/share/datagramSocket/
tweaked usage of strncpy
changed "sockaddr_un name" to more descriptive client_address and server_address

writer.c:
commented out: extern int make_named_socket (const char *name);

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/test/testcases/cprogram/clientServerDatagramSocket/reader.c

    r927 r1703  
    1212 
    1313 
    14 #define READER  "/usr/share/datagramSocket/reader_socket" 
     14#define READER  "/var/tmp/reader_socket" 
    1515#define MAXMSG  512 
    1616 
    1717 
    18      int 
    19      main (void) 
    20      { 
    21        int sock; 
    22        char message[MAXMSG]; 
    23        struct sockaddr_un name; 
    24        size_t size, size_so; 
    25        int nbytes; 
     18int main (void){ 
     19        int sock; 
     20        char message[MAXMSG]; 
     21        struct sockaddr_un server_address; 
     22        size_t size, size_so; 
     23        int nbytes; 
     24         
     25        /* Remove the filename first, it's ok if the call fails */ 
     26        unlink (READER); 
     27         
     28        /* Make the socket, then loop endlessly. */ 
     29        sock = socket (PF_LOCAL, SOCK_DGRAM, 0); 
     30        if (sock < 0){ 
     31                perror ("socket"); 
     32                exit (EXIT_FAILURE); 
     33        }//end if 
     34         
     35        server_address.sun_family = AF_LOCAL; 
     36        strncpy (server_address.sun_path, READER, sizeof (server_address.sun_path)-1); 
     37        server_address.sun_path[sizeof(server_address.sun_path)-1]='\0'; 
     38         
     39        size_so = (offsetof (struct sockaddr_un, sun_path) 
     40                + strlen (server_address.sun_path) + 1); 
     41         
     42        if (bind (sock, (struct sockaddr *) &server_address, size_so) < 0){ 
     43                perror ("bind"); 
     44                exit (EXIT_FAILURE); 
     45        }//end if 
    2646 
    27        /* Remove the filename first, it's ok if the call fails */ 
    28        unlink (READER); 
    29  
    30        /* Make the socket, then loop endlessly. */ 
    31        sock = socket (PF_LOCAL, SOCK_DGRAM, 0); 
    32        if (sock < 0) 
    33          { 
    34            perror ("socket"); 
    35            exit (EXIT_FAILURE); 
    36          } 
    37  
    38        name.sun_family = AF_LOCAL; 
    39        strncpy (name.sun_path, READER, sizeof (name.sun_path)); 
    40  
    41        size_so = (offsetof (struct sockaddr_un, sun_path) 
    42                + strlen (name.sun_path) + 1); 
    43  
    44        if (bind (sock, (struct sockaddr *) &name, size_so) < 0) 
    45          { 
    46            perror ("bind"); 
    47            exit (EXIT_FAILURE); 
    48          } 
    49        while (1) 
    50          { 
    51            /* Wait for a datagram. */ 
    52            size = sizeof (name); 
    53            nbytes = recvfrom (sock, message, MAXMSG, 0, 
    54                               (struct sockaddr *) & name, &size); 
    55            if (nbytes < 0) 
    56              { 
    57                perror ("recfrom (server)"); 
    58                exit (EXIT_FAILURE); 
    59              } 
    60  
    61            /* Give a diagnostic message. */ 
    62            fprintf (stderr, "Server: got message: %s\n", message); 
    63          } 
    64      } 
     47        while (1){ 
     48                /* Wait for a datagram. */ 
     49                size = sizeof (server_address); 
     50                nbytes = recvfrom (sock, message, MAXMSG, 0, 
     51                                (struct sockaddr *) & server_address, &size); 
     52                if (nbytes < 0){ 
     53                        perror ("recfrom (server)"); 
     54                        exit (EXIT_FAILURE); 
     55                } 
     56         
     57                /* Give a diagnostic message. */ 
     58                printf("Server: got message: %s\n", message); 
     59        }//end while 
     60}//end main 
  • trunk/test/testcases/cprogram/clientServerDatagramSocket/writer.c

    r927 r1703  
    1010 
    1111 
    12 #define READER  "/usr/share/datagramSocket/reader_socket" 
     12#define READER  "/var/tmp/reader_socket" 
    1313#define MESSAGE "Yow!!! Are we having fun yet?!?" 
    1414 
     
    1818     main (void) 
    1919     { 
    20        extern int make_named_socket (const char *name); 
     20//       extern int make_named_socket (const char *name); 
    2121       int sock; 
    22        struct sockaddr_un name
     22       struct sockaddr_un client_address
    2323       size_t size; 
    2424       int nbytes; 
     
    3232 
    3333       /* Initialize the server socket address. */ 
    34        name.sun_family = AF_LOCAL; 
    35        strcpy (name.sun_path, READER); 
    36        size = strlen (name.sun_path) + sizeof (name.sun_family); 
     34       client_address.sun_family = AF_LOCAL; 
     35        strncpy (client_address.sun_path, READER, sizeof (client_address.sun_path)-1); 
     36        client_address.sun_path[sizeof(client_address.sun_path)-1]='\0'; 
     37//       strcpy (name.sun_path, READER); 
     38       size = sizeof (client_address); 
    3739 
    3840       /* Send the datagram. */ 
    3941       nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0, 
    40                         (struct sockaddr *) & name, size); 
     42                        (struct sockaddr *) & client_address, size); 
    4143       if (nbytes < 0) 
    4244         {