文章目录
- 4.32UDP通信实现
- udp_client.c
- udp_server.c
- 4.33广播
- bro_server.c
- bro_client.c
- 4.34组播
- multi_server.c
- multi_client.c
- 4.35本地套接字通信
- ipc_server.c
- ipc_client.c
4.32UDP通信实现
udp_client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>int main() {// 1.创建一个通信的socketint fd = socket(PF_INET, SOCK_DGRAM, 0);if(fd == -1) {perror("socket");exit(-1);} // 服务器的地址信息struct sockaddr_in saddr;saddr.sin_family = AF_INET;saddr.sin_port = htons(9999);inet_pton(AF_INET, "127.0.0.1", &saddr.sin_addr.s_addr);int num = 0;// 3.通信while(1) {// 发送数据char sendBuf[128];sprintf(sendBuf, "hello , i am client %d \n", num++);sendto(fd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *)&saddr, sizeof(saddr));// 接收数据int num = recvfrom(fd, sendBuf, sizeof(sendBuf), 0, NULL, NULL);printf("server say : %s\n", sendBuf);sleep(1);}close(fd);return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/9a33040c7d40471594d952acaba17255.png)
udp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>int main() {// 1.创建一个通信的socketint fd = socket(PF_INET, SOCK_DGRAM, 0);if(fd == -1) {perror("socket");exit(-1);} struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(9999);addr.sin_addr.s_addr = INADDR_ANY;// 2.绑定int ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));if(ret == -1) {perror("bind");exit(-1);}// 3.通信while(1) {char recvbuf[128];char ipbuf[16];struct sockaddr_in cliaddr;int len = sizeof(cliaddr);// 接收数据int num = recvfrom(fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&cliaddr, &len);printf("client IP : %s, Port : %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, ipbuf, sizeof(ipbuf)),ntohs(cliaddr.sin_port));printf("client say : %s\n", recvbuf);// 发送数据sendto(fd, recvbuf, strlen(recvbuf) + 1, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));}close(fd);return 0;
}
4.33广播
向子网中多台计算机发送消息,并且子网中所有的计算机都可以接收到发送方发送的消息,每个广播消息都包含一个特殊的IP地址,这个IP中子网内主机标志部分的二进制全部为1.
a、只能在局域网中使用
b、客户端需要绑定服务器广播使用的端口,才可以接收到广播消息
//设置广播属性的函数
int setsockopt(int sockfd,int level,int optname,const void *optval,socklen_t optlen);-sockfd:文件描述符-level:SOL_SOCKET-optname:SO_BROADCAST-optval:int类型的值,为1表示允许广播-optlen:optval的大小
bro_server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>int main(){//1、创建一个通信的socketint fd=socket(PF_INET,SOCK_DGRAM,0);if(fd==-1){perror("socket");exit(-1);}//2、设置广播属性int op=1;setsockopt(fd,SOL_SOCKET,SO_BROADCAST,&op,sizeof(op));//3、创建一个广播的地址struct sockaddr_in cliaddr;cliaddr.sin_family=AF_INET;cliaddr.sin_port=htons(9999);inet_pton(AF_INET,"192.168.193.255",&cliaddr.sin_addr.s_addr);//4、通信int num=0;while(1){char sendBuf[128];sprintf(sendBuf,"hello,client...%d\n",num++);//发送数据sendto(fd,sendBuf,strlen(sendBuf)+1,0,(struct sockaddr*) &cliaddr,sizeof(cliaddr));printf("广播的数据:%s\n",sendBuf);sleep(1);}close(fd);return 0;
}
bro_client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>int main() {// 1.创建一个通信的socketint fd = socket(PF_INET, SOCK_DGRAM, 0);if(fd == -1) {perror("socket");exit(-1);} struct in_addr in;// 2.客户端绑定本地的IP和端口struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(9999);addr.sin_addr.s_addr = INADDR_ANY;int ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));if(ret == -1) {perror("bind");exit(-1);}// 3.通信while(1) {char buf[128];// 接收数据int num = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);printf("server say : %s\n", buf);}close(fd);return 0;
}
4.34组播
multi_server.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>int main() {// 1.创建一个通信的socketint fd = socket(PF_INET, SOCK_DGRAM, 0);if(fd == -1) {perror("socket");exit(-1);} // 2.设置多播的属性,设置外出接口struct in_addr imr_multiaddr;// 初始化多播地址inet_pton(AF_INET, "239.0.0.10", &imr_multiaddr.s_addr);setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &imr_multiaddr, sizeof(imr_multiaddr));// 3.初始化客户端的地址信息struct sockaddr_in cliaddr;cliaddr.sin_family = AF_INET;cliaddr.sin_port = htons(9999);inet_pton(AF_INET, "239.0.0.10", &cliaddr.sin_addr.s_addr);// 3.通信int num = 0;while(1) {char sendBuf[128];sprintf(sendBuf, "hello, client....%d\n", num++);// 发送数据sendto(fd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));printf("组播的数据:%s\n", sendBuf);sleep(1);}close(fd);return 0;
}
multi_client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>int main() {// 1.创建一个通信的socketint fd = socket(PF_INET, SOCK_DGRAM, 0);if(fd == -1) {perror("socket");exit(-1);} struct in_addr in;// 2.客户端绑定本地的IP和端口struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(9999);addr.sin_addr.s_addr = INADDR_ANY;int ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));if(ret == -1) {perror("bind");exit(-1); }struct ip_mreq op;inet_pton(AF_INET, "239.0.0.10", &op.imr_multiaddr.s_addr);op.imr_interface.s_addr = INADDR_ANY;// 加入到多播组setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &op, sizeof(op));// 3.通信while(1) {char buf[128];// 接收数据int num = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);printf("server say : %s\n", buf);}close(fd);return 0;
}
4.35本地套接字通信
本地套接字的作用:本地的进程间通信
有关系的进程间的通信
没有关系的进程间的通信
本地套接字实现流程和网络套接字类似,一般采用TCP通信流程
//本地套接字通信的流程 -tcp
//服务器端
1、创建监听的套接字int lfd=socket(AF_UNIX/AF_LOCAL,SOCK_STREAM,0);
2、监听的套接字绑定本地的套接字文件->server端struct sockaddr_un addr;//绑定成之后,指定的sun_path中的套接字文件会自动生成bind(lfd,addr,len);
3、监听listen(lfd,100);
4、等待并接受连接请求struct sockaddr_un cliaddr;int len=sizeof(cliaddr);int cfd=accept(lfd,&cliaddr,len);
5、通信接收数据:read/rev发送数据:write/send
6、关闭连接close()//客户端的流程
1、创建通信的套接字int fd=socket(AF_UNIX/AF_LOCAL,SOCK_STREAM,0);
2、监听的套接字绑定本地的IP端口struct sockaddr_un addr;//绑定成功之后,指定的sun_path中的套接字文件会自动生成bind(lfd,addr,len);
3、连接服务端struct sockaddr_un serveraddr;connect(fd,&serveraddr,sizeof(serveraddr));
4、通信接收数据:read/recv发送数据:write/send
5、关闭连接close()
//头文件:sys/un.h
#define UNIX_PATH_MAX 108
struct sockaddr_un{sa_family_t sun_family;//地址族协议 af_localchar sun_path[UNIX_PATH_MAX];//套接字文件的路径,这是一个伪文件,大小永远=0
}
ipc_server.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/un.h>int main() {unlink("server.sock");// 1.创建监听的套接字int lfd = socket(AF_LOCAL, SOCK_STREAM, 0);if(lfd == -1) {perror("socket");exit(-1);}// 2.绑定本地套接字文件struct sockaddr_un addr;addr.sun_family = AF_LOCAL;strcpy(addr.sun_path, "server.sock");int ret = bind(lfd, (struct sockaddr *)&addr, sizeof(addr));if(ret == -1) {perror("bind");exit(-1);}// 3.监听ret = listen(lfd, 100);if(ret == -1) {perror("listen");exit(-1);}// 4.等待客户端连接struct sockaddr_un cliaddr;int len = sizeof(cliaddr);int cfd = accept(lfd, (struct sockaddr *)&cliaddr, &len);if(cfd == -1) {perror("accept");exit(-1);}printf("client socket filename: %s\n", cliaddr.sun_path);// 5.通信while(1) {char buf[128];int len = recv(cfd, buf, sizeof(buf), 0);if(len == -1) {perror("recv");exit(-1);} else if(len == 0) {printf("client closed....\n");break;} else if(len > 0) {printf("client say : %s\n", buf);send(cfd, buf, len, 0);}}close(cfd);close(lfd);return 0;
}
ipc_client.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/un.h>int main() {unlink("client.sock");// 1.创建套接字int cfd = socket(AF_LOCAL, SOCK_STREAM, 0);if(cfd == -1) {perror("socket");exit(-1);}// 2.绑定本地套接字文件struct sockaddr_un addr;addr.sun_family = AF_LOCAL;strcpy(addr.sun_path, "client.sock");int ret = bind(cfd, (struct sockaddr *)&addr, sizeof(addr));if(ret == -1) {perror("bind");exit(-1);}// 3.连接服务器struct sockaddr_un seraddr;seraddr.sun_family = AF_LOCAL;strcpy(seraddr.sun_path, "server.sock");ret = connect(cfd, (struct sockaddr *)&seraddr, sizeof(seraddr));if(ret == -1) {perror("connect");exit(-1);}// 4.通信int num = 0;while(1) {// 发送数据char buf[128];sprintf(buf, "hello, i am client %d\n", num++);send(cfd, buf, strlen(buf) + 1, 0);printf("client say : %s\n", buf);// 接收数据int len = recv(cfd, buf, sizeof(buf), 0);if(len == -1) {perror("recv");exit(-1);} else if(len == 0) {printf("server closed....\n");break;} else if(len > 0) {printf("server say : %s\n", buf);}sleep(1);}close(cfd);return 0;
}