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("setsockeopt 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.1.255");//发送数据char sbuf[128]="";while(1){printf("please enter:");fgets(sbuf,sizeof(sbuf),stdin);sbuf[strlen(sbuf)-1]=0;sendto(sfd,sbuf,strlen(sbuf),0,(struct sockaddr*)&sin,sizeof(sin));printf("send success\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 sockaddr_in rin;rin.sin_family=AF_INET;rin.sin_port=htons(8888);rin.sin_addr.s_addr=inet_addr("192.168.1.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("u recv:%s\n",rbuf);}//关闭套接字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;}//绑定端口号和ip地址//填充地址信息结构体struct sockaddr_in rin;rin.sin_family=AF_INET;rin.sin_port=htons(7890);rin.sin_addr.s_addr=inet_addr("192.168.1.7");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("please enter:");fgets(sbuf,sizeof(sbuf),stdin);sbuf[strlen(sbuf)-1]=0;//向组播地址发送消息sendto(rfd,sbuf,strlen(sbuf),0,(struct sockaddr*)&sin,sizeof(sin));printf("send success\n");}//关闭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;}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.1.7");//本地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("u recv:%s\n",rbuf);}//关闭套接字close(rfd);return 0;
}
3、流式域套接字
发送端
#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," OvO");send(newfd,rbuf,strlen(rbuf),0);printf("send success\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;}printf("cfd=%d\n",cfd);//判断套接字文件是否存在,存在则删除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,"./mysocket1");//连接服务器if(connect(cfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("connect error");return -1;}printf("connect success\n");//数据收发char wbuf[128]="";while(1){//清空数据bzero(wbuf,sizeof(wbuf));printf("please enter:");fgets(wbuf,sizeof(wbuf),stdin);//终端输入wbuf[strlen(wbuf)-1]=0;//将数据发给服务器send(cfd,wbuf,strlen(wbuf),0);printf("send success\n");//判断发生的数据if(strcmp(wbuf,"quit")==0){break;}//接收服务器发来的数据//清空数据bzero(wbuf,sizeof(wbuf));recv(cfd,wbuf,sizeof(wbuf),0);printf("u recv:%s\n",wbuf);}//关闭套接字close(cfd);return 0;
}
4、报式域套接字
发送端
#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("u recv:%s\n",rbuf);//回复strcat(rbuf," OvO");if(sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cun,socklen)==-1){perror("sendto error");return -1;}}//关闭套接字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;}printf("cfd=%d\n",cfd);//判断套接字文件是否存在,存在需删除if(access("./linux2",F_OK)==0){if(unlink("./unix2")==-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("please enter:");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;//将数据发给服务器sendto(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&sun,sizeof(sun));printf("send success\n");//接收回复的消息bzero(wbuf,sizeof(wbuf));recvfrom(cfd,wbuf,sizeof(wbuf),0,NULL,NULL);printf("u recv:%s\n",wbuf);}//关闭套接字文件close(cfd);return 0;
}
5、思维导图