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

📄 socketserver.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 SERVER_PORT 6666#define BACKLOG 20#define RECEIVE_BUFF_SIZE 1024*5int main( int argc , char ** argv ) {	int iSin_size;	int iSockfd;	//int iSockfd = -1;	int iClientfd;	int iRet = -1;		int iServerPort;	struct sockaddr_in server_addr;	struct sockaddr_in client_addr;	char * cLogin = "login :";	char * cPasswd = "passwd :";	char sLogin[50];	char sPasswd[50];	char * pLogin = "user";	char * pPasswd = "1234";	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] );	}	iServerPort = atoi ( argv[1] );	snprintf ( pLogName , sizeof ( pLogName ) , "%s" , argv[2] );//	while ( iSockfd == -1 ) {	iSockfd = socket ( AF_INET , SOCK_STREAM , 0 );	if ( iSockfd == -1 ) {		printf ( "Socket Found error !\n" );		exit ( 1 );	}		printf ( "Socket Found Success !\n" );//	}	server_addr.sin_family = AF_INET;	//server_addr.sin_port = htons ( SERVER_PORT );	server_addr.sin_port = htons ( iServerPort );	server_addr.sin_addr.s_addr = htonl ( INADDR_ANY );		bzero ( & ( server_addr.sin_zero ) , 8 );	if ( bind ( iSockfd , ( struct sockaddr * )&server_addr , sizeof ( struct sockaddr ) ) == -1 ) {		printf ( "Bind Error !\n" );		exit ( 1 );	}	printf ( "Bind Success !\n" );	if ( listen ( iSockfd , BACKLOG ) == -1 ) {		printf ( "Listen Error !\n" );		exit ( 1 );	}	printf ( "Listen Success !\n" );	printf ( "Listen Port : %d\n" , iServerPort );	while ( 1 ) {		iSin_size = sizeof ( struct sockaddr_in );		if ( ( iClientfd = accept ( iSockfd , ( struct sockaddr * )&client_addr , &iSin_size ) ) == -1 ) {			printf ( "Accept Error !\n" );		}else {			printf ( "Accept Success !\n" );			printf ( "Receive A Connection From %s\n" , inet_ntoa( client_addr.sin_addr ) );			iLogin = 1;			while ( iLogin == 1 ) {				iRet = -1;				while ( iRet == -1 ) {					iRet = send ( iClientfd , cLogin , strlen( cLogin ) , 0 );					if ( iRet == -1 ) {						printf ( "Send Error !\n" );					}				}				iRet = -1;						while ( iRet == -1 || iRet == 0 ) {					memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) );					iRet = recv ( iClientfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 ); 					if ( iRet == -1 ) {						printf ( "Receive Error !\n" );					}				}				strcpy ( sLogin , sReceiveBuff ) ;				iRet = -1;				while ( iRet == -1 ) {					iRet = send ( iClientfd , cPasswd , strlen( cPasswd ) , 0 );					if ( iRet == -1 ) {						printf ( "Send Error !\n" );					}				}				iRet = -1;				while ( iRet == -1 || iRet == 0 ) {					memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) );					iRet = recv ( iClientfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 ); 					if ( iRet == -1 ) {						printf ( "Receive Error !\n" );					}				}				strcpy ( sPasswd , sReceiveBuff ) ;				//memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) );				if ( ( strcmp ( sLogin , pLogin ) == 0 ) && (strcmp ( sPasswd , pPasswd ) == 0 ) ) {					iRet = send ( iClientfd , "Login Success !" , 16 , 0 );					if ( iRet == -1 ) {						printf ( "Send Error !\n" );					}					iLogin = 0;				} else {					iRet = send ( iClientfd , "Login Failure !" , 16 , 0 );					iLogin = 1;					iRet = -1;				}			}			while ( 1 ) {				memset ( sReceiveBuff , 0 , sizeof ( sReceiveBuff ) );				iRet = recv ( iClientfd , sReceiveBuff , RECEIVE_BUFF_SIZE , 0 );					if ( iRet == -1 ) {					printf ( "Receive Error !\n" );				}else if ( iRet == 0 ) {					printf ( "Client Closed !\n" );					close ( iClientfd );					break;				}else {					printf ( "Client Message : %s\n" , sReceiveBuff );					memset ( sPrintLogBuff , 0 , sizeof ( sPrintLogBuff ) );					snprintf ( sPrintLogBuff , sizeof ( sKeyBoardInput ) , "Client Message : %s\n" , sReceiveBuff );					iWriteLog ( pLogName , sPrintLogBuff );				}				printf ( "Server Message : " );				scanf ( "%s" , &sKeyBoardInput ); //printf ( "KEY = %s\n" , sKeyBoardInput );				memset ( sPrintLogBuff , 0 , sizeof ( sPrintLogBuff ) );				snprintf ( sPrintLogBuff , sizeof ( sReceiveBuff ) , "Server Message : %s\n" , sKeyBoardInput );				iWriteLog ( pLogName , sKeyBoardInput );				iRet = send ( iClientfd , sKeyBoardInput , strlen( sKeyBoardInput ) , 0 );				if ( iRet == -1 ) {					printf ( "Send Error !\n" );				}			}		}	}}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 + -