| 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; |
|---|
| | 18 | int 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 |
|---|
| 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 |
|---|