思维导图
广播模型发送端-->类似于UDP客户端
#include<head.h>
int main(int argc, const char *argv[])
{//1、创建套接字int sfd=socket(AF_INET,SOCK_DGRAM,0);if(sfd==-1){perror("socket error ");return -1;}//2、将套接字设置成允许广播int broadcast=1;if(setsockopt(sfd,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast))==-1){perror("setsockopt error");return -1;}//3、绑定(非必须)//4、填充接收端地址信息结构体struct sockaddr_in cin;cin.sin_family=AF_INET;cin.sin_port=htons(6666);cin.sin_addr.s_addr=inet_addr("192.168.123.255");//5、发送消息char buf[128]="";while(1){printf("请输入>>>");fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1]=0;sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin));}//6、关闭套接字close(sfd);return 0;
}
广播模型接受端-->类似于UDP服务器端
#include<head.h>
int main(int argc, const char *argv[])
{//1、创建套接字int sfd=socket(AF_INET,SOCK_DGRAM,0);if(sfd==-1){perror("socket error ");return -1;}//2、填充地址struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(6666);sin.sin_addr.s_addr=inet_addr("192.168.123.255");socklen_t socklen=sizeof(sin);//3、绑定if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("bind error");return -1;}//4.接收信息char buf[128]="";while(1){recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&socklen);printf("%s\n",buf);}//6、关闭套接字close(sfd);return 0;
}
组播发送端
#include<head.h>
int main(int argc, const char *argv[])
{//1、创建套接字int sfd=socket(AF_INET,SOCK_DGRAM,0);if(sfd==-1){perror("socket error");return -1;}//2、填充地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(9999);sin.sin_addr.s_addr = inet_addr("224.1.2.3");//3、发送消息char sbuf[128] = "";while(1){printf("请输入>>>");fgets(sbuf, sizeof(sbuf), stdin);sbuf[strlen(sbuf)-1] = 0;//将消息发送出去sendto(sfd, sbuf, sizeof(sbuf), 0, (struct sockaddr*)&sin, sizeof(sin));printf("发送成功\n");}//4、关闭套接字close(sfd);return 0;
}
组播接收端
#include<head.h>
int main(int argc, const char *argv[])
{//1、创建套接字int sfd=socket(AF_INET,SOCK_DGRAM,0);if(sfd==-1){perror("socket error");return -1;}//2、设置网络属性struct ip_mreqn im;im.imr_multiaddr.s_addr = inet_addr("224.1.2.3"); //组播地址im.imr_address.s_addr = inet_addr("192.168.122.88"); //本机ipim.imr_ifindex = 2; //网络索引if(setsockopt(sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &im, sizeof(im)) == -1){perror("setsockopt error");return -1;}printf("加入多播组成功\n");//3、填充结构体struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(9999);sin.sin_addr.s_addr=inet_addr("224.1.2.3");//4、绑定if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("bind error");return -1;}//5、接收消息char buf[128]="";while(1){bzero(buf,sizeof(buf));recvfrom(sfd,buf,sizeof(buf),0,0,0);printf("收到:%s\n",buf);}close(sfd);return 0;
}
流式域套接字 服务器端
#include<head.h>
int main(int argc, const char *argv[])
{//1.创建套接字int sfd=socket(AF_UNIX,SOCK_STREAM,0);if(sfd==-1){perror("sfd error");return -1;}//套接字文件只能绑定一个不存在的套接字文件//所以在绑定前需要判断当前文件是否存在if(access("./unix",F_OK)==0){//表示文件存在,删除该文件if(unlink("./unix")==-1){perror("unlink error");return -1;}}//2、填充地址信息结构体struct sockaddr_un sun;sun.sun_family=AF_UNIX;//sun.sun_path="./unix";//错误,字符串赋值不能使用复制运算符strcpy(sun.sun_path,"./unix");//3.绑定if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("bind error");return -1;}//4、监听if(listen(sfd,128)==-1){perror("listen error");return -1;}//5、阻塞接收客户端连接请求struct sockaddr_un cun;socklen_t socklen=sizeof(cun);int newfd=accept(sfd,(struct sockaddr*)&cun,&socklen);//表示不接收客户端地址信息if(newfd==-1){perror("accept error");return -1;}//6、收发数据char buf[128]="";while(1){ bzero(buf,sizeof(buf));int res=recv(newfd,buf,sizeof(buf),0);if(res==0){printf("客户端以下线\n");break;}printf("[%s]buf= %s\n",cun.sun_path,buf);}//7、关闭套接字close(newfd);close(sfd);return 0;
}
流式域套接字客户端
#include<head.h>
int main(int argc, const char *argv[])
{//1.创建套接字int sfd=socket(AF_UNIX,SOCK_STREAM,0);if(sfd==-1){perror("sfd error");return -1;}//套接字文件只能绑定一个不存在的套接字文件//所以在绑定前需要判断当前文件是否存在if(access("./linix",F_OK)==0){//表示文件存在,删除该文件if(unlink("./linix")==-1){perror("unlink error");return -1;}}//2、填充地址信息结构体struct sockaddr_un cun;cun.sun_family=AF_UNIX;strcpy(cun.sun_path,"./linix");//3.绑定if(bind(sfd,(struct sockaddr*)&cun,sizeof(cun))==-1){perror("bind error");return -1;}//4.链接struct sockaddr_un sun;sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./unix");if(connect(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("connect error");return -1;}//收发数据char buf[128]="";while(1){bzero(buf,sizeof(buf));printf("请输入>>>");fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1]=0;send(sfd,buf,sizeof(buf),0);printf("发送成功\n");if(strcmp(buf,"quit")==0){break;}}//7、关闭套接字close(sfd);return 0;
}
报式域套接字服务器端
#include<head.h>
int main(int argc, const char *argv[])
{//创建套接字文件int sfd = socket(AF_UNIX,SOCK_DGRAM,0);if(sfd==-1){perror("sfd error");return -1;}if(access("./unix",F_OK)==0){if(unlink("./unix")==-1){printf("unlink error");return -1;}}//绑定struct sockaddr_un sun;sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./unix");if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("bind error");return -1;}//数据的收发char buf[128]="";struct sockaddr_un cun;socklen_t socklen=sizeof(cun);while(1){bzero(buf,sizeof(buf));recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,&socklen);printf("读取的消息为:%s\n",buf);strcat(buf,"*_*");if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,socklen)==-1){perror("write error");return -1;}}close(sfd);return 0;
}
报式域套接字客户端
#include<head.h>
int main(int argc, const char *argv[])
{//创建套接字文件int sfd = socket(AF_UNIX,SOCK_DGRAM,0);if(sfd==-1){perror("sfd error");return -1;}if(access("./linix",F_OK)==0){if(unlink("./linix")==-1){printf("unlink error");return -1;}}//绑定struct sockaddr_un sun;sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./linix");if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("bind error");return -1;}//数据的收发 struct sockaddr_un cun;cun.sun_family=AF_UNIX;strcpy(cun.sun_path,"./unix");char buf[128]="";while(1){printf("请输入>>>");fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1]=0;sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,sizeof(cun));if(strcmp(buf,"quit")==0){break;}recvfrom(sfd,buf,sizeof(buf),0,NULL,NULL);printf("收到消息:%s\n",buf);}close(sfd);return 0;
}