home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mlddc320.zip / netio.c < prev    next >
C/C++ Source or Header  |  1997-12-16  |  813b  |  37 lines

  1. /*  Copyright 1997 Artur Skawina <skawina@usa.net>
  2.  *  All Rights Reserved.
  3.  */
  4.  
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <netdb.h>
  10. #include <string.h>
  11.  
  12. #include "netio.h"
  13.  
  14. int Connect( const char *hostname, const int port )
  15. {
  16.    struct sockaddr_in sin = {0};
  17.  
  18.    struct hostent   *phe;
  19.    int              sock;
  20.  
  21.    sin.sin_family = AF_INET;
  22.    sin.sin_port   = htons( port );
  23.  
  24.    if ( !(phe = gethostbyname( hostname )) )
  25.       fatalerr( "DNS error (gethostbyname)" );
  26.  
  27.    memcpy( (char *)&sin.sin_addr, phe->h_addr, phe->h_length );
  28.  
  29.    if ( (sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == -1 )
  30.       fatalerr( "socket" );
  31.  
  32.    if ( connect( sock, (struct sockaddr *)&sin, sizeof(sin)) == -1 )
  33.       fatalerr( "connect");
  34.  
  35.    return sock;
  36. }
  37.