/* NetEcho Program * * On Hitachi-UX * cc -Aa -D_HIUX_SOURCE -o netecho netecho.c * * On WinNT/Win95 Win32 console mode using Microsoft VC++ 4.0 * cl -DWIN32 netecho.c wsock32.lib */ #include #include #ifdef WIN32 #define STRICT #include #include #else #include #include #include #include #include #endif #define SEND_BUFSIZE 256 #define RECV_BUFSIZE 512 #ifndef INADDR_NONE #define INADDR_NONE 0xffffffff /* should be in */ #endif #ifndef IPPORT_ECHO #define IPPORT_ECHO 7 /* should be in */ #endif #ifndef WIN32 typedef int SOCKET; #endif int netstart( void ) { #ifdef WIN32 WSADATA wsadata; WORD wsaversion; wsaversion = 0x0101; return( WSAStartup( wsaversion, &wsadata ) ); #else return(0); #endif } void netclose( SOCKET sockfd ) { #ifdef WIN32 closesocket( sockfd ); WSACleanup(); #else close( sockfd ); #endif return; } int netgetn( SOCKET sockfd, char *buf, int bufsize ) { int n, nrecv; char c; for( n = 0; n < (bufsize - 1); n++ ) { if( (nrecv = recv( sockfd, &c, 1, 0 )) == 1 ) { if( c == '\0' ) { *buf = '\0'; return (n + 1); } *buf++ = c; } else if( nrecv == 0 ) { /* EOF */ *buf = '\0'; return n; } else { /* Error */ return(-1); } } *buf = '\0'; return (n + 1); } int netputn( SOCKET sockfd, char *buf, int nput ) { int nleft, nsend; nleft = nput; while( nleft > 0 ) { nsend = send( sockfd, buf, nleft, 0 ); if( nsend <= 0 ) { /* Error */ return( nsend ); } nleft -= nsend; buf += nsend; } return( nput - nleft ); } void main( int argc, char *argv[] ) { unsigned long inaddr; /* 32bit Internet Address */ struct hostent *hp; SOCKET sockfd; /* Socket file descriptor */ struct sockaddr_in serv_addr; char sendbuf[SEND_BUFSIZE]; char recvbuf[RECV_BUFSIZE]; int nsend, nrecv, c; /* Check the number of arguments */ if( argc != 2 ) { fprintf( stderr, "Usage: %s hostaddress\n", argv[0] ); return; } /* Start network module */ if( netstart() != 0 ) { fprintf( stderr, "Cannot start the network.\n" ); return; } /* Clear the server address */ memset( (char *)&serv_addr, 0, sizeof( serv_addr ) ); /* Get the server address */ if( (inaddr = inet_addr( argv[1] )) != INADDR_NONE ) { /* OK, It's dotted-decimal format */ memcpy( (char *)&serv_addr.sin_addr, (char *)&inaddr, sizeof( inaddr ) ); } else { /* Try to get hostaddress by using gethostbyname */ if( (hp = gethostbyname( argv[1] )) == NULL ) { #ifdef WIN32 WSACleanup(); #endif fprintf( stderr, "Error to get the hostaddress of %s\n", argv[1] ); return; } memcpy( (char *)&serv_addr.sin_addr, hp->h_addr, hp->h_length ); printf( "IP address of %s is %s\n", argv[1], inet_ntoa( serv_addr.sin_addr ) ); } serv_addr.sin_port = htons( IPPORT_ECHO ); serv_addr.sin_family = AF_INET; /* Create TCP socket */ #ifdef WIN32 if( (sockfd = socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET ) { WSACleanup(); #else if( (sockfd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) { #endif fprintf( stderr, "Cannot create TCP socket\n" ); return; } /* Connet to the server */ if( connect( sockfd, (struct sockaddr *)&serv_addr, sizeof( serv_addr ) ) < 0 ) { netclose( sockfd ); fprintf( stderr, "Cannot connect to the server\n" ); return; } printf( "Connected to the host: %s\n", argv[1] ); nsend = 0; while( (c = fgetc( stdin )) != EOF ) { if( c == '\n' ) { sendbuf[nsend++] = 0; /* add NULL */ printf( "Send: %d-bytes: %s\n", nsend, sendbuf ); /* Send data including NULL */ if( netputn( sockfd, sendbuf, nsend ) != nsend ) { /* Error */ netclose( sockfd ); fprintf( stderr, "Send Error\n" ); return; } /* Clear the receive buffer */ memset( recvbuf, 0, RECV_BUFSIZE ); if( (nrecv = netgetn( sockfd, recvbuf, RECV_BUFSIZE )) <= 0 ) { /* Error */ netclose( sockfd ); fprintf( stderr, "Recv Error\n" ); return; } printf( "Recv: %d-bytes: %s\n", nrecv, recvbuf ); nsend = 0; } else { if( nsend < (SEND_BUFSIZE - 1) ) { sendbuf[nsend++] = c; } } } netclose( sockfd ); return; }