コード例 #1
0
/*
 * 客户请求进行相应的处理
 * 并给予相应的响应
 */
void process_cli(int connfd ,struct sockaddr_in caddr)
{
	int num;
	char cli_data[MAXDATASIZE];
	char recvbuf[MAXDATASIZE],sendbuf[MAXDATASIZE],
		 cli_name[MAXDATASIZE];
	printf("You got a connection from %s.\n",inet_ntoa(caddr.sin_addr));
	num = recv(connfd,cli_name,MAXDATASIZE,0);
	if(num==0)
	{
		close(connfd);
		printf("Client disconnected\n");
		return ;
	}
	cli_name[num-1] = '\0';
	printf("Client's name is %s\n",cli_name);
	while(num=recv(connfd,recvbuf,MAXDATASIZE,0))
	{
		recvbuf[num] = '\0';
		printf("Recvived client (%s) message:%s",cli_name,recvbuf);
		//save user's data
		//savedata(recvbuf,num,cli_data); 
		savedata_r(recvbuf,num,cli_data);
		int i;
		for(i = 0;i<num;i++)
		{
			if((recvbuf[i]>='a'&&recvbuf[i]<='z')||(recvbuf[i]>='A'&&recvbuf[i]<='Z'))
			{
				recvbuf[i] = recvbuf[i]+3;
				if((recvbuf[i]>'Z'&&recvbuf[i]<'Z'+3)||(recvbuf[i]>'z'))
				{
					recvbuf[i] = recvbuf[i] - 26;
				}
			}
			sendbuf[i] = recvbuf[i];
		}
		sendbuf[num] = '\0';
		send(connfd,sendbuf,strlen(sendbuf),0);
	}
	printf("有客户退出%s:%d\n",inet_ntoa(caddr.sin_addr),ntohs(caddr.sin_port));
	printf("Client (%s) closed connection.\nUser's data:%s\n",cli_name,cli_data);
	close(connfd);
}
コード例 #2
0
ファイル: PRG6_7.C プロジェクト: anjsxz/ctest
	void process_cli(int connectfd, sockaddr_in client)
	{
	int num;
	char cli_data[5000];
	char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE], cli_name[MAXDATASIZE];

	printf("You got a connection from %s.  ",inet_ntoa(client.sin_addr) );   
	/* Get client's name from client */
	num = recv(connectfd, cli_name, MAXDATASIZE,0);
	if (num == 0) {
	   close(connectfd);
	   printf("Client disconnected.\n");
	   return;
	   }
	cli_name[num - 1] = '\0';
	printf("Client's name is %s.\n",cli_name);

	while (num = recv(connectfd, recvbuf, MAXDATASIZE,0)) {
	   recvbuf[num] = '\0';
	   printf("Received client( %s ) message: %s",cli_name, recvbuf);

	   /* save user's data */
	   savedata_r(recvbuf,num,cli_data);

	   /* reverse usr's data */
	   for (int i = 0; i < num - 1; i++) {
	      sendbuf[i] = recvbuf[num - i -2];
	      }
	   sendbuf[num - 1] = '\0';

	   send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */ 
	}

	close(connectfd); /*  close connectfd */ 
	printf("Client( %s ) closed connection. User's data: %s\n",cli_name,cli_data);
	}