7

Write a server in stream mode

  1. /*
  2.  * Simple stream echo server.
  3.  *
  4.  *   socket - AF_INET SOCK_STREAM
  5.  *   bind
  6.  *   listen
  7.  *   accept
  8.  *   read
  9.  *   write
  10.  *
  11.  *   sockaddr_in
  12.  */
  13.  
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #if defined( LINUX )
  22. #include <string.h>
  23. #endif
  24.  
  25. int open_socket( int portnum ) {
  26.     int sd = -1;
  27.     struct sockaddr_in sd_address;
  28.     socklen_t addrlen = sizeof (struct sockaddr_in);
  29.  
  30.     int sockoptval, sockoptlen;
  31.  
  32.     if ( (sd = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) == -1 )
  33.         goto error;
  34.  
  35.     sockoptval = 1; /* on */
  36.     sockoptlen = sizeof(sockoptval);
  37.     if ( setsockopt( sd, SOL_SOCKET, SO_REUSEADDR, &sockoptval, sockoptlen ) < 0 )
  38.         goto error;
  39.  
  40.     sd_address.sin_family = AF_INET;
  41.     sd_address.sin_addr.s_addr = htonl( INADDR_ANY );
  42.     sd_address.sin_port = htons( portnum );
  43.  
  44.     if ( bind( sd, (struct sockaddr *) &sd_address, addrlen ) != 0 )
  45.         goto error;
  46.  
  47.     if ( listen( sd, 5 ) != 0 )
  48.         goto error;
  49.  
  50.     return sd;
  51.  
  52. error:
  53.  
  54.     fprintf( stderr, "%s\n", strerror( errno ) );
  55.     if ( sd != -1 )
  56.         close( sd );
  57.     return -1;
  58. }
  59.  
  60. void echo_input( int socket ) {
  61.     int sd = -1;
  62.  
  63.     struct sockaddr_in sd_address;
  64.     socklen_t addrlen = sizeof(struct sockaddr_in);
  65.  
  66.     char buf[4096];
  67.     int n;
  68.  
  69.     sd = accept( socket, (struct sockaddr *) &sd_address, &addrlen );
  70.     if ( sd == -1 )
  71.         goto error;
  72.  
  73.     while ( (n = read( sd, buf, sizeof(buf) )) != 0 )
  74.         if ( write( sd, buf, n ) == -1 )
  75.             break;
  76.  
  77.     close( sd );
  78.  
  79.     return;
  80.  
  81. error:
  82.  
  83.     fprintf( stderr, "%s\n", strerror( errno ) );
  84.     if ( sd != -1 )
  85.         close( sd );
  86. }
  87.  
  88. int main( int argc, char *argv[] ) {
  89.     int portnum, n;
  90.     int socket, sd;
  91.  
  92.     switch ( argc ) {
  93.     case 2:
  94.         n = atoi( argv[1] );
  95.         if ( n == 0 )
  96.             goto usage;
  97.         portnum = n;
  98.         break;
  99.     default:
  100.         usage: fprintf( stderr, "%s portnum\n", argv[0] );
  101.         exit( 1 );
  102.     }
  103.  
  104.     if ( (socket = open_socket( portnum )) == -1 )
  105.         exit( 1 );
  106.  
  107.     echo_input( socket );
  108.  
  109.     close( socket );
  110.  
  111.     exit( 0 );
  112. }

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].