嵌入式学习

笔记

作业 

        多进程实现TCP并发服务器。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

#define SER_PORT 8888
#define SER_IP "192.168.125.176"

//定义信号处理函数
void handler(int signo)
{
	//判断要处理的信号
	if(signo == SIGCHLD)
	{
		while(waitpid(-1,NULL,WNOHANG) != 0);
	}
}

int main(int argc, const char *argv[])
{
	//将子进程的SICCHLD(17)信号
	//当子进程退出时,会向父进程发送信号
	if(signal(SIGCHLD,handler) == SIG_ERR)
	{
		perror("signal error");
		return -1;
	}

	//1. 为通信创建一个端点
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sfd)
	{
		perror("socket error");
		return -1;
	}
	printf("socket success sfd=%d\n",sfd);

	//调用端口号快速重用函数
	int reuse=1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1)
	{
		perror("setsockopt error");
		return -1;
	}

	//2. 绑定ip和端口号
	//2.1 准备地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(SER_PORT);
	sin.sin_addr.s_addr=inet_addr(SER_IP);
	//2.2 绑定工作
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");

	//3. 将套接字设置成被动监听状态
	if(listen(sfd,128) == -1)
	{
		perror("listen error");
		return -1;
	}
	printf("listen success\n");

	//4. 阻塞等待客户端的连接
	//4.1 定义用于接收客户端信息的容器
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);
	int newfd=-1;//客户套接字变量

	while(1)
	{
		//父进程
		newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(-1 == newfd)
		{
			perror("accept error");
			return -1;
		}
		printf("[%s:%d]:发来的请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
		pid_t pid=fork();//创建子进程
		if(pid > 0)
		{
			//父进程体
			//关闭newfd
			close(newfd);
		}else if(0 == pid)
		{
			//关闭sfd
			close(sfd);

			//5. 与客户端进行交互通信
			char rbuf[128]="";//读取消息内容的容器
			while(1)
			{
				//清空容器
				bzero(rbuf,sizeof(rbuf));

				//从套接字中读取数据
				int ret=recv(newfd,rbuf,sizeof(rbuf),0);
				if(0 == ret)
				{
					printf("客户端已经下线\n");
					break;
				}

				//将读取的消息展示出来
				printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),htons(cin.sin_port),rbuf);

				//将收到的消息处理一下,回复给客户端
				strcat(rbuf,"*_*");

				//将消息发送给客户端
				send(newfd,rbuf,strlen(rbuf),0);
				printf("发送成功\n");
			}
			//6. 关闭套接字
			close(newfd);

			//退出子进程
			exit(EXIT_SUCCESS);
		}else {
			perror("fork error");
			return -1;
		}
	}

	close(sfd);

	return 0;
}

        多线程实现TCP并发服务器。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

#define SER_PORT 8888
#define SER_IP "192.168.125.176"

//定义一个结构体类型,用于向线程体函数传递参数
struct Info {
	int newfd;
	struct sockaddr_in cin;
};

//定义线程体函数
void* deal_cli_msg(void *arg)
{
	//解析传递进来的数据
	int newfd=((struct Info*)arg)->newfd;
	struct sockaddr_in cin=((struct Info*)arg)->cin;

	//5. 与客户端进行相互通信
	char rbuf[128]="";//读取消息内容的容器

	while(1)
	{
		//清空容器
		bzero(rbuf,sizeof(rbuf));

		//从套接字中读取数据
		int ret=recv(newfd,rbuf,sizeof(rbuf),0);
		if(0 == ret)
		{
			printf("客户端已经下线\n");
			break;
		}

		//将读取的消息展现出来
		printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
		
		//将收到的消息处理一下,回复给客户端
		strcat(rbuf,"*_*");

		//将消息发送给客户端
		send(newfd,rbuf,strlen(rbuf),0);
		printf("发送成功\n");
	}

	//6. 关闭套接字
	close(newfd);

	//退出线程
	pthread_exit(NULL);

}

int main(int argc, const char *argv[])
{
	//1. 为通信创建一个端点
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sfd)
	{
		perror("socket error");
		return -1;
	}
	printf("socket success sfd=%d\n",sfd);

	//2. 绑定ip和端口号
	//2.1 准备地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_port=htons(SER_PORT);
	sin.sin_addr.s_addr=inet_addr(SER_IP);

	//2.2 绑定工作
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success\n");

	//3. 将套接字设置成被动监听状态
	if(listen(sfd,128) == -1)
	{
		perror("listen error");
		return -1;
	}
	printf("listen success\n");

	//4. 阻塞等待客户端的连接
	//4.1 定义用于接收客户端信息的容器
	struct sockaddr_in cin;
	socklen_t addrlen=sizeof(cin);

	while(1)
	{
		//accept函数会预选一个当前未分配的最小的文件描述符
		//即使在阻塞过程中,有更小的文件描述符产生,本次操作的文件描述符也不会更改了
		int newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(-1 == newfd)
		{
			perror("accept error");
			return -1;
		}
		printf("[%s:%d]:发来的连接请求,newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
		
		//定义要传递数据的结构体变量
		struct Info buf={newfd,cin};

		//创建分支线程,用于通信
		pthread_t tid=-1;
		if(pthread_create(&tid,NULL,deal_cli_msg,&buf) == -1)
		{
			printf("pthread_create error\n");
			return -1;
		}

		//将线程设置成分离态
		pthread_detach(tid);
	}
	close(sfd);

	return 0;
}

  • 17
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值