1.广播
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg)do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while (0);
#define IP "192.168.9.255"
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("%d\n",sfd);
//填充接收方本身的地址
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定成功\n");
//存储数据包来源的地址
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
//recvfrom 接收广播数据
char buf[128]="";
ssize_t res=0;
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;
}
发送端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define IP "192.168.9.255"
#define PORT 6666
#define ERR_MSG(msg)do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0);
int main(int argc, const char *argv[])
{
//创建套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
//setsockopt 设置允许广播
int broad=1;
if(setsockopt(cfd,SOL_SOCKET,SO_BROADCAST,&broad,sizeof(broad))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("set sockopt success\n");
//非必须绑定
//填充接受方的地址信息结构体,给sendto函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//sendto函数
char buf[128]="";
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;//将\n改为0;
res=sendto(cfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin));
if(res<0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
}
return 0;
}
2.组播
接收端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0);
#define GRP_IP "224.1.2.3" //组播IP ifconfig 224.0.0.0~239.255.255.255
#define IP "192.168.0.90"
#define PORT 6666 //1024-49151
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//加入多播组
struct ip_mreqn mq;
mq.imr_multiaddr.s_addr = inet_addr(GRP_IP);
mq.imr_address.s_addr = inet_addr(IP);
mq.imr_ifindex = 2;
if(setsockopt(sfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mq,sizeof(mq))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("加入多播组成功\n");
//填充接收方本身的地址信息结构体
//真实的地址信息结构体根据地址族指定 AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INEF
sin.sin_port =htons(PORT); //端口号,1024-49151
sin.sin_addr.s_addr =inet_addr(GRP_IP); //组播IP地址,与加入的组播IP一致
//将IP和端口绑定到套接字上
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
struct sockaddr_in cin; //存储连接成功的客户端地址信息
socklen_t addrlen=sizeof(cin);
//阻塞函数,从已完成连接的队列头中获取一个客户端信息,生成一个新的文件描述符
//该文件描述符才是与客户端通信的文件描述符
char buf[128]="";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
//接受数据,必须接受数据包的发送放地址信息,给sendto使用
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] %s __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf,__LINE__);
}
close(sfd);
return 0;
}
发送端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0);
#define SER_IP "224.1.2.3" //组播IP ifconfig
#define SER_PORT 6666 //1024-49151
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("cfd = %d\n",cfd);
//填充地址信息结构体
//真实的地址信息结构体根据地址族指定 AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INEF
sin.sin_port =htons(SER_PORT); //端口号,1024-49151
sin.sin_addr.s_addr =inet_addr(SER_IP); //本机IP
struct sockaddr_in sinaddr; //存储连接成功的服务端地址信息
socklen_t addrlen=sizeof(sinaddr);
char buf[128]="";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
//发送 将数据拼接字符串后发送回去
printf("请输入>>\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin))<0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
close(cfd);
return 0;
}