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