当前位置: 首页 > article >正文

DAY4 网络编程(广播和多线程并发)

作业1:

          1、将广播发送和接收端实现一遍,完成一个发送端发送信息,对应多个接收端接收信息实验。

send.c代码:

#include <myhead.h>
#define IP "192.168.61.255"//广播IP
#define PORT 7777
int main(int argc, const char *argv[])
{
	int oldfd=socket(AF_INET,SOCK_DGRAM,0);
	if(oldfd==-1)
	{
        perror("socket");
		return -1;
	}
	int n=-1;
	if(setsockopt(oldfd,SOL_SOCKET,SO_BROADCAST,&n,sizeof(n))==-1)
	{
      perror("setsockopt");
	  return -1;
	}
	printf("允许广播设置成功\n");
	struct sockaddr_in send={
       .sin_family=AF_INET,
	   .sin_port=htons(PORT),
	   .sin_addr.s_addr=inet_addr(IP),
	};

	char buff[1024];
	while(1)
	{
		bzero(buff,sizeof(buff));
		fgets(buff,sizeof(buff),stdin);
        sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr*)&send,sizeof(send));
	}
	close(oldfd);
	return 0;
}

recv.c代码:

#include <myhead.h>
#define IP "192.168.61.255"
#define PORT 7777
int main(int argc, const char *argv[])
{
	int oldfd=socket(AF_INET,SOCK_DGRAM,0);
    if(oldfd==-1)
	{
        perror("socket");
		return -1;
	}
	struct sockaddr_in recv={
       .sin_family=AF_INET,
	   .sin_port=htons(PORT),
	   .sin_addr.s_addr=inet_addr(IP),
	};
	if(bind(oldfd,(struct sockaddr*)&recv,sizeof(recv))==-1)
	{
          perror("bind");
		  return -1;
	}
    char buff[1024];
	socklen_t len=sizeof(recv);
	while(1)
	{
		bzero(buff,sizeof(buff));
        recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr*)&recv,&len);
		printf("接收到%s的消息:%s\n",inet_ntoa(recv.sin_addr),buff);
	}
    close(oldfd);
	return 0;
}

运行测试结果:

作业2:

        使用多线程基于TCP协议的并发执行,一个服务器对应多个客户端实现通信实验。

server.c代码:

#include <myhead.h>
#define IP "192.168.60.82"
#define PORT 8888
#define BACKLOG 20
typedef struct
{
	int newfd;
	struct sockaddr_in client;
}DDD;
void *fun(void *sss)
{
	char buff[1024];
	DDD s=*(DDD *)sss;
	int newfd=s.newfd;
	struct sockaddr_in client=s.client;
	while(1)
	{
		int len=recv(newfd,buff,sizeof(buff),0);
		{
            if(len==0)
			{
               printf("客户端下线\n");
			   break;
			}
		}
		printf("接收到%s的消息:%s\n",inet_ntoa(client.sin_addr),buff);
		strcpy(buff,"666");
		send(newfd,buff,sizeof(buff),0);
		printf("发送成功\n");
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	int oldfd=socket(AF_INET,SOCK_STREAM,0);
	if(oldfd==-1)
	{
		perror("socket");
		return -1;
	}
	int n=1;
	if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(n))==-1)
	{
		perror("setsockopt");
		return -1;
	}
	struct sockaddr_in server={
		.sin_family=AF_INET,
		.sin_port=htons(PORT),
		.sin_addr.s_addr=inet_addr(IP),
	};
	if(bind(oldfd,(struct sockaddr*)&server,sizeof(server))==-1)
	{
		perror("bind");
		return -1;
	}
	if(listen(oldfd,BACKLOG)==-1)
	{
		perror("listen");
		return -1;
	}
	struct sockaddr_in client;
	socklen_t client_len=sizeof(client);
	while(1)
	{   
		int newfd=accept(oldfd,(struct sockaddr*)&client,&client_len);
		DDD s;
		s.newfd=newfd;
		s.client=client;
		pthread_t tid;
		if(pthread_create(&tid,NULL,fun,&s)==-1)
		{
			perror("pthread_create");
			return -1;
		}
		pthread_detach(tid);
	}
	return 0;
}

client.c代码:

