关闭

一个服务器与两个客户端的通信

标签: c语言tcp
637人阅读 评论(0) 收藏 举报
分类:
创建两个客户端A,B,一个服务器。
要求:客户端A 传小写字母给服务器,服务器接收后转换成大写字母回传给客户端B。

效果:客户端A处循环输入一个字母,客户端B输出一个大写字母。(可以添加说明printf()输入为 / 转换后输出为)

/*********************************************************************
File Name:               tcp_net_socket.h
Author:                 xxx        date:
Description:            
Fuction List:			
********************************************************************/

#ifndef _TCP_NET_SOCKET_H
#define _TCP_NET_SOCKET_H

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>

#define SERV_PORT 9000

extern int tcp_init();
extern int tcp_accept(int sfd);
extern int tcp_connet();
extern void signalhandler(void);

#endif


/*********************************************************************
File Name:               tcp_net_socket.c
Author:                  xxx        date:
Description:            
Fuction List:			int tcp_init() 							//用于初始化操作
						int tcp_accept(int sfd)					//用于服务器的接收
						int tcp_connect(const char* ip)			//用于客户端的连接
						void signalhandler(void)				//用于信号处理,让服务器在按下Ctrl+c或Ctrl+\时不会退出
********************************************************************/

#include "tcp_net_socket.h"

//用于初始化操作
int tcp_init()  
{
	int sfd = socket(AF_INET, SOCK_STREAM, 0);     //创建套接字
	if(sfd == -1)
	{
		perror("socket");
		return -1;
	}
	
	int ret;
	struct sockaddr_in serveraddr;

	memset(&serveraddr,0,sizeof(struct sockaddr));	
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(SERV_PORT);
	serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
	
	ret = bind(sfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("bind");
		return -1;
	}
	
	ret = listen(sfd,10);           //监听它,并设置允许最大的连接数为10个
	if(ret == -1)
	{
		perror("listen");
		close(sfd);
		return -1;
	}
	
	return sfd;
}

//用于服务器的接收
int tcp_accept(int sfd)
{
	struct sockaddr_in clientaddr;
	memset(&clientaddr, 0, sizeof(struct sockaddr));
	int addrlen = sizeof(struct sockaddr);
	
	//sfd接受客户端的连接,并创建新的socket为new_fd,将请求连接的客户端的ip、port保存在结构体clientaddr中
	int new_fd = accept(sfd, (struct sockaddr*)&clientaddr, &addrlen);       
	if(new_fd == -1)
	{
		perror("accept");
		close(sfd);
		return -1;
	}
	printf("%s %d success connet...\n", 
	inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));
	
	return new_fd;
}

//用于客户端的连接
int tcp_connect(const char* ip)
{
	int ret;
	int sfd = socket(AF_INET, SOCK_STREAM, 0);     //申请新的socket
	if(sfd == -1)
	{
		perror("socket");
		return -1;
	}
	
	struct sockaddr_in serveraddr;
	
	memset(&serveraddr, 0,sizeof(struct sockaddr));
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(SERV_PORT);
	serveraddr.sin_addr.s_addr = inet_addr(ip);    
	
	ret = connect(sfd, (struct sockaddr*)&serveraddr, sizeof(struct sockaddr));       //将sfd连接至指定的服务器网络地址 serveraddr
	if(ret == -1)
	{
		perror("connect");
		close(sfd);
		return -1;
	}
	
	return sfd;
}

//用于信号处理,让服务器在按下Ctrl+c或Ctrl+\时不会退出
void signalhandler(void)
{
	sigset_t sigSet;
	sigemptyset(&sigSet);
	sigaddset(&sigSet,SIGINT);
	sigaddset(&sigSet,SIGQUIT);
	sigprocmask(SIG_BLOCK,&sigSet,NULL);
}

/*********************************************************************
File Name:               tcp_net_server.c
Author:                  xxx        date:
Description:            
Fuction List:			
********************************************************************/
#include "tcp_net_socket.h"

char buf;
sem_t MSG1;
sem_t MSG2;

void *client1(void* fd)
{
	int cfd = *((int *)fd);
	int ret;
	while(1)
	{
		sem_wait(&MSG1);
	
		ret = read(cfd, &buf, sizeof(char));
		if(ret == -1)
		{
			perror("read");
			close(cfd);
			return ;
		}
		printf("read success!\n");
		
		buf = buf - 'a' + 'A';
		
		sem_post(&MSG2);
	 } 
	 close(cfd);
}

void *client2(void* fd)
{
	int cfd = *((int *)fd);
	int ret;
	while(1)
	{
		sem_wait(&MSG2);
		
		ret = write(cfd, &buf, sizeof(char));
		if(ret == -1)
		{
			perror("write");
			close(cfd);
			return;
		}
		printf("write success!\n");
		memset(&buf,0,sizeof(char));
		
		sem_post(&MSG1);
	}
	close(cfd);
}

int main()
{
	signalhandler();
	
	int sfd = tcp_init();
	int ret;
	int cfd[2];
	pthread_t client1ID;
	pthread_t client2ID;
	int i = 0;
	
	sem_init(&MSG1, 0, 1);
	sem_init(&MSG2, 0, 0);
	
	while(1)
	{
		printf ("waiting for client to connect.....\n");
		
		cfd[i] = tcp_accept(sfd);
		if(cfd[i] == -1)
		{
			return -1;
		}
		
		if(i == 0)
		{
			pthread_create(&client1ID, NULL, client1, (void*)&cfd[i]);
		}
		
		if(i == 1)
		{
			pthread_create(&client2ID, NULL, client2, (void*)&cfd[i]);
		}
		
		i++;
	}
	
	close(sfd);
	
	return 0;
}
/*********************************************************************
File Name:               tcp_net_client1.c
Author:                 xxx       date:
Description:            
Fuction List:			
********************************************************************/
#include "tcp_net_socket.h"

int main(int argc, char** argv)
{
	if(argc != 2)
	{
		printf("Usage:./client ip\n");
		return -1;
	}
	
	int ret;
	char buf[2] = {0};
	int sfd = tcp_connect(argv[1]);
	
	while(1)
	{
		memset(&buf,0,sizeof(buf));
		printf("请输入小写字母:");
		scanf("%s",&buf);
		
		ret = write(sfd, &buf[0], 1);
		if(ret == -1)
		{
			perror("write");
			return -1;
		}
	}
	
	close(sfd);
	
	return 0;
}
/*********************************************************************
File Name:               tcp_net_client2.c
Author:                  xxx        date:
Description:            
Fuction List:			
********************************************************************/
#include "tcp_net_socket.h"

int main(int argc, char** argv)
{
	if(argc != 2)
	{
		printf("Usage:./client ip\n");
		return -1;
	}
	
	int ret;
	char buf;
	int sfd = tcp_connect(argv[1]);
	
	while(1)
	{
		memset(&buf,0,sizeof(buf));
		
		ret = read(sfd, &buf, sizeof(char));
		if(ret == -1)
		{
			perror("read");
			return -1;
		}
		
		printf("转换后的大写字母为:");
		printf("%c\n",buf);
		
	}
	close(sfd);
	
	return 0;
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26862次
    • 积分:2341
    • 等级:
    • 排名:第16011名
    • 原创:216篇
    • 转载:20篇
    • 译文:0篇
    • 评论:15条
    最新评论