3.4作业
课上代码复习:
广播接收端代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int rfd = socket(AF_INET,SOCK_DGRAM,0);
if(rfd == -1)
{
perror("socket error");
return -1;
}
printf("rfd = %d\n",rfd);
//填充地址信息结构体
struct sockaddr_in rin;
rin.sin_family = AF_INET;
rin.sin_port = htons(8888);
rin.sin_addr.s_addr = inet_addr("192.168.244.255");
//绑定
if(bind(rfd,(struct sockaddr*)&rin,sizeof(rin)) == -1)
{
perror("bind error");
return -1;
}
//接收广播消息
char rbuf[128] ="";
while(1)
{
//清空消息
bzero(rbuf,sizeof(rbuf));
//读取消息
recv(rfd,rbuf,sizeof(rbuf),0);
printf("收到消息:%s\n",rbuf);
}
//关闭套接字
close(rfd);
return 0;
}
广播发送端代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd == -1)
{
perror("sockeet error");
return -1;
}
//设置当前套接字允许广播属性
int broadcast = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast)) == -1)
{
perror("setsockopt error");
return -1;
}
//绑定(可选)
//填充接收端地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.s_addr = inet_addr("192.168.244.255");
//发送数据
char sbuf[128] = "";
while(1)
{
printf("请输入>>");
fgets(sbuf,sizeof(sbuf),stdin);
sbuf[strlen(sbuf)-1] = 0;
sendto(sfd,sbuf,strlen(sbuf),0,(struct sockaddr*)&sin,sizeof(sin));
printf("发送成功\n");
}
//关闭
close(sfd);
return 0;
}
组播接收端代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int rfd = socket(AF_INET,SOCK_DGRAM,0);
if(rfd == -1)
{
perror("socket error");
return -1;
}
printf("rfd = %d\n",rfd);
//设置加入多播组
struct ip_mreqn imr;
imr.imr_multiaddr.s_addr = inet_addr("224.1.2.3"); //组播ip
imr.imr_address.s_addr = inet_addr("192.168.244.140"); //本地ip
imr.imr_ifindex = 2; //网卡编号
if(setsockopt(rfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&imr,sizeof(imr)) == -1)
{
perror("setsockopt error");
return -1;
}
//填充地址信息结构体
struct sockaddr_in rin;
rin.sin_family = AF_INET;
rin.sin_port = htons(8888);
rin.sin_addr.s_addr = inet_addr("224.1.2.3");
//绑定
if(bind(rfd,(struct sockaddr*)&rin,sizeof(rin)) == -1)
{
perror("bind error");
return -1;
}
//接收组播消息
char rbuf[128] ="";
while(1)
{
//清空消息
bzero(rbuf,sizeof(rbuf));
//读取消息
recv(rfd,rbuf,sizeof(rbuf),0);
printf("收到消息:%s\n",rbuf);
}
//关闭套接字
close(rfd);
return 0;
}
组播发送端代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int rfd = socket(AF_INET,SOCK_DGRAM,0);
if(rfd == -1)
{
perror("socket error");
return -1;
}
//绑定端口号和ip地址
//填充地址信息结构体
struct sockaddr_in rin;
rin.sin_family = AF_INET;
rin.sin_port = htons(9999);
rin.sin_addr.s_addr = inet_addr("192.168.244.140");
//绑定端口号和IP
if(bind(rfd,(struct sockaddr*)&rin,sizeof(rin)) == -1)
{
perror("bind error");
return -1;
}
//发送消息
//填充接收端地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.s_addr = inet_addr("224.1.2.3");
char sbuf[128] = "";
while(1)
{
printf("请输入>>");
fgets(sbuf,sizeof(sbuf),stdin);
sbuf[strlen(sbuf)-1] = 0;
//向组播地址中发送信息
sendto(rfd,sbuf,strlen(sbuf),0,(struct sockaddr*)&sin,sizeof(sin));
printf("发送成功\n");
}
return 0;
}
流式域套接字
TCP服务器端实现:
#include<myhead.h>
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = socket(AF_UNIX,SOCK_STREAM,0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
//判断套接字文件是否存在,如果存在需要删除
if(access("./mysocket",F_OK) == 0);
{
//说明存在,将该文件删除
if(unlink("./mysocket") == -1)
{
perror("unlink error");
return -1;
}
}
//绑定套接字文件
//填充地址信息结构体
struct sockaddr_un sun;
sun.sun_family = AF_UNIX; //地址族
strcpy(sun.sun_path , "./mysocket"); //套接字文件
//绑定
if(bind(sfd , (struct sockaddr*)&sun , sizeof(sun)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//监听
if(listen(sfd,128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success\n");
//阻塞等待客户端连接请求
//定义容器接收客户端地址信息
struct sockaddr_un cun;
socklen_t socklen = sizeof(cun);
int newfd = -1;
if((newfd = accept(sfd,(struct sockaddr*)&cun,&socklen))==-1)
{
perror("accept error");
return -1;
}
printf("有新客户发来链接请求\n");
//数据收发
char rbuf[128]="";
while(1)
{
//清空
bzero(rbuf,sizeof(rbuf));
//收取数据
recv(newfd,rbuf,sizeof(rbuf),0);
printf("[%s]:%s\n",cun.sun_path,rbuf);
//链接一个笑脸回复
strcat(rbuf,"*_*");
//发送回去
send(newfd,rbuf,strlen(rbuf),0);
printf("发送成功\n");
}
//关闭套接字
close(sfd);
close(newfd);
return 0;
}
TCP客户端实现:
#include<myhead.h>
int main(int argc, const char *argv[])
{
//1.创建用于通信的套接字文件描述符
int cfd = socket(AF_UNIX,SOCK_STREAM,0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
printf("cfd = %d\n",cfd);
//判断套接字文件是否存在,如果存在则需要删除
if(access("./mysocket1",F_OK) == 0)
{
//说明存在,将该文件删除
if(unlink ("./mysocket1") == -1)
{
perror("unlink error");
return -1;
}
}
//2.绑定(非必须)
//填充地址信息结构体
struct sockaddr_un cun;
cun.sun_family = AF_UNIX;
strcpy(cun.sun_path,"./mysocket1");
//绑定
if(bind(cfd,(struct sockaddr*)&cun , sizeof(cun)) == -1)
{
perror("bind error");
return -1;
}
//3.连接服务器
//3.1填充要连接的服务器地址信息结构体
struct sockaddr_un sun;
sun.sun_family = AF_UNIX; //地址族
strcpy(sun.sun_path,"./mysocket"); //套接字文件
//3.2连接服务器
if(connect(cfd ,(struct sockaddr*)&sun,sizeof(sun)) == -1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
//4.数据收发
char wbuf[128] ="";
while(1)
{
//清空数据
bzero(wbuf,sizeof(wbuf));
printf("请输入>>");
fgets(wbuf,sizeof(wbuf),stdin); //从终端输入
wbuf[strlen(wbuf)-1] = 0;
//将数据发送给服务器
send(cfd , wbuf , strlen(wbuf) , 0);
printf("发送成功\n");
//判断发送的数据
if(strcmp(wbuf,"quit") == 0)
{
break;
}
//接受服务器发来的信息
//清空数据
bzero(wbuf,sizeof(wbuf));
recv(cfd , wbuf , sizeof(wbuf), 0);
printf("收到信息为:%s\n",wbuf);
}
//5.关闭套接字
close(cfd);
return 0;
}
UDP服务器端实现:
#include<myhead.h>
int main(int argc, const char *argv[])
{
//创建用于通信的套接字
int sfd = socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n",sfd);
//判断套接字文件是否存在,如果存在则需要删除
if(access("./linux1",F_OK) == 0)
{
//说明文件存在,将该文件删除
if(unlink("./linux1") == -1)
{
perror("unlink error");
return -1;
}
}
//绑定IP地址和端口号
//填充地址信息结构体
struct sockaddr_un sun;
sun.sun_family = AF_UNIX; //地址族
strcpy(sun.sun_path,"./linux1");
//绑定
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//收发数据
char rbuf[128] = "";
//定义容器接受对端地址信息结构体
struct sockaddr_un cun;
socklen_t socklen = sizeof(cun);
while(1)
{
//清空数组
bzero(rbuf , sizeof(rbuf));
//接收数据
recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cun,&socklen);
printf("收到信息为:%s\n",rbuf);
//将消息加“*_*”返回
strcat(rbuf,"*_*");
if(sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cun,socklen) == -1)
{
perror("sendto error");
return -1;
}
}
//关闭套接字
close(sfd);
return 0;
}
UDP客户端实现:
#include<myhead.h>
int main(int argc, const char *argv[])
{
//创建用于通信的套接字
int cfd = socket(AF_UNIX,SOCK_DGRAM,0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
printf("cfd = %d\n",cfd);
//判断套接字文件是否存在,如果存在需要删除
if(access("./linux2",F_OK) == 0)
{
//说明存在,将该文件删除
if(unlink("./linux2") == -1)
{
perror("unlink error");
return -1;
}
}
//绑定IP地址和端口号(非必要)
struct sockaddr_un cun;
cun.sun_family = AF_UNIX;
strcpy(cun.sun_path,"./linux2");
if(bind(cfd,(struct sockaddr*)&cun,sizeof(cun)) == -1)
{
perror("bind error");
return -1;
}
//收发数据
char wbuf[128] = "";
//定义容器,记录服务器地址信息结构体
struct sockaddr_un sun;
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path,"./linux1");
while(1)
{
//清空数组
bzero(wbuf , sizeof(wbuf));
//从终端获取数据
printf("请输入:");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1] = 0;
//将数据发送给服务器
sendto(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&sun,sizeof(sun));
printf("发送成功\n");
//接收服务器回复的消息
bzero(wbuf , sizeof(wbuf));
}
//关闭套接字
close(cfd);
return 0;
}
课程总结:
原文地址:https://blog.csdn.net/Demonlxl/article/details/136459559
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!