Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. #include    <sys/types.h>
  2. #include    <stdio.h>
  3. #include    <stdlib.h>
  4. #include    <unistd.h>
  5. #include    <errno.h>
  6. #include    <string.h>
  7. #include    <sys/socket.h>
  8. #include    <netdb.h>
  9. #include    <pthread.h>
  10.  
  11. // Miniature server to exercise getaddrinfo(2).
  12.  
  13. int
  14. claim_port( const char * port )
  15. {
  16.     struct addrinfo     addrinfo;
  17.     struct addrinfo *   result;
  18.     int         sd;
  19.     char            message[256];
  20.     int         on = 1;
  21.  
  22.     addrinfo.ai_flags = AI_PASSIVE;     // for bind()
  23.     addrinfo.ai_family = AF_INET;       // IPv4 only
  24.     addrinfo.ai_socktype = SOCK_STREAM; // Want TCP/IP
  25.     addrinfo.ai_protocol = 0;       // Any protocol
  26.     addrinfo.ai_canonname = NULL;
  27.     addrinfo.ai_addrlen = 0;
  28.     addrinfo.ai_addr = NULL;
  29.     addrinfo.ai_canonname = NULL;
  30.     addrinfo.ai_next = NULL;
  31.     if ( getaddrinfo( 0, port, &addrinfo, &result ) != 0 )      // want port 3000
  32.     {
  33.         fprintf( stderr, "\x1b[1;31mgetaddrinfo( %s ) failed errno is %s.  File %s line %d.\x1b[0m\n", port, strerror( errno ), __FILE__, __LINE__ );
  34.         return -1;
  35.     }
  36.     else if ( errno = 0, (sd = socket( result->ai_family, result->ai_socktype, result->ai_protocol )) == -1 )
  37.     {
  38.         write( 1, message, sprintf( message, "socket() failed.  File %s line %d.\n", __FILE__, __LINE__ ) );
  39.         freeaddrinfo( result );
  40.         return -1;
  41.     }
  42.     else if ( setsockopt( sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) ) == -1 )
  43.     {
  44.         write( 1, message, sprintf( message, "setsockopt() failed.  File %s line %d.\n", __FILE__, __LINE__ ) );
  45.         freeaddrinfo( result );
  46.         close( sd );
  47.         return -1;
  48.     }
  49.     else if ( bind( sd, result->ai_addr, result->ai_addrlen ) == -1 )
  50.     {
  51.         freeaddrinfo( result );
  52.         close( sd );
  53.         write( 1, message, sprintf( message, "\x1b[2;33mBinding to port %s ...\x1b[0m\n", port ) );
  54.         return -1;
  55.     }
  56.     else if ( listen( sd, 100 ) == -1 )
  57.     {
  58.         printf( "listen() failed in file %s line %d\n", __FILE__, __LINE__ );
  59.         close( sd );
  60.         return 0;
  61.     }
  62.     else
  63.     {
  64.         write( 1, message, sprintf( message,  "\x1b[1;32mSUCCESS : Bind to port %s\x1b[0m\n", port ) );
  65.         freeaddrinfo( result );    
  66.         return sd;          // bind() succeeded;
  67.     }
  68. }
  69.  
  70. int
  71. main( int argc, char ** argv )
  72. {
  73.     int         sd;
  74.     char            message[256];
  75.     socklen_t       ic;
  76.     int         fd;
  77.     struct sockaddr_in      senderAddr;
  78.     char            request[2048];
  79.     char            temp;
  80.     int         i;
  81.     int         limit, size;
  82.     int         ignore;
  83.  
  84.     if ( argc < 2 )
  85.     {
  86.         fprintf( stderr, "\x1b[1;31mMust specify port number on command line.  File %s line %d.\x1b[0m\n", __FILE__, __LINE__ );
  87.         exit( 1 );
  88.     }
  89.     else if ( sscanf( argv[1], "%d", &ignore ) == 0 )
  90.     {
  91.         fprintf( stderr, "\x1b[1;31mMust specify port number as integerr on command line.  File %s line %d.\x1b[0m\n", __FILE__, __LINE__ );
  92.         exit( 1 );
  93.     }
  94.     else if ( (sd = claim_port( argv[1] )) == -1 )
  95.     {
  96.         write( 1, message, sprintf( message,  "\x1b[1;31mCould not bind to port %s errno %s\x1b[0m\n", "3000", strerror( errno ) ) );
  97.         return 1;
  98.     }
  99.     else
  100.     {
  101.         ic = sizeof(senderAddr);
  102.         while ( (fd = accept( sd, (struct sockaddr *)&senderAddr, &ic )) != -1 )
  103.         {
  104.             while ( read( fd, request, sizeof(request) ) > 0 )
  105.             {
  106.                 printf( "server receives input:  %s\n", request );
  107.                 size = strlen( request );
  108.                 limit = strlen( request ) / 2;
  109.                 for ( i = 0 ; i < limit ; i++ )
  110.                 {
  111.                     temp = request[i];
  112.                     request[i] = request[size - i - 1];
  113.                     request[size - i - 1] = temp;
  114.                 }
  115.                 write( fd, request, strlen(request) + 1 );
  116.             }
  117.             close( fd );
  118.         }
  119.         printf( "No longer accepting incoming connections file %s line %d\n", __FILE__, __LINE__ );
  120.         close( sd );
  121.         return 0;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement