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;
}
运行测试结果: