| 1 |
/* Example from glibc manual (16.9.7). */ |
| 2 |
/* { dg-require-effective-target sockets } */ |
| 3 |
/* { dg-additional-options "-Wno-analyzer-too-complex" } */ |
| 4 |
/* { dg-skip-if "" { hppa*-*-hpux* powerpc*-*-aix* } } */ |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <errno.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#if __has_include(<sys/select.h>) |
| 13 |
#include <sys/select.h> |
| 14 |
#endif |
| 15 |
#include <sys/socket.h> |
| 16 |
#include <netinet/in.h> |
| 17 |
#include <arpa/inet.h> |
| 18 |
#include <netdb.h> |
| 19 |
|
| 20 |
#define PORT 5555 |
| 21 |
#define MAXMSG 512 |
| 22 |
|
| 23 |
int |
| 24 |
make_socket (uint16_t port) |
| 25 |
{ |
| 26 |
int sock; |
| 27 |
struct sockaddr_in name; |
| 28 |
|
| 29 |
/* Create the socket. */ |
| 30 |
sock = socket (PF_INET, SOCK_STREAM, 0); |
| 31 |
if (sock < 0) |
| 32 |
{ |
| 33 |
perror ("socket"); |
| 34 |
exit (EXIT_FAILURE); |
| 35 |
} |
| 36 |
|
| 37 |
/* Give the socket a name. */ |
| 38 |
name.sin_family = AF_INET; |
| 39 |
name.sin_port = htons (port); |
| 40 |
name.sin_addr.s_addr = htonl (INADDR_ANY); |
| 41 |
if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) |
| 42 |
{ |
| 43 |
perror ("bind"); |
| 44 |
exit (EXIT_FAILURE); |
| 45 |
} |
| 46 |
|
| 47 |
return sock; |
| 48 |
} |
| 49 |
|
| 50 |
int |
| 51 |
read_from_client (int filedes) |
| 52 |
{ |
| 53 |
char buffer[MAXMSG]; |
| 54 |
int nbytes; |
| 55 |
|
| 56 |
nbytes = read (filedes, buffer, MAXMSG); |
| 57 |
if (nbytes < 0) |
| 58 |
{ |
| 59 |
/* Read error. */ |
| 60 |
perror ("read"); |
| 61 |
exit (EXIT_FAILURE); |
| 62 |
} |
| 63 |
else if (nbytes == 0) |
| 64 |
/* End-of-file. */ |
| 65 |
return -1; |
| 66 |
else |
| 67 |
{ |
| 68 |
/* Data read. */ |
| 69 |
fprintf (stderr, "Server: got message: `%s'\n", buffer); |
| 70 |
return 0; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
int |
| 75 |
main (void) |
| 76 |
{ |
| 77 |
int sock; |
| 78 |
fd_set active_fd_set, read_fd_set; |
| 79 |
int i; |
| 80 |
struct sockaddr_in clientname; |
| 81 |
socklen_t size; |
| 82 |
|
| 83 |
/* Create the socket and set it up to accept connections. */ |
| 84 |
sock = make_socket (PORT); |
| 85 |
if (listen (sock, 1) < 0) |
| 86 |
{ |
| 87 |
perror ("listen"); |
| 88 |
exit (EXIT_FAILURE); |
| 89 |
} |
| 90 |
|
| 91 |
/* Initialize the set of active sockets. */ |
| 92 |
FD_ZERO (&active_fd_set); |
| 93 |
FD_SET (sock, &active_fd_set); |
| 94 |
|
| 95 |
while (1) |
| 96 |
{ |
| 97 |
/* Block until input arrives on one or more active sockets. */ |
| 98 |
read_fd_set = active_fd_set; |
| 99 |
if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) |
| 100 |
{ |
| 101 |
perror ("select"); |
| 102 |
exit (EXIT_FAILURE); |
| 103 |
} |
| 104 |
|
| 105 |
/* Service all the sockets with input pending. */ |
| 106 |
for (i = 0; i < FD_SETSIZE; ++i) |
| 107 |
if (FD_ISSET (i, &read_fd_set)) |
| 108 |
{ |
| 109 |
if (i == sock) |
| 110 |
{ |
| 111 |
/* Connection request on original socket. */ |
| 112 |
int new; |
| 113 |
size = sizeof (clientname); |
| 114 |
new = accept (sock, |
| 115 |
(struct sockaddr *) &clientname, |
| 116 |
&size); |
| 117 |
if (new < 0) |
| 118 |
{ |
| 119 |
perror ("accept"); |
| 120 |
exit (EXIT_FAILURE); |
| 121 |
} |
| 122 |
fprintf (stderr, |
| 123 |
"Server: connect from host %s, port %hd.\n", |
| 124 |
inet_ntoa (clientname.sin_addr), |
| 125 |
ntohs (clientname.sin_port)); |
| 126 |
FD_SET (new, &active_fd_set); |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
/* Data arriving on an already-connected socket. */ |
| 131 |
if (read_from_client (i) < 0) |
| 132 |
{ |
| 133 |
close (i); |
| 134 |
FD_CLR (i, &active_fd_set); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |