思维导图
作业
将广播发送和接收端实现一遍,完成一个发送端发送信息,对应多个接收端接收
自实验
//广播发送端
#include <myhead.h>
#define G_PORT 8765
#define G_IP "192.168.124.255"
int main(int argc, const char *argv[])
{
//创建套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socker");
return -1;
}
//设置允许广播
int k=1;
if(setsockopt(oldfd,SOL_SOCKET,SO_BROADCAST,&k,sizeof(k))==-1)
{
perror("setsockopt");
return -1;
}
//填充广播地址信息结构体
struct sockaddr_in broadcast = {
.sin_family = AF_INET,
.sin_port = htons(G_PORT),
.sin_addr.s_addr = inet_addr(G_IP)
};
//收发消息
char buff[1024];
while(1)
{
printf("请输入发送消息:");
fgets(buff,sizeof(buff),stdin);
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&broadcast,sizeof(broadcast));
}
close(oldfd);
return 0;
}
//广播接收端
#include <myhead.h>
#define G_PORT 8765
#define G_IP "192.168.124.255"
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 broadcast = {
.sin_family = AF_INET,
.sin_port = htons(G_PORT),
.sin_addr.s_addr = inet_addr(G_IP)
};
int broadcast_len = sizeof(broadcast);
//绑定
if(bind(oldfd,(struct sockaddr *)&broadcast,broadcast_len)==-1)
{
perror("bind");
return -1;
}
//收发消息
char buff[1024];
while(1)
{
recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);
printf("接收到消息:%s\n",buff);
}
return 0;
}
使用多线程基于TCP协议的并发执行,一个服务器对应多个客户端实现通信实验
//多线程服务器
#include <myhead.h>
#define PORT 8764
#define IP "192.168.124.94"
void *fun(void *fd)
{
int newfd = *(int *)fd;
char buff[1024];
while(1)
{
recv(newfd,buff,sizeof(buff),0);
printf("接收到客户端的消息:%s\n",buff);
}
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,10)==-1)
{
perror("listen");
return -1;
}
//创建子线程,主线程接收客户端,子线程负责收发消息
pthread_t tid;
struct sockaddr_in client;
int client_len = sizeof(client);
int newfd;
while(1)
{
newfd = accept(oldfd,(struct sockaddr *)&client,&client_len);
if(newfd==-1)
{
perror("accept");
return -1;
}
if(pthread_create(&tid,NULL,fun,&newfd)==-1)
{
perror("pthread_create");
return -1;
}
}
pthread_join(tid,NULL);
return 0;
}
//多线程客户端
#include <myhead.h>
#define PORT 8764
#define IP "192.168.124.94"
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)
{
printf("请输入要发送的消息:");
fgets(buff,sizeof(buff),stdin);
send(oldfd,buff,sizeof(buff),0);
}
return 0;
}
流式域套接字
//流式域服务器
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int oldfd = socket(AF_UNIX,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//判断套接字文件存在就删除
if(access("./server1",F_OK)==0)
{
if(unlink("./server1")==-1)
{
perror("unlink");
return -1;
}
}
//填充结构体信息
struct sockaddr_un server;
server.sun_family = AF_UNIX;
strcpy(server.sun_path,"./server1");
//绑定本地套接字文件
if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
//监听
if(listen(oldfd,10)==-1)
{
perror("listen");
return -1;
}
//接收客户端
struct sockaddr_un client;
int client_len = sizeof(client);
int newfd = accept(oldfd,(struct sockaddr *)&client,&client_len);
if(newfd==-1)
{
perror("accept");
return -1;
}
//收发消息
char buff[1024];
while(1)
{
recv(newfd,buff,sizeof(buff),0);
printf("接收到客户端消息:%s\n",buff);
}
return 0;
}
//流式域客户端
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int oldfd = socket(AF_UNIX,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//判断套接字文件存在就删除
if(access("./client1",F_OK)==0)
{
if(unlink("./client1")==-1)
{
perror("unlink");
return -1;
}
}
//绑定
struct sockaddr_un client;
client.sun_family = AF_UNIX;
strcpy(client.sun_path,"./client1");
if(bind(oldfd,(struct sockaddr *)&client,sizeof(client))==-1)
{
perror("bind");
return -1;
}
//填充结构体
int client_len = sizeof(client);
struct sockaddr_un server;
server.sun_family = AF_UNIX;
strcpy(server.sun_path,"./server1");
//连接服务器
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
//数据收发
char buff[1024];
while(1)
{
printf("请输入您想要发送的信息:");
fgets(buff,sizeof(buff),stdin);
send(oldfd,buff,sizeof(buff),0);
}
return 0;
}