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

  1. /* isockt.c -- open an internet socket and talk into it */
  2. /* (C) 1991 Blair P. Houghton, All Rights Reserved, copying and */
  3. /* distribution permitted with copyright intact.        */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <math.h>
  11.  
  12. #ifdef __STDC__
  13. extern void    perror( char * );
  14. extern int    bind( int, struct sockaddr *, int );
  15. extern int    socket( int, int, int );
  16. extern int    write( int, char *, unsigned );
  17. extern char *    strcpy( char *, char *b );
  18. extern int    strlen( char * );
  19. extern void    exit( int );
  20. extern int    connect( int, struct sockaddr *, int );
  21. extern struct hostent *    gethostbyname( char * );
  22. extern int    fprintf( FILE *, char *, ... );
  23. extern int    atoi( char * );
  24. extern void    bcopy( char *, char *b, int );
  25. #endif
  26.  
  27. char *line[] = {
  28.     "Mary had a little lamb;\n",
  29.     "Its fleece was white as snow;\n",
  30.     "And everywhere that Mary went,\n",
  31.     "She told everyone that Edison invented\nthe telephone before Bell did.\n"
  32. };
  33. int n_line = 4;
  34.  
  35. /*
  36.  *  arg 0 is program name; arg 1 is remote host; arg 2 is
  37.  *  port number of listener on remote host
  38.  */
  39. #ifdef __STDC__
  40. void main( int argc, char *argv[] )
  41. #else
  42. main(argc,argv)
  43. int argc; char *argv[];
  44. #endif
  45. {
  46.  
  47.     int plug;                /* socket to "plug" into the socket */
  48.     struct sockaddr_in socketname;    /* mode, addr, and port data for the socket */
  49.     struct hostent *remote_host;    /* internet numbers, names */
  50.     extern int n_line;
  51.     extern char *line[];
  52.     char buf[BUFSIZ];
  53.     int i;
  54.  
  55.     /* make an internet-transmitted, file-i/o-style, protocol-whatever plug */
  56.     if ( (plug = socket( AF_INET, SOCK_STREAM, 0 )) < 0 )
  57.     perror(argv[0]);
  58.  
  59.     /* plug it into the listening socket */
  60.     socketname.sin_family = AF_INET;
  61.     if ( (remote_host = gethostbyname( argv[1] )) == (struct hostent *)NULL ) {
  62.     fprintf( stderr, "%s: unknown host: %s\n",
  63.          argv[0], argv[1] );
  64.     exit(__LINE__);
  65.     }
  66.     (void) bcopy( (char *)remote_host->h_addr, (char *) &socketname.sin_addr,
  67.           remote_host->h_length );
  68.     socketname.sin_port = htons(atoi(argv[2]));
  69.  
  70.     if ( connect( plug, (struct sockaddr *) &socketname,
  71.           sizeof socketname ) < 0 ) {
  72.     perror(argv[0]);
  73.     exit(__LINE__);
  74.     }
  75.  
  76.     /* wait for ack */
  77.     if ( read( plug, buf, sizeof buf ) > 0 )
  78.     printf(buf);
  79.  
  80.     /* say something into it; something historic */
  81.     for ( i = 0; i < n_line; i++ ) {
  82.     sleep(1);
  83.     if ( write( plug, line[i], strlen(line[i]) ) < 0 ) {
  84.         perror(argv[0]);
  85.         exit(__LINE__);
  86.     }
  87.     }
  88.     
  89.     /* all the socket connections are closed automatically */
  90.     exit(0);
  91. }
  92.