4

Write a server in unix mode

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

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].