| 1 |
/* Example from glibc manual (16.9.6). */ |
| 2 |
/* { dg-require-effective-target sockets } */ |
| 3 |
|
| 4 |
/* Needed on some targets until we have exception-handling working (PR 111475). */ |
| 5 |
/* { dg-additional-options "-fno-exceptions" } */ |
| 6 |
|
| 7 |
/* { dg-skip-if "" { hppa*-*-hpux* powerpc*-*-aix* } } */ |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <sys/types.h> |
| 15 |
#include <sys/socket.h> |
| 16 |
#include <netinet/in.h> |
| 17 |
#include <netdb.h> |
| 18 |
|
| 19 |
#define PORT 5555 |
| 20 |
#define MESSAGE "Yow!!! Are we having fun yet?!?" |
| 21 |
#define SERVERHOST "www.gnu.org" |
| 22 |
|
| 23 |
void |
| 24 |
write_to_server (int filedes) |
| 25 |
{ |
| 26 |
int nbytes; |
| 27 |
|
| 28 |
nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1); |
| 29 |
if (nbytes < 0) |
| 30 |
{ |
| 31 |
perror ("write"); |
| 32 |
exit (EXIT_FAILURE); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
int |
| 38 |
main (void) |
| 39 |
{ |
| 40 |
extern void init_sockaddr (struct sockaddr_in *name, |
| 41 |
const char *hostname, |
| 42 |
uint16_t port); |
| 43 |
int sock; |
| 44 |
struct sockaddr_in servername; |
| 45 |
|
| 46 |
/* Create the socket. */ |
| 47 |
sock = socket (PF_INET, SOCK_STREAM, 0); |
| 48 |
if (sock < 0) |
| 49 |
{ |
| 50 |
perror ("socket (client)"); |
| 51 |
exit (EXIT_FAILURE); |
| 52 |
} |
| 53 |
|
| 54 |
/* Connect to the server. */ |
| 55 |
init_sockaddr (&servername, SERVERHOST, PORT); |
| 56 |
if (0 > connect (sock, |
| 57 |
(struct sockaddr *) &servername, |
| 58 |
sizeof (servername))) |
| 59 |
{ |
| 60 |
perror ("connect (client)"); |
| 61 |
exit (EXIT_FAILURE); |
| 62 |
} |
| 63 |
|
| 64 |
/* Send data to the server. */ |
| 65 |
write_to_server (sock); |
| 66 |
close (sock); |
| 67 |
exit (EXIT_SUCCESS); |
| 68 |
} |