⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketclient.c

📁 TCP协议下套接字编程
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/stat.h>#include <sys/time.h>#include <assert.h>#include <fcntl.h>#include <netinet/in.h>#include <netdb.h>#include <assert.h>#include <sys/errno.h>#include <errno.h>#include <strings.h>#include <string.h>#include <netinet/in.h>#include <arpa/inet.h>#include <signal.h>#include <unistd.h>#define BACKLOG 20#define RECEIVE_BUFF_SIZE 1024*5int main( int argc , char ** argv ) {	int iSin_size;	//int iSockfd;	int iServerfd;	int iRet;		char sServerIp[20];	int iServerPort;	struct hostent * host;	struct sockaddr_in server_addr;	char * pLogin = "login :";	char * pPasswd = "passwd :";	char sLogin[50];	char sPasswd[50];	int iLogin = 1;	char sReceiveBuff[RECEIVE_BUFF_SIZE];	char sKeyBoardInput[RECEIVE_BUFF_SIZE];		char sPrintLogBuff[RECEIVE_BUFF_SIZE];	char pLogName[100];	if ( argc < 2 ) {		printf ( "Usage : %s parameter\n" , argv[0] );	}	if ( ( host = gethostbyname ( argv[1] ) ) == NULL ) {		printf ( "Gethostbyname Error !\n" );		exit ( 1 );	}	snprintf ( sServerIp , sizeof ( sServerIp ) , "%s" , argv[1] );	iServerPort = atoi ( argv[2] );	snprintf ( pLogName , sizeof ( pLogName ) , "%s" , argv[3] );	iServerfd = socket ( AF_INET , SOCK_STREAM , 0 );	if ( iServerfd == -1 ) {		printf ( "Socket Found error !\n" );		exit ( 1 );	}		printf ( "Socket Found Success !\n" );	server_addr.sin_family = AF_INET;	server_addr.sin_port = htons ( iServerPort );	server_addr.sin_addr.s_addr = inet_addr ( sServerIp );	//server_addr.sin_addr.s_addr = htonl (* ( unsigned int * ) * host->h_addr_list );	bzero ( & ( server_addr.sin_zero ) , 8 );while ( 1 ) {	iSin_size = sizeof ( server_addr );	if ( connect ( iServerfd , ( struct sockaddr * ) &server_addr , iSin_size ) == -1 ) {		printf ( "Connect Error !\n" );		sleep ( 1 );		continue;	}	printf ( "Connect Ip : %s\nPort : %d\n" , sServerIp , iServerPort );	while ( 1 ) {		iLogin = 1;		while ( iLogin == 1 ) {			iRet = -1;			while ( iRet == -1 || iRet == 0 ) {				memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) );				iRet = recv ( iServerfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 ); 				if ( iRet == -1 ) {					printf ( "Receive Error !\n" );				}			}			strcpy ( sLogin , sReceiveBuff ) ;			if ( strcmp ( pLogin , sLogin ) != 0 ) {				break;			}else {				iRet = -1;				printf ( "%s" , sLogin );				scanf ( "%s" , &sKeyBoardInput );				while ( iRet == -1 ) {					iRet = send ( iServerfd , sKeyBoardInput , strlen( sKeyBoardInput ) , 0 );					if ( iRet == -1 ) {						printf ( "Send Error !\n" );					}				}				iRet = -1;				while ( iRet == -1 || iRet == 0 ) {					memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) );					iRet = recv ( iServerfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 ); 					if ( iRet == -1 ) {						printf ( "Receive Error !\n" );					}				}				strcpy ( sPasswd , sReceiveBuff ) ;				if ( strcmp ( pPasswd , sPasswd ) != 0 ) {					break;				}else { 					iRet = -1;					printf ( "%s" , sPasswd );					scanf ( "%s" , &sKeyBoardInput );					while ( iRet == -1 ) {						iRet = send ( iServerfd , sKeyBoardInput , strlen( sKeyBoardInput ) , 0 );						if ( iRet == -1 ) {							printf ( "Send Error !\n" );						}					}				}			}			iRet = -1;			while ( iRet == -1 || iRet == 0 ) {				memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) );				iRet = recv ( iServerfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 ); 				if ( iRet == -1 ) {					printf ( "Receive Error !\n" );				}			}			if ( strcmp ( sReceiveBuff , "Login Failure !" ) == 0 ) {				printf ( "aaaaaaaaaaa\n" );				printf ( "%s\n" , sReceiveBuff );			}else {				printf ( "%s\n" , sReceiveBuff );				break;			}		}		memset ( sPrintLogBuff , 0 , sizeof ( sPrintLogBuff ) );		while ( 1 ) {			printf ( "Client Message : " );			scanf ( "%s" , &sKeyBoardInput ); //printf ( "KEY = %s\n" , sKeyBoardInput );			snprintf ( sPrintLogBuff , sizeof ( sPrintLogBuff ) , "Client Message : %s\n" , sKeyBoardInput );			iWriteLog ( pLogName , sPrintLogBuff );			iRet = send ( iServerfd , sKeyBoardInput , strlen( sKeyBoardInput ) , 0 );			if ( iRet == -1 ) {				printf ( "Send Error !\n" );			}			memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) );			iRet = recv ( iServerfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 );				if ( iRet == -1 ) {				printf ( "Receive Error !\n" );			}else if ( iRet == 0 ) {				printf ( "Server Closed !\n" );				close ( iServerfd );				exit ( 1 );			}else {				printf ( "Server Message : %s\n" , sReceiveBuff );				memset ( sPrintLogBuff , 0 , sizeof ( sPrintLogBuff ) );				snprintf ( sPrintLogBuff , sizeof ( sPrintLogBuff ) , "Server Message : %s\n" , sReceiveBuff );				iWriteLog ( pLogName , sPrintLogBuff );			}		}	}}}int iWriteLog (char * pFileName, char * pLogBuff){	int iOpenfd;	int iWriteNum;	iOpenfd = open ( pFileName , O_APPEND|O_RDWR|O_CREAT , S_IRWXU );	iWriteNum = write (iOpenfd , pLogBuff , strlen ( pLogBuff ) );	return iWriteNum;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -