网络编程 day4~day5.1——多点通信,域套接字
笔记脑图
作业:
1、将广播发送和接收端实现一遍,完成一个发送端发送信息,对应多个接收端接收信息实验。
接收端
#include <stdio.h>
#include <string.h>
#include <myhead.h>
#define IP "192.168.61.255"
#define PORT 6666
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];
while(1)
{
recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);
printf("接收到%s发送来的消息:%s\n",inet_ntoa(recv.sin_addr),buff);
if(strcmp(buff,"quit")==0)
{
printf("接收端退出\n");
break;
}
}
close(oldfd);
return 0;
}
发送端
#include <stdio.h>
#include <string.h>
#include <myhead.h>
#define IP "192.168.61.255"
#define PORT 6666
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;
}
//结构体
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);
buff[strlen(buff)-1]='\0';
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&send,sizeof(send));
printf("发送成功\n");
if(strcmp(buff,"quit")==0)
{
printf("发送端退出\n");
break;
}
}
close(oldfd);
return 0;
}
2、使用多线程基于TCP协议的并发执行,一个服务器对应多个客户端实现通信实验。
服务器
#include <stdio.h>
#include <string.h>
#include <myhead.h>
#define IP "192.168.60.44"
#define PORT 6666
#define BACKLOG 1024
typedef struct
{
struct sockaddr_in client;
int newfd;
}stu;
void *fun(void *sss)
{
int newfd=((stu *)sss)->newfd;
struct sockaddr_in client=((stu *)sss)->client;
printf("%s发来消息\n",inet_ntoa(client.sin_addr));
char buff[1024];
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);
strcat(buff,"1973");
send(newfd,buff,sizeof(buff),0);
}
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;
}
//
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);
stu a;
a.newfd=newfd;
a.client=client;
pthread_t tid;
tid=pthread_create(&tid,NULL,fun,&a);
if(tid==-1)
{
perror("pthread_create");
return -1;
}
pthread_detach(tid);
}
return 0;
}
客户端
#include <stdio.h>
#include <string.h>
#include <myhead.h>
#define IP "192.168.60.44"
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建
int oldfd=socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
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);
int len=recv(oldfd,buff,sizeof(buff),0);
if(len==0)
{
printf("服务器意外退出\n");
break;
}
printf("接收服务器的消息:%s\n",buff);
}
//关闭
close(oldfd);
return 0;
}