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

📄 threadserver.c

📁 thread编程
💻 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>#include <pthread.h>#include "ThreadServer.h"#define BACKLOG 20#define RECEIVE_BUFF_SIZE 1024*5char pLogName[100];int main( int argc , char ** argv ) {	int iSin_size;	int iSockfd;	int iClientfd;	int iRet;		int iServerPort;	struct sockaddr_in server_addr;	struct sockaddr_in client_addr;	pthread_t pThread;	pthread_attr_t  pThreadAttr;	if ( argc < 2 ) {		printf ( "Usage : %s parameter\n" , argv[0] );	}	iServerPort = atoi ( argv[1] );	snprintf ( pLogName , sizeof ( pLogName ) , "%s" , argv[2] );	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 ( 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 );	iRet = pthread_attr_init ( &pThreadAttr );	if ( iRet != 0 ) {		printf ( "pthread_attr_init error !\n" );		exit ( 1 );	}	iRet = pthread_attr_setscope ( &pThreadAttr , PTHREAD_SCOPE_SYSTEM );	if ( iRet != 0 ) {		printf ( "pthread_attr_setscope error !\n" );		exit ( 1 );				}	while ( 1 ) {		iSin_size = sizeof ( struct sockaddr_in );		iClientfd = accept ( iSockfd , ( struct sockaddr * )&client_addr , &iSin_size );		if ( iClientfd < 0 ) {			printf ( "Accept Error !\n" );		} else {			printf ( "Accept Success !\n" );			printf ( "Receive A Connection From %s\n" , inet_ntoa( client_addr.sin_addr ) );			iRet = pthread_create ( &pThread , &pThreadAttr , AcceptSuccess , &iClientfd );			if ( iRet != 0 ) {				printf ( "pthread_create error !\n" );			}		}	}}void * AcceptSuccess ( void * vClientfd ) {	int iLogin;	int iRet = -1;	int * iClientfd;	char sReceiveBuff[RECEIVE_BUFF_SIZE];	char sKeyBoardInput[RECEIVE_BUFF_SIZE];	char sPrintLogBuff[RECEIVE_BUFF_SIZE];	char * cLogin = "login :";	char * cPasswd = "passwd :";	char sLogin[50];	char sPasswd[50];	char * pLogin = "user";	char * pPasswd = "1234";	assert ( vClientfd );	iClientfd = ( int * ) vClientfd;	while ( 1 ) {		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 ) ;			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 ) {				close ( *iClientfd );				printf ( "Client Connection Closed !\n" );			}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 + -