广播
#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、将套接字设置成允许广播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(6789);cin.sin_addr.s_addr = inet_addr("192.168.125.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*)&cin, sizeof(cin));printf("发送成功\n");}//6、关闭套接字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;}//2、填充地址信息结构体struct sockaddr_in rin;rin.sin_family = AF_INET;rin.sin_port = htons(6789); //端口号rin.sin_addr.s_addr = inet_addr("192.168.125.255"); //广播地址//3、绑定if(bind(rfd, (struct sockaddr*)&rin, sizeof(rin)) == -1){perror("bind error");return -1;}//4、接收消息char rbuf[128] = "";while(1){//清空数组bzero(rbuf , sizeof(rbuf));//接收消息recvfrom(rfd, rbuf, sizeof(rbuf), 0, 0, 0);printf("收到消息为:%s\n", rbuf);}//5、关闭套接字close(rfd);return 0;
}
组播
#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(9999);sin.sin_addr.s_addr = inet_addr("224.1.2.3");//4、发送消息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");}//5、关闭套接字close(sfd);return 0;
}
流式域套接字
#include<myhead.h>int main(int argc, const char *argv[])
{//1、创建套接字int sfd = socket(AF_UNIX, SOCK_STREAM, 0);//int sfd = socket(AF_UNIX, SOCK_RAW, IPPROTO_TCP);if(sfd == -1){perror("socket 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;}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 = 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]: %s\n", cun.sun_path ,buf);}//7、关闭套接字close(newfd);close(sfd);return 0;
}
#include<myhead.h>int main(int argc, const char *argv[])
{//1、创建套接字int sfd = socket(AF_UNIX, SOCK_STREAM, 0);//int sfd = socket(AF_UNIX, SOCK_RAW, IPPROTO_TCP);if(sfd == -1){perror("socket 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;}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 = 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]: %s\n", cun.sun_path ,buf);}//7、关闭套接字close(newfd);close(sfd);return 0;
}
报式域套接字
#include<myhead.h>int main(int argc, const char *argv[])
{//1、创建套接字int sfd = socket(AF_UNIX, SOCK_STREAM, 0);//int sfd = socket(AF_UNIX, SOCK_RAW, IPPROTO_TCP);if(sfd == -1){perror("socket 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;}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 = 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]: %s\n", cun.sun_path ,buf);}//7、关闭套接字close(newfd);close(sfd);return 0;
}
#include<myhead.h>int main(int argc, const char *argv[])
{//1、创建套接字int sfd = socket(AF_UNIX, SOCK_STREAM, 0);//int sfd = socket(AF_UNIX, SOCK_RAW, IPPROTO_TCP);if(sfd == -1){perror("socket 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;}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 = 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]: %s\n", cun.sun_path ,buf);}//7、关闭套接字close(newfd);close(sfd);return 0;
}