UDP通信
server.c
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main(){
//1.创建一个通信的socket
int 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;
}
client.c
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main(){
//1.创建一个通信的socket
int 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;
}
广播
向子网中多台计算机发送消息,只能向局域网发送。
每个广播消息都包含一个特殊的IP地址,这个IP中子网内主机标志部分的二进制全部为1。
192.168.10.最后一位255可以发给所有人
客户端需要绑定服务器广播使用的端口
客户端从部分数据才开始接收
client.c
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main(){
//1.创建一个通信的socket
int fd=socket(PF_INET,SOCK_DGRAM,0);
if(fd==-1){
perror("socket");
exit(-1);
}
//客户端绑定本地的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);
}
int num=0;
//3.通信
while(1){
char buf[128];
//接收数据
int num=recvfrom(fd,buf,sizeof(buf),0,NULL,NULL);
printf("server say:%s\n",buf);
sleep(1);
}
close(fd);
return 0;
}
server.c
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main(){
//1.创建一个通信的socket
int 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.227.255",&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;
}
组播
组播既可以用于局域网,也可以用于广域网
客户端需要加入多播组,才能接收到多播的数据
client.c
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main(){
//1.创建一个通信的socket
int fd=socket(PF_INET,SOCK_DGRAM,0);
if(fd==-1){
perror("socket");
exit(-1);
}
//客户端绑定本地的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));
int num=0;
//3.通信
while(1){
char buf[128];
//接收数据
int num=recvfrom(fd,buf,sizeof(buf),0,NULL,NULL);
printf("server say:%s\n",buf);
sleep(1);
}
close(fd);
return 0;
}
server.c
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main(){
//1.创建一个通信的socket
int 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;
}