网络编程 DAY3 作业

1.TCP传输并发服务器——多进程实现

#include <head.h>
#define PORT 9999
#define IP "192.168.125.***"

int del_cin_msg(int newfd,struct sockaddr_in cin)
{
	char buf[128] = "";
	while(1)
	{
		bzero(buf,sizeof(buf));   //数组清零
		int res = recv(newfd,buf,sizeof(buf),0);
		if(res == 0)
		{
			printf("客户端下线\n");
			break;
		}
		printf("[%s:%d]发送信息:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);

		strcat(buf,"*_*");
		send(newfd,buf,sizeof(buf),0);
	}
	close(newfd);
	return 0;
}

void handler(int signo)
{
	if(signo == SIGCHLD)
	{
		while(waitpid(-1,NULL,WNOHANG) > 0);
	}
}


/**************************主函数*****************************/
int main(int argc, const char *argv[])
{
	//1.创建套接字
	int sfd = -1;
	if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
	{
		perror("socket error");
		return -1;
	}
	printf("sfd = %d\n",sfd);

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

	//2.绑定函数
	//2.1定义地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;  //地址族信息
	sin.sin_port = htons(PORT);  //端口号网络字节序
	sin.sin_addr.s_addr = inet_addr(IP);  //IP地址网络字节序

	//2.2绑定函数
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success %d_%s_%s\n", __LINE__ , __FILE__ , __func__);

	//3.设置监听状态
	if(listen(sfd,128) == -1)
	{
		perror("listen error");
		return -1;
	}
	printf("listen success %d_%s_%s\n", __LINE__ , __FILE__ , __func__);
	
	//4.进行连接函数
	int newfd = -1;

	//定义接收地址信息结构体
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);

	//定义进程号
	pid_t pid = -1;

	while(1)
	{
		if((newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen)) == -1)
		{
			perror("accept error");
			return -1;
		}
		printf("[%s:%d]连接成功,newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);

		//创建子进程
		pid = fork();

		if(pid > 0)
		{
			//父进程
			
			//绑定信号
			if(signal(SIGCHLD,handler) == SIG_ERR)
			{
				perror("signal error");
				return -1;
			}

			close(newfd);
		}else if(pid == 0)
		{
			//子进程
			close(sfd);

			//收发信息
			del_cin_msg(newfd,cin);

			exit(EXIT_SUCCESS);
		}else
		{
			perror("pid error");
			return -1;
		}
	}

	close(sfd);
	return 0;
}

2.TCP传输并发服务器——多线程实现

#include <head.h>
#define PORT 9999
#define IP "192.168.125.***"

//定义线程传参结构体
struct pthread_data
{
	int newfd;   //处理客户端套接字文件描述符
	struct sockaddr_in cin;  //客户端地址信息结构体
};
void* del_cin_msg(void*arg)
{
	int newfd = ((struct pthread_data*)arg) -> newfd;
	struct sockaddr_in cin = ((struct pthread_data*)arg) -> cin;  //接收参数,强转类型

	char buf[128] = "";
	while(1)
	{
		bzero(buf,sizeof(buf));   //数组清零
		int res = recv(newfd,buf,sizeof(buf),0);
		if(res == 0)
		{
			printf("客户端下线\n");
			break;
		}
		printf("[%s:%d]发送信息:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);

		strcat(buf,"*_*");
		send(newfd,buf,sizeof(buf),0);
	}
	close(newfd);
	//关闭分支线程
	pthread_exit(NULL);
}



/**************************主函数*****************************/
int main(int argc, const char *argv[])
{
	//1.创建套接字
	int sfd = -1;
	if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
	{
		perror("socket error");
		return -1;
	}
	printf("sfd = %d\n",sfd);

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

	//2.绑定函数
	//2.1定义地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;  //地址族信息
	sin.sin_port = htons(PORT);  //端口号网络字节序
	sin.sin_addr.s_addr = inet_addr(IP);  //IP地址网络字节序

	//2.2绑定函数
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("bind error");
		return -1;
	}
	printf("bind success %d_%s_%s\n", __LINE__ , __FILE__ , __func__);

	//3.设置监听状态
	if(listen(sfd,128) == -1)
	{
		perror("listen error");
		return -1;
	}
	printf("listen success %d_%s_%s\n", __LINE__ , __FILE__ , __func__);
	
	//4.进行连接函数
	int newfd = -1;
	pthread_t tid = -1;

	//定义接收地址信息结构体
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);



	while(1)
	{
		if((newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen)) == -1)
		{
			perror("accept error");
			return -1;
		}
		printf("[%s:%d]连接成功,newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
	
		//定义传参结构体并初始化
		struct pthread_data info = {newfd,cin};

		if(pthread_create(&tid,NULL,del_cin_msg,&info) != 0)
		{
			printf("tid create error\n");
			return -1;
		}
	}

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

3.简单项目——发送协议数据包让机械臂进行移动

#include <head.h>
#define PORT 8888
#define IP "192.168.125.***"
int main(int argc, const char *argv[])
{
	//1.创建套接字
	int sfd = -1;
	if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
	{
		perror("socket error");
		return -1;
	}

	//2.绑定(非必须)

	//3.连接函数
	//定义服务器地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;   //地址族信息
	sin.sin_port = htons(PORT);  //端口号网络字节序
	sin.sin_addr.s_addr = inet_addr(IP);  //IP地址网络字节序

	//连接到机械臂服务器
	if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
	{
		perror("connect error");
		return -1;
	}

	//4.收发函数
	char rbuf[5] = {0xff,0x02,0x00,0x00,0xff};
	unsigned char bbuf[5] = {0xff,0x02,0x01,0x00,0xff};
	int value = 0;
	while(1)
	{
		value = getchar();  //从终端获取字符
		switch(value)
		{
		case 'w':
		case 'W':
			{
				rbuf[3] += 2;
				if(rbuf[3] >= 90)
				{
					rbuf[3] = 90;
				}
				//发送信息到服务器
				send(sfd,rbuf,sizeof(rbuf),0);
			}
			break;
		case 's':
		case 'S':
			{
				rbuf[3] -= 2;
				if(rbuf[3] <= -90)
				{
					rbuf[3] = -90;
				}
				//返送信息到服务器
				send(sfd,rbuf,sizeof(rbuf),0);
			}			 	
			break;
		case 'd':
		case 'D':
			{
				bbuf[3] += 2;
				if(bbuf[3] >= 180)
				{
					bbuf[3] = 180;
				}
				send(sfd,bbuf,sizeof(bbuf),0);
			}
			break;
		case 'a':
		case 'A':
			{
				bbuf[3] -= 2;
				if(bbuf[3] <= 0)
				{
					bbuf[3] = 0;
				}
				send(sfd,bbuf,sizeof(bbuf),0);
			}
			break;
		}
	}

	//5.关闭套接字
	close(sfd);

	return 0;
}

4.思维套图

 

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值