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

📄 scc.c

📁 在RedHad9 下实现文字界面的聊天程序。此源码示范了C语言的socket通信编码;RedHad下如何编译c代码。内附说明文档详细说明了socket通信原理和代码解释。
💻 C
字号:
#include "scc.h"

void sendm();
void getm();
char* getMessage(char* sendline,int len,FILE * fp);
int IsCommand(char sendline[]);

int sendMsg(int sockfd, char sendline[], char passwd[]);
void myencrypt(char sendbuf[], char passwd[]);
void mydecrypt(char sendbuf[], char passwd[]);

int main(int argc,char * argv[])
{
	int fd ;
	struct hostent * he;
	struct sockaddr_in server;
	pthread_t thread;
	int isSecure,port;
	char hostname[HOSTSIZE];
	char nickname[NAMESIZE];

	isSecure=1;
	port=0;
	
	if(checkParam(argc,argv,hostname,&isSecure,&port,nickname)==0)
	{
		exit(1);
	}
	printf("isSecure:%d\tnickname:%s,\tport:%d\thostname:%s\n",isSecure,nickname,port,hostname);

	//通过字符串形式的IP地址获取服务器的地址信息
	if((he=gethostbyname(hostname))==NULL)
	{
		printf("gethostbyname() error\n");
		exit(1);
	}

	//产生TCP套接字
	if((fd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
		printf("socket() error\n");
		exit(1);
	}

	bzero(&server,sizeof(server));
	server.sin_family=AF_INET;
	server.sin_port=htons((port==0)?PORT:port);
	server.sin_addr=*((struct in_addr * )he->h_addr );

	//连接到服务器
	if(connect(fd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1)
	{
		printf("connect() error\n");
		exit(1);
	}
	fp = stdin;
	sockfd = fd;
	
	//开线程,接收服务器发来的信息
	if(pthread_create(&thread,NULL,getm,(void*)NULL))
	{
		perror("Pthread_create() error");
		exit(1);			
	}	
	//获取用户的输入
	sendm(nickname,isSecure);

	close(fd);
}

void sendm(char nickname[],int isSecure)
{
	char sendline[MAXDATASIZE],recvline[MAXDATASIZE];
	int numbytes;

	strcpy(sendline,nickname);

	printf("Connected to server. \n");
	send(sockfd,sendline,strlen(sendline),0);
	
//************* password ***********
	if(isSecure==1)
	{
		GetPasswd(passwd, sendline);		
		passwd[strlen(passwd)]='\0';                 //delete crlf
		send(sockfd,sendline,strlen(sendline),0);	
	}
	else
	{	
		sleep(1);	
		printf("You can write your message now!\n");
		strcpy(passwd,STRPWD);
		passwd[strlen(passwd)]='\0';      
		bzero(sendline,sizeof(sendline));
		strcat(sendline,"/passwd ");
		strcat(sendline,passwd);
		send(sockfd,sendline,strlen(sendline),0);	
	}
//*****************************

	while(getMessage(sendline,MAXDATASIZE,fp)!=NULL)
	{
		sendline[strlen(sendline)-1]='\0';
		if( IsCommand(sendline)==0)			
		{	
			sendMsg(sockfd, sendline, passwd);	// Only the input that is not command can be sent !
		}
		bzero(sendline,sizeof(bzero));
	}
	printf("\nExit.\n");
}

void getm()
{
	char recvline[MAXDATASIZE];
	int numbytes;

	while((numbytes=recv(sockfd,recvline,MAXDATASIZE,0))!=0)
	{
		recvline[numbytes]='\0';
		if(isPasswd==2)
		{
			 mydecrypt(recvline, passwd);
			 printf("%s\n",recvline);
		}
		else
		{
			printf("%s\n",recvline);
			isPasswd=2;
		}
		if (isLog==1 && strLogFile!=NULL)
		{
			LogFile(recvline, strLogFile);
		}	
	}
	printf("Server terminated.\n");
	return;
}

char* getMessage(char* sendline,int len,FILE* fp)
{
	return(fgets(sendline,MAXDATASIZE,fp));
}

int IsCommand(char sendline[])
{
	int i;
	char combuf[COMMANDSIZE];
	char * tokenPtr;
	strcpy(combuf,sendline);
	combuf[strlen(combuf)-1]='\0';

	tokenPtr=(char *)strtok(combuf," ");
	while(tokenPtr != NULL)
	{
		if(!strcmp(tokenPtr,"/log"))
		{
			tokenPtr=(char *)strtok(NULL," ");
			if(tokenPtr != NULL)
			{					
				strcpy(strLogFile,tokenPtr);
				strLogFile[strlen(strLogFile)]='\0';
				isLog = 1;
				return 1;
			}
			else
			{
				printf("Command error !");
				return 0;
			}
		}
		tokenPtr=(char *)strtok(NULL," ");
	}
	return 0;
}

int sendMsg(int sockfd, char sendline[], char passwd[])
{
	if(strlen(passwd)>=0)
	{
		myencrypt(sendline, passwd);
		send(sockfd,sendline,strlen(sendline),0);	
	}
	else
	{
		send(sockfd,sendline,strlen(sendline),0);	
	}
}

void myencrypt(char sendbuf[], char passwd[])
{
	int i;
	int buflength;
	int pwdlength;
	buflength = strlen(sendbuf);
	pwdlength = strlen(passwd);
//printf("\n--%d--%s--\n",pwdlength, passwd);
	for( i=0; i<buflength ; i++)
	{
		sendbuf[i]=sendbuf[i]+passwd[i%pwdlength];
	}	
}

void mydecrypt(char sendbuf[], char passwd[])
{
	int i;
	int buflength;
	int pwdlength;
	buflength = strlen(sendbuf);
	pwdlength = strlen(passwd);
//printf("\n--%d--%s--\n",pwdlength, passwd);
	for( i=0; i<buflength ; i++)
	{
		sendbuf[i]=sendbuf[i]-passwd[i%pwdlength];
	}
}

⌨️ 快捷键说明

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