home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / sockdemo / sockl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  3.0 KB  |  102 lines

  1. /* sockl.c -- set up a UNIX STREAM socket and listen on it */
  2. /* (C) 1991 Blair P. Houghton, All Rights Reserved, copying and */
  3. /* distribution permitted with copyright intact.        */
  4.  
  5. /* a stream (a socket opened using SOCK_STREAM) requires the use of
  6. listen() and accept() in a receiver, and connect() in a sender */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/un.h>
  12. #include <fcntl.h>
  13. /* include sockl.h only after <sys/socket.h> */
  14. #include "sockl.h" 
  15.  
  16. #ifdef __STDC__
  17. extern void    exit( int );
  18. extern void    perror( char * );
  19. extern int    printf( char *, ... );
  20. extern int    bind( int, struct sockaddr *, int );
  21. extern int    socket( int, int, int );
  22. extern int    read( int, char *, unsigned );
  23. extern char *    strcpy( char *, char *b );
  24. extern int    fcntl( int, int, int );
  25. extern int    accept( int, struct sockaddr *, int * );
  26. extern int    listen( int, int );
  27. extern int    unlink( char * );
  28.  
  29. void main( int argc, char *argv[] )
  30. #else
  31. main(argc,argv)
  32. int argc; char *argv[];
  33. #endif
  34. {
  35.     int sock;                /* fd for the actual socket */
  36.     int ear;                /* fd for the working socket */
  37.     struct sockaddr_un sockaddr;    /* sytem's location of the socket */
  38.     char buf[BUFSIZ];
  39.     int read_ret;
  40.     int f_ret;
  41.  
  42.     /*
  43.      *  open a unix (local) socket, using stream (file-style i/o)
  44.      *  mode, with protocol irrelevant ( == 0 ) (the protocol is
  45.      *  generally determined by the connection style: tcp for stream,
  46.      *  udp for datagrams, but this is not immutable nor all the
  47.      *  protocols for these styles).
  48.      */
  49.     if ( (sock = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) {
  50.     char s[BUFSIZ];
  51.     sprintf( s, "%s: can't assign fd for socket", argv[0] );
  52.     perror(s);
  53.     exit(__LINE__);
  54.     }
  55.  
  56.     /* place a filename in the filesystem for other processes to find */
  57.     sockaddr.sun_family = AF_UNIX;
  58.     (void) strcpy( sockaddr.sun_path, SOCKET_PATH_NAME );
  59.  
  60.     if ( bind( sock, (struct sockaddr *) &sockaddr, sizeof sockaddr ) < 0 ) {
  61.     char s[BUFSIZ];
  62.     sprintf( s, "%s: can't bind socket (%d) to pathname (%s)",
  63.         argv[0], sock, sockaddr.sun_path );
  64.     perror(s);
  65.     exit(__LINE__);
  66.     }
  67.  
  68.     printf("opened socket as fd (%d) at path (%s) for stream i/o\n",
  69.         sock, sockaddr.sun_path);
  70.  
  71.     /* put an ear to the socket, listening for a knock-knock-knocking */
  72.     listen( sock, 1 );                /* 1: only one queue slot */
  73.     /* ear will be a temporary (non-reusable) socket different from sock */
  74.     if ( (ear = accept( sock, (struct sockaddr *)NULL, (int *)NULL )) < 0 ) {
  75.     perror(argv[0]);
  76.     exit(__LINE__);
  77.     }
  78.  
  79.     /* read lines and print until the stream closes */
  80.     while ( (read_ret = read( ear, buf, sizeof buf )) > 0 )
  81.     printf( "\n%s: read from socket as follows:\n(%s)", argv[0], buf ); 
  82.  
  83.     if ( read_ret < 0 ) {
  84.     char s[BUFSIZ];
  85.     sprintf( s, "%s: error reading socket", argv[0] );
  86.     perror(s);
  87.     exit(__LINE__);
  88.     }
  89.  
  90.    /*
  91.     *  lots of things will close automatically: sock, ear;
  92.     *  but, this one must be done cleanly:
  93.     */
  94.  
  95.     /* rm filename */
  96.     unlink( SOCKET_PATH_NAME );
  97.  
  98.     /* loop ended normally:  read() returned NULL */
  99.     exit(0);
  100. }
  101.  
  102.