广播模型:
发送端:
#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.125.255");//广播地址//绑定(可选)//发送广播消息char sbuf[1024];while(1){bzero(sbuf,sizeof(sbuf));printf("请输入:");fgets(sbuf,sizeof(sbuf),stdin);sbuf[strlen(sbuf)-1]='\0';//发送消息sendto(sfd,sbuf,sizeof(sbuf),0,(struct sockaddr*)&sin,sizeof(sin));puts("发送成功");if(strcmp(sbuf,"end")==0){break;}}//关闭套接字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;}//填充地址信息结构体struct sockaddr_in rin;rin.sin_family=AF_INET;rin.sin_port=htons(8888);rin.sin_addr.s_addr=inet_addr("192.168.125.255");//广播地址//绑定if(bind(rfd,(struct sockaddr*)&rin,sizeof(rin))==-1){perror("bind error");return -1;}//接收广播消息char rbuf[1024];while(1){bzero(rbuf,sizeof(rbuf));//读取消息recv(rfd,rbuf,sizeof(rbuf),0);printf("收到消息:%s\n",rbuf);if(strcmp(rbuf,"end")==0){break;}}//关闭套接字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("socket 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[1024];while(1){printf("请输入:");fgets(sbuf,sizeof(sbuf),stdin);sbuf[strlen(sbuf)-1]='\0';sendto(sfd,sbuf,sizeof(sbuf),0,(struct sockaddr*)&sin,sizeof(sin));puts("发送成功");if(strcmp(sbuf,"end")==0){break;}}//关闭套接字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;}//设置加入多播组struct ip_mreqn imr;imr.imr_multiaddr.s_addr=inet_addr("224.1.2.3");//组播ipimr.imr_address.s_addr=inet_addr("192.168.125.151");//本地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[1024];while(1){bzero(rbuf,sizeof(rbuf));//读取消息recv(rfd,rbuf,sizeof(rbuf),0);printf("收到消息:%s\n",rbuf);if(strcmp(rbuf,"end")==0){break;}}//关闭套接字close(rfd);return 0;
}
流式套接字模型:
服务器:
#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;}if(listen(sfd,128)==-1){perror("listen error");return -1;}//客户端地址信息结构体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;}char rbuf[128]="";while(1){bzero(rbuf,sizeof(rbuf));recv(newfd,rbuf,sizeof(rbuf),0);printf("[%s]:%s\n",cun.sun_path,rbuf);if(strcmp(rbuf,"end")==0){break;}strcat(rbuf,"*_*");send(newfd,rbuf,strlen(rbuf),0);printf("发送成功\n");}close(sfd);close(newfd);return 0;
}
客户端:
#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;}//服务器地址信息结构体struct sockaddr_un sun;sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./mysocket");//连接客户端if(connect(cfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("connect error");return -1;}//收发数据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);puts("发送成功");if(strcmp(wbuf,"end")==0){break;}bzero(wbuf,sizeof(wbuf));recv(cfd,wbuf,sizeof(wbuf),0);printf("收到服务器回发的消息是:%s\n",wbuf);}//关闭套接字close(cfd);return 0;
}
报式套接字 模型:
服务器:
#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;}//判断套接字if(access("./socket",F_OK)==0){//存在该文件,需要删除该文件if(unlink("./socket")==-1){perror("unlink error");return -1;}}//绑定套接字文件//服务器端地址信息结构体struct sockaddr_un sun;sun.sun_family=AF_UNIX;//地址族strcpy(sun.sun_path,"./socket");//套接字文件if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("bind error");return -1;}//客户端地址信息结构体struct sockaddr_un cun;socklen_t socklen=sizeof(cun);char rbuf[128]="";while(1){bzero(rbuf,sizeof(rbuf));recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cun,&socklen);printf("[%s]:%s\n",cun.sun_path,rbuf);if(strcmp(rbuf,"end")==0){break;}strcat(rbuf,"*_*");sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cun,socklen);printf("发送成功\n");}close(sfd);return 0;
}
客户端:
#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;}//判断套接字if(access("./socket1",F_OK)==0){//存在该文件,需要删除该文件if(unlink("./socket1")==-1){perror("unlink error");return -1;}}//绑定套接字文件//客户端地址信息结构体struct sockaddr_un cun;cun.sun_family=AF_UNIX;//地址族strcpy(cun.sun_path,"./socket1");//套接字文件if(bind(cfd,(struct sockaddr*)&cun,sizeof(cun))==-1){perror("bind error");return -1;}//服务器地址信息结构体struct sockaddr_un sun;sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./socket");char wbuf[128]="";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");if(strcmp(wbuf,"end")==0){break;}//接收服务器回复的消息bzero(wbuf, sizeof(wbuf));recvfrom(cfd, wbuf, sizeof(wbuf), 0, NULL, NULL);printf("收到服务器回发的消息为:%s\n", wbuf);}close(cfd);return 0;
}
思维导图