📄 test-client.c
字号:
/*** Copyright (C) 2006 Thai Computational Linguistics Laboratory (TCL)** National Institute of Information and Communications Technology (NICT)** Canasai Kruengkrai <canasai xx gmail yy com, where xx=at and yy=dot>**** This file is part of the `libs' library.**** This library is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h> // definitions of a number of data types used in system calls#include <sys/socket.h> // definitions of structures needed for sockets#include <netinet/in.h> // constants and structures needed for internet domain addresses#include <sys/signal.h>#include <netdb.h> // variable server is a pointer to a structure of type hostentvoid error( char *msg ){ perror( msg ); exit( 1 );}int main(int argc, char *argv[]){ int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[1024]; if( argc < 4 ) { fprintf( stderr, "usage: %s hostname port 'STRING'\n", argv[0] ); exit( 0 ); } portno = atoi( argv[2] ); if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) error( "ERROR opening socket" ); if( ( server = gethostbyname( argv[1] ) ) == NULL ) { fprintf( stderr,"ERROR, no such host\n" ); exit( 0 ); } if( strlen( argv[2] ) > 1024 ) { fprintf( stderr,"ERROR, input string too long (> 1024)\n" ); exit( 0 ); } bzero( ( char * )&serv_addr, sizeof( serv_addr ) ); serv_addr.sin_family = AF_INET; bcopy( ( char * )server->h_addr, ( char * )&serv_addr.sin_addr.s_addr, server->h_length ); serv_addr.sin_port = htons( portno ); if( connect( sockfd, ( struct sockaddr * )&serv_addr, sizeof( serv_addr ) ) < 0 ) error( "ERROR connecting" ); fprintf( stderr, "# input string: `%s'\n", argv[3] ); bzero( buffer, 1024 ); strcpy( buffer, argv[3] ); n = write( sockfd, buffer, strlen( buffer ) ); if( n < 0 ) error( "ERROR writing to socket" ); bzero( buffer, 1024 ); n = read( sockfd, buffer, 255 ); if( n < 0 ) error( "ERROR reading from socket" ); printf( "# client receives: `%s'\n", buffer ); return( 0 );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -