一、多点通信之广播的收发端实现
1.广播发送端代码:
#include<myhead.h>int main(int argc, const char *argv[])
{int sfd=socket(AF_INET,SOCK_DGRAM,0);//创建套接字if(sfd==-1){perror("socket,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.32.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;
}
2.广播接收端代码:
int main(int argc, const char *argv[])
{int rfd=socket(AF_INET,SOCK_DGRAM,0);//基于ipv4的udp模型实现广播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.32.255");//ip地址主机号全为1的地址为广播地址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;
}
运行:
二、多点通信之组播的收发实现
1.组播发送端代码
#include<myhead.h>int main(int argc, const char *argv[])
{//创建套接字int rfd=socket(AF_INET,SOCK_DGRAM,0);//基于ipv4的udp模型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.32.130");//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");//组播的ipchar 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");}//关闭close(rfd);return 0;
}
2.组播接收端代码
#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");//组播ipimr.imr_address.s_addr=inet_addr("192.168.32.130");//本机ipimr.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;
}
运行结果:
三、 流式域套接字(基于tcp)的服务器、客户端实现。
1.服务器代码:
#include<myhead.h>int main(int argc, const char *argv[])
{int sfd=socket(AF_UNIX,SOCK_STREAM,0);//通过设置参数不同创建域套接字 传输方式tcpif(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 error");//接收客户端发来的链接请求,并保存客户端地址信息,返回新的文件描述符用于通信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;}puts("已接受新客户端连接");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;
}
2.客户端代码
#include<myhead.h>int main(int argc, const char *argv[])
{int cfd=socket(AF_UNIX,SOCK_STREAM,0);if(cfd==-1){perror("socket error");return -1;}if(access("./mysocket1",F_OK)==0){if(unlink("./mysocket1")==-1){perror("unlink error");return -1;}}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;}printf("bind success\n");struct sockaddr_un sun;//服务器地址信息sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./mysocket");if(connect(cfd,(struct sockaddr*)&sun,sizeof(sun))==-1)//连接服务器{perror("connet error");return -1;}printf("connet success\n");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);}close(cfd);return 0;
}
运行:
四、报式域套接字 (基于udp)的服务器、客户端实现。
1.服务器代码
#include<myhead.h>int main(int argc, const char *argv[])
{int sfd=socket(AF_UNIX,SOCK_DGRAM,0);//创建基于udp创建域套接字if(sfd==-1){perror("socket error");return -1;}if(access("./linux1",F_OK)==0)//检查文件是否存在如果存在则删除{if(unlink("./linux1")==-1){perror("unlink error");return -1;}}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,sizeof(cun))==-1){perror("sendto error");return -1;}}close(sfd);return 0;
}
2.客户端代码
#include<myhead.h>int main(int argc, const char *argv[])
{int cfd=socket(AF_UNIX,SOCK_DGRAM,0);//创建域套接字 传输方式udpif(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;}} 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;}printf("bind success\n");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));recvfrom(cfd,wbuf,sizeof(wbuf),0,NULL,NULL);//接收消息,因为前面已经有了服务器地址信息 所以此处可以不接收printf("收到消息:%s\n",wbuf);}close(cfd);return 0;
}
运行:
思维导图: