组播通信
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:__%d__",__LINE__);\
perror(msg);\
}while(0)
#define GRP_PORT 2222 //端口号 范围1024~49151
#define GRP_IP "224.1.8.2" //组播IP 224.0.0.0~239.255.25.255 与接收方填一致
int main(int argc, const char *argv[])
{
//创建流式套接字-->TCP
int sfd=socket(AF_INET, SOCK_DGRAM, 0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("sockfd = %d\n",sfd);
//填充地址信息结构体在 man 7 ip
//AF_INET -->man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(GRP_PORT);//1024~49151
sin.sin_addr.s_addr = inet_addr(GRP_IP);//本机IP
char buf[128]="";
ssize_t res=0;
//存储客户端的地址信息
//struct sockaddr_in cin;//存储发送方的地址信息
socklen_t addrlen =sizeof(sin);
while(1){
//将数据发送给服务器
bzero(buf,sizeof(buf));
printf("输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
//关闭
close(sfd);
return 0;
}
include
#include
#include
#include
#include
#include
#include
#include
//搭建流程 socket创建套接字-->bind锁定服务器的ip和端口-->listen将套接字设置为被监听状态
//-->accept获取连接成功后的套接字-->recv接收客户端的信息-->发送信息给客户端-->关闭套接字
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:__%d__",__LINE__);\
perror(msg);\
}while(0)
#define GRP_PORT 2222 //端口号 范围1024~49151
#define GRP_IP "224.1.8.2" //本机IP在终端输入ifconfig查看
int main(int argc, const char *argv[])
{
//创建流式套接字-->TCP
int sfd=socket(AF_INET, SOCK_DGRAM, 0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("sockfd = %d\n",sfd);
//填充地址信息结构体在 man 7 ip
//AF_INET -->man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(GRP_PORT);//1024~49151
sin.sin_addr.s_addr = inet_addr(GRP_IP);//本机IP
//功能:将IP地址和端口号绑定到指定套接字中
if( bind(sfd, (struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
printf("绑定成功 __%d__\n",__LINE__);
//加入多播组
struct ip_mreqn mq;
mq.imr_multiaddr.s_addr = inet_addr(GRP_IP);//组播IP的网络字节序
mq.imr_address.s_addr = inet_addr("192.168.8.250");//本机IP
mq.imr_ifindex = if_nametoindex("ens33");//网卡设备索引号
if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mq,sizeof(mq))<0){
ERR_MSG("setsockopt");
return -1;
}
printf("加入多播组成功\n");
char buf[128]="";
ssize_t res=0;
//存储客户端的地址信息
struct sockaddr_in cin;//存储发送方的地址信息
socklen_t addrlen =sizeof(cin);
while(1){
//接收
bzero(buf,sizeof(buf));
res =recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
if(res<0){
ERR_MSG("recv");
return -1;
}
printf("[%s | %d] :%s\n",inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),buf);
}
//关闭
close(sfd);
return 0;
}
广播
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:__%d__ ", __LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 2222 //接收放绑定的一致
#define SER_IP "192.168.8.255" //广播IP
int main(int argc, const char *argv[])
{
//创建套接字 socket
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//设置允许广播
int broad = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &broad, sizeof(int)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("broadcast set success\n");
//服务器的地址信息结构体,供给下面的sendto函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(SER_PORT); //接收放绑定的一致
sin.sin_addr.s_addr = inet_addr(SER_IP); //广播IP
char buf[128] = "";
ssize_t res = 0;
struct sockaddr_in rcvaddr; //存储发送方的地址信息
socklen_t addrlen = sizeof(rcvaddr);
while(1)
{
//将数据发送给服务器
bzero(buf, sizeof(buf));
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
//要发送给服务器,所以地址信息结构体需要填服务器的
if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
//关闭文件描述符
close(sfd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:__%d__ ", __LINE__);\
perror(msg);\
}while(0)
#define PORT 2222 //1024~49151
#define IP "192.168.8.255" //广播IP
int main(int argc, const char *argv[])
{
//创建报式套接字 socket
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//服务器自身的地址信息结构体,真实的地址信息结构体根据AF_INET查找
//man 7 IP
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET
sin.sin_port = htons(PORT); //端口号,1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //广播IP
//绑定服务器的地址信息结构体 bind
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("server bind success\n");
char buf[128] = "";
ssize_t res = 0;
struct sockaddr_in cin; //存储发送方的地址信息
socklen_t addrlen = sizeof(cin);
while(1)
{
bzero(buf, sizeof(buf));
//接收客户端发送过来的数据
res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] :%s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
}
//关闭文件描述符
close(sfd);
return 0;
}