UDP广播服务端(发送)
server.c
#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 SER_IP "192.168.0.255" // 广播iP
// #define IP "192.168.0.185"
// #define IP "0.0.0.0"
#define SER_PORT 1562
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);
int broad = 1;
if (setsockopt(cfd, SOL_SOCKET, SO_BROADCAST, &broad, sizeof(broad)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
/
// 获取接收方地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; // 必须填AF_INET
sin.sin_port = htons(SER_PORT); // 端口号,1024~49151
sin.sin_addr.s_addr = inet_addr(SER_IP); // 本机IP地址,ifconfig
struct sockaddr_in rcvaddr;
socklen_t addrlen = sizeof(rcvaddr);
char buf[128] = "";
int res = 0;
while (1)
{
bzero(buf, sizeof(buf));
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
printf("buf=%s\n", buf);
// 发送数据给服务器
if (sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
}
close(cfd);
return 0;
}
UDP广播客户端(接受)
client.c
#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.0.255" // 广播iP
#define PORT 1562 // 端口
int main(int argc, const char *argv[])
{
// 创建流式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("sfd=%d\n", sfd);
// 获取接收方地址信息结构体
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地址,ifconfig
// 绑定,必须绑定
if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
struct sockaddr_in cin; // 储存数据包从哪来的
socklen_t addrlen = sizeof(cin);
char buf[128] = "";
int res = 0;
while (1)
{
printf("等待消息...\n");
bzero(buf, sizeof(buf));
// 接收信息,必须接受数据奥的发送方地址信息给snedto使用
res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&cin, &addrlen);
if (res < 0)
{
ERR_MSG("recvfeom");
return -1;
}
printf("[%s :%d]:%s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
}
close(sfd);
return 0;
}
组播服务端(接收)
server.c
#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.8.135" // 本机ip
#define GRP_IP "224.1.1.1" // 组播ip
#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;
}
//加入多播组
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");
// 填充地址信息结构体
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(GRP_IP); // 本机IP地址,ifconfig
// 绑定,必须绑定的
if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
struct sockaddr_in cin; // 数据包是从谁那里来的
socklen_t addrlen = sizeof(cin);
char buf[128] = "";
ssize_t res = 0;
while (1)
{
printf("等待消息...\n");
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;
}
组播客户端(发送)
client.c
#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.8.135" // 本机ip
#define GRP_IP "224.1.1.1" // 组播ip
#define PORT 6666 // 端口
int main(int argc, const char *argv[])
{
// 创建报式套接字
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if (cfd < 0)
{
ERR_MSG("socket");
return -1;
}
// 填充地址信息结构体
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(GRP_IP); // 本机IP地址,ifconfig
// 绑定,非必须绑定的
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;
}