2.9
[glibc/nacl-glibc.git] / manual / examples / inetcli.c
blob35dfb379e83b06394b69e94c2b7e48fa2d4e8f4a
1 #include <stdio.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 #include <netdb.h>
10 #define PORT 5555
11 #define MESSAGE "Yow!!! Are we having fun yet?!?"
12 #define SERVERHOST "mescaline.gnu.org"
14 void
15 write_to_server (int filedes)
17 int nbytes;
19 nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
20 if (nbytes < 0)
22 perror ("write");
23 exit (EXIT_FAILURE);
28 int
29 main (void)
31 extern void init_sockaddr (struct sockaddr_in *name,
32 const char *hostname,
33 uint16_t port);
34 int sock;
35 struct sockaddr_in servername;
37 /* Create the socket. */
38 sock = socket (PF_INET, SOCK_STREAM, 0);
39 if (sock < 0)
41 perror ("socket (client)");
42 exit (EXIT_FAILURE);
45 /* Connect to the server. */
46 init_sockaddr (&servername, SERVERHOST, PORT);
47 if (0 > connect (sock,
48 (struct sockaddr *) &servername,
49 sizeof (servername)))
51 perror ("connect (client)");
52 exit (EXIT_FAILURE);
55 /* Send data to the server. */
56 write_to_server (sock);
57 close (sock);
58 exit (EXIT_SUCCESS);