作业1:广播
发送端:
#include <myhead.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;}printf("sfd=%d\n",sfd);//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 sin;sin.sin_family=AF_INET;sin.sin_port=(8888); //广播端口号sin.sin_addr.s_addr=inet_addr("192.168.199.255"); //广播地址//5.发送数据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");}//6.关闭套接字close(sfd);return 0;
}
接收端:
#include <myhead.h>
int main(int argc, const char *argv[])
{//1.创建套接字int cfd=socket(AF_INET,SOCK_DGRAM,0);if(cfd==-1){perror("socket error");return -1;}printf("cfd=%d\n",cfd);//2.绑定//填充地址信息结构体struct sockaddr_in cin;cin.sin_family=AF_INET;cin.sin_port=(8888); //广播端口号cin.sin_addr.s_addr=inet_addr("192.168.199.255"); //广播地址//绑定if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1){perror("bind error");return -1;}printf("bind success\n");//3.接收数据char rbuf[128]="";while(1){//清空数据bzero(rbuf,sizeof(rbuf));//读取消息recvfrom(cfd,rbuf,sizeof(rbuf),0,NULL,NULL);printf("收到的消息为:%s\n",rbuf);}//4.关闭套接字close(cfd);return 0;
}
效果图:
作业2:组播
发送端:
#include <myhead.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.绑定(非必须)//3.发送消息//填充地址信息结构体struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(8888); //组播端口号sin.sin_addr.s_addr=inet_addr("224.1.1.2"); //组播IPchar 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 <myhead.h>
int main(int argc, const char *argv[])
{//1.创建套接字int rfd=socket(AF_INET,SOCK_DGRAM,0);if(rfd==-1){perror("socket error");return -1;}printf("rfd=%d\n",rfd);//2.加入多播组struct ip_mreqn imr;imr.imr_multiaddr.s_addr=inet_addr("224.1.1.2"); //组播ipimr.imr_address.s_addr=inet_addr("192.168.199.131"); //本机ipimr.imr_ifindex=2; //网卡编号if(setsockopt(rfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&imr,sizeof(imr))==-1){perror("setsockopt error");return -1;}//3.绑定//填充地址信息结构体struct sockaddr_in rin;rin.sin_family=AF_INET;rin.sin_port=htons(8888); //组播端口号rin.sin_addr.s_addr=inet_addr("224.1.1.2"); //组播ip//绑定if(bind(rfd,(struct sockaddr*)&rin,sizeof(rin))==-1){perror("bind error");return -1;}printf("bind success\n");//4.接收组播消息char rbuf[128]="";while(1){//清空消息bzero(rbuf,sizeof(rbuf));//接收消息recvfrom(rfd,rbuf,sizeof(rbuf),0,NULL,NULL);printf("收到的消息为:%s\n",rbuf);}//5.关闭套接字close(rfd);return 0;
}
效果图:
作业3:流式域套接字
服务器端:
#include <myhead.h>
int main(int argc, const char *argv[])
{//1.创建套接字int sfd=socket(AF_UNIX,SOCK_STREAM,0);if(sfd==-1){perror("socket error");return -1;}//2.判断文件是否存在,存在则删除if(access("./mysocket",F_OK)==0){//存在,删除if(unlink("./mysocket")==-1){perror("unlink error");return -1;}}//3.绑定//填充地址信息结构体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");//4.开启监听if(listen(sfd,128)==-1){perror("listen error");return -1;}//5.阻塞等待客户端连接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");//6.进行数据收发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");}//7.关闭套接字close(sfd);close(newfd);return 0;
}
客户端:
#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;}//2.判断文件是否存在,存在则删除if(access("./mysocket1",F_OK)==0){//存在,删除if(unlink("./mysocket1")==-1){perror("unlink error");return -1;}}//3.绑定(非必须)//填充地址信息结构体struct sockaddr_un sun;sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./mysocket1");//绑定if(bind(cfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("bind error");return -1;}printf("bind success\n");//4.连接服务器struct sockaddr_un cun;cun.sun_family=AF_UNIX;strcpy(cun.sun_path,"./mysocket");if(connect(cfd,(struct sockaddr*)&cun,sizeof(cun))==-1){perror("connect error");return -1;}printf("connect success\n");//5.进行数据收发char wbuf[128]="";while(1){//清空数据bzero(wbuf,sizeof(wbuf));//从终端获取数据printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;if(strcmp(wbuf,"quit")==0){break;}//发送消息send(cfd,wbuf,strlen(wbuf),0);printf("发送成功\n");//接收服务器发来的消息bzero(wbuf,sizeof(wbuf));recv(cfd,wbuf,sizeof(wbuf),0);printf("收到的消息为:%s\n",wbuf);}//7.关闭套接字close(cfd);return 0;
}
效果图:
作业4:报式域套接字
服务器端:
#include <myhead.h>
int main(int argc, const char *argv[])
{//1.创建套接字int sfd=socket(AF_UNIX,SOCK_DGRAM,0);if(sfd==-1){perror("socket error");return -1;}//2.判断文件是否存在,存在则删除if(access("./linux",F_OK)==0){//存在,删除if(unlink("./linux")==-1){perror("unlink error");return -1;}}//3.绑定//填充地址信息结构体struct sockaddr_un sun;sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./linux");//绑定if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("bind error");return -1;}printf("bind success\n");//6.进行数据收发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," !!!");//发送消息sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cun,socklen);}//7.关闭套接字close(sfd);return 0;
}
客户端:
#include <myhead.h>int main(int argc, const char *argv[])
{//1.创建套接字int cfd=socket(AF_UNIX,SOCK_DGRAM,0);if(cfd==-1){perror("socket error");return -1;}//2.判断文件是否存在,存在则删除if(access("./linux1",F_OK)==0){//存在,删除if(unlink("./linux1")==-1){perror("unlink error");return -1;}}//3.绑定//填充地址信息结构体struct sockaddr_un sun;sun.sun_family=AF_UNIX;strcpy(sun.sun_path,"./linux1");//绑定if(bind(cfd,(struct sockaddr*)&sun,sizeof(sun))==-1){perror("bind error");return -1;}printf("bind success\n");//5.进行数据收发char wbuf[128]="";//定义地址信息结构体struct sockaddr_un cun;cun.sun_family=AF_UNIX;strcpy(cun.sun_path,"linux");while(1){//清空数据bzero(wbuf,sizeof(wbuf));//从终端获取数据printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;if(strcmp(wbuf,"quit")==0){break;}//发送消息sendto(cfd,wbuf,strlen(wbuf),0,(struct sockaddr*)&cun,sizeof(cun));printf("发送成功\n");//接收服务器发来的消息bzero(wbuf,sizeof(wbuf));recvfrom(cfd,wbuf,sizeof(wbuf),0,NULL,NULL);printf("收到的消息为:%s\n",wbuf);}//7.关闭套接字close(cfd);return 0;
}
效果图: