【0814作业】多线程并发服务器

1) 代码

cs 复制代码
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <head.h>
#include <pthread.h>

#define PORT 6666               //1024-49151
#define IP "192.168.1.102"    //ifconfig查看本机ip

//需要传入到分支线程的数据类型
struct CliMsg
{
	int newfd;
	struct sockaddr_in cin;
};

void *deal_cli_msg(void *arg);
void handlr(int sig)
{
	while(waitpid(-1,NULL,WNOHANG) > 0);
}

int main(int argc, const char *argv[])
{
	if(signal(17,handlr) == SIG_ERR)
	{
		ERR_MSG("signal");
		return -1;
	}

	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}
	printf("sfd= %d\n",sfd);                                                                                                    

	//设置端口允许端口被快速复用
	int reuse = 1;
	if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速重用成功\n");

	//填充地址信息结构体,真实的地址信息结构体根据地址族制定
	//AF_INET: man 7 ip
	struct sockaddr_in sin;
	sin.sin_family         = AF_INET;       //必须填AF_INET
	sin.sin_port           = htons(PORT);   //端口号:1024~49151(网络端口号的字节序)(端口号存储在2个字节的无符号整数中)
	sin.sin_addr.s_addr    = inet_addr(IP); //本机IP inconfig查看(本机IP地址的字节序)

	//绑定服务器的IP和端口号--->必须绑定( bind )
	if(bind(sfd , (struct sockaddr*)&sin, sizeof(sin)) < 0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	//将套接字设置为被动监听状态( listen)
	if(listen(sfd,128) < 0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");

	struct sockaddr_in cin;   //存储客户端的地址信息
	socklen_t addrlen = sizeof(cin);
	int newfd = -1;
	pthread_t tid;
	struct CliMsg info;
	while(1)
	{
		newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
		if(newfd < 0)
		{
			ERR_MSG("accept");
			return -1;
		}
		printf("[%s : %d]newfd=%d 客户端连接成功\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
		info.newfd = newfd;
		info.cin=cin;
		if(pthread_create(&tid,NULL,deal_cli_msg,&info) != 0)
		{
			ERR_MSG("pthread_create");
			return -1;
		}
		pthread_detach(tid);
	}
	//关闭所有文件描述符
	close(sfd);
	return 0;
}
//线程执行体
void* deal_cli_msg(void *arg)
{
	int newfd = ((struct CliMsg *)arg)->newfd;
	struct sockaddr_in cin = ((struct CliMsg *)arg)->cin;

	char buf[128]="";
	ssize_t res = 0;
	while(1)
	{
		//接收数据
		bzero(buf,sizeof(buf));
		res=recv(newfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			break;
		}
		else if(0 == res)
		{
			printf("[%s : %d]newfd=%d 客户端已下线\n",\
					inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
			break;
		}
		printf("[%s : %d]newfd=%d : %s\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd, buf);
		//发送数据
		strcat(buf,"*_*");
		if(send(newfd,buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			return NULL;
		}
		printf("send succuss\n");
	}
	close(newfd);
	pthread_exit(NULL);
}

2) 多线程中的newfd,能否修改成全局,为什么?

不行。因为同属于一个进程下的线程,共享其附属进程的所有资源。代码中的newfd和cin(用于存储客户端的信息)是临界资源,当有新的客户端连接之后,将会覆盖之前的文件描述符和客户端的地址信息,导致收发对象会出现错误。

3) 多线程中分支线程的newfd能否不另存,直接用指针间接访问主线程中的newfd,为什么?

不行。与上一道题一样,同属于一个进程下的线程,共享其附属进程的所有资源。当指针指向变量,将指针指向的值修改,原本变量内的值也会随之改变。并不是另存一个值,而是修改了原本的值。

相关推荐
qq_4557608514 小时前
docker - 虚拟化和容器化
linux·运维·服务器
小年糕是糕手14 小时前
【C++】string类(一)
linux·开发语言·数据结构·c++·算法·leetcode·改行学it
大聪明-PLUS14 小时前
常见的 Docker 问题及解决方法
linux·嵌入式·arm·smarc
sali-tec14 小时前
C# 基于halcon的视觉工作流-章70 深度学习-Deep OCR
开发语言·人工智能·深度学习·算法·计算机视觉·c#·ocr
fendouweiqian14 小时前
warm-flow 生产环境静态资源 404,本地正常的原因与 Nginx 配置解决方案
运维·nginx
顾安r14 小时前
12.17 脚本网页 创意导航
java·linux·前端·游戏·html
Json____14 小时前
springboot框架对接物联网,配置TCP协议依赖,与设备通信,让TCP变的如此简单
java·spring boot·后端·tcp/ip
武藤一雄14 小时前
C#中常见集合都有哪些?
开发语言·微软·c#·.net·.netcore
云和数据.ChenGuang14 小时前
ELK 是一套**开源的日志收集、存储、分析与可视化的技术栈
服务器·数据库·elk·开源·运维技术·数据库运维工程师
艾莉丝努力练剑14 小时前
【Linux进程(二)】Linux进程的诞生、管理与消亡:一份基于内核视角的完整分析
大数据·linux·运维·服务器·c++·安全·centos