#include <myhead.h>
#define IP "192.168.60.82"
#define PORT 8888
#define BACKLOG 20
int main(int argc, const char *argv[])
{
	int oldfd=socket(AF_INET,SOCK_STREAM,0);
	if(oldfd==-1)
	{
        perror("socket");
		return -1;
	}
	int n=1;
	if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(n))==-1)
	{
        perror("setsockopt");
		return -1;
	}
	struct sockaddr_in server={
       .sin_family=AF_INET,
	   .sin_port=htons(PORT),
	   .sin_addr.s_addr=inet_addr(IP),
	};
	if(connect(oldfd,(struct sockaddr*)&server,sizeof(server))==-1)
	{
        perror("connect");
		return -1;
	}
	char buff[1024];
	while(1)
	{  
		fgets(buff,sizeof(buff),stdin);
		buff[strlen(buff)-1]='\0';
		send(oldfd,buff,sizeof(buff),0);
		recv(oldfd,buff,sizeof(buff),0);
		printf("接收到服务器消息:%s\n",buff);
	}
	close(oldfd);
	return 0;
}

运行测试结果:

作业3:

        流式域套接字实现(TCP)

服务器代码:

#include <myhead.h>
#define BACKLOG 100
int main(int argc, const char *argv[])
{
    //1、创建流式域套接字
    int oldfd = socket(AF_UNIX,SOCK_STREAM,0);//创建流式域套接字
    if(oldfd==-1)
    {
        perror("socket");
        return -1;
    }
    //2、绑定本地套接字文件作为服务器身份
    if(access("./myserver",F_OK)==0)//如果存在本地文件就删除
    {
        if(unlink("./myserver")==-1)//删除文件
        {
            perror("unlink");
            return -1;
        }
    }
    struct sockaddr_un server;
    server.sun_family = AF_UNIX;//绑定本地套接字
    strcpy(server.sun_path,"./myserver");//绑定本地文件
    if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
    {
        perror("bind");
        return -1;
    }
    
    //3、监听
    if(listen(oldfd,BACKLOG)==-1)
    {
        perror("listen");
        return -1;
    }
    //4、接收客户端请求
    char buff[1024];
    struct sockaddr_un client;
    socklen_t client_len = sizeof(client);

    int newfd = accept(oldfd,(struct sockaddr *)&client,&client_len);
    printf("%s发来连接请求\n",client.sun_path);//打出文件名即可
    while(1)
    {
		bzero(buff,sizeof(buff));
        int len=recv(newfd,buff,sizeof(buff),0);
		if(len==0)
		{
             printf("客户端下线\n");
			 break;
		}
        printf("接收到信息:%s\n",buff);

        strcpy(buff,"666");
        send(newfd,buff,sizeof(buff),0);
        printf("发送成功\n");
    }
    close(oldfd);
    close(newfd);
    return 0;
}

客户端代码:

#include <myhead.h>
#define BACKLOG 100
int main(int argc, const char *argv[])
{
    //1、创建流式域套接字
    int oldfd = socket(AF_UNIX,SOCK_STREAM,0);//创建流式域套接字
    if(oldfd==-1)
    {
        perror("socket");
        return -1;
    }
    struct sockaddr_un server;
    server.sun_family = AF_UNIX;
    strcpy(server.sun_path,"./myserver");
    if(connect(oldfd,(struct sockaddr*)&server,sizeof(server))==-1) 
	{
        perror("connect");
		return -1;
	}
    char buff[1024];
    while(1)
    {
        fgets(buff,sizeof(buff),stdin);
		buff[strlen(buff)-1]='\0';
        send(oldfd,buff,sizeof(buff),0);
        printf("发送成功\n");
    }
    close(oldfd);
    return 0;
}

运行测试结果:


http://www.kler.cn/a/404138.html

相关文章:

  • python操作selenium的简单封装
  • DDei在线设计器V1.2.43版发布
  • Win11 24H2新BUG或影响30%CPU性能,修复方法在这里
  • 14. 【.NET 8 实战--孢子记账--从单体到微服务】--简易权限--章节总结
  • Mybatis框架之观察者模式 (Observer Pattern)
  • window 中安装 php 环境
  • shell(6)之for循环
  • React可以做全栈开发吗
  • atob()为啥明明表示base64toASCII却叫atob?(2)
  • AIX下crs-5005 ip address is aready in use in the network的解决办法
  • Apple Vision Pro开发003-PolySpatial2.0新建项目
  • Ubuntu24.04——软件包系统已损坏
  • 关于学习小波分析
  • Xilinx IP核(3)XADC IP核
  • AcWing 842. 排列数字(周四)
  • translation1
  • 【Maven】IDEA创建Maven项目 Maven配置
  • ssm框架-spring-spring声明式事务
  • Spring Boot实验室管理系统:高效科研管理解决方案
  • HBase Flink操作
  • 详解Rust枚举类型(enum)的用法
  • 第十二章 Shell脚本编写实战
  • Java项目实战II基于微信小程序的农场驿站平台(开发文档+数据库+源码)
  • 第三代指标平台相较于前两代的显著优势分析
  • 太阳能激光驱鸟器的工作原理是什么,对鸟类有无影响?
  • android MQTT使用示例