1.recv_brodcast
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
int main(int argc,char **argv)
{
if(argc != 3)
{
printf("请给我IP地址和端口号\n");
return -1;
}
int main(int argc, char **argv)
{
//1.创建套接字(默认关闭广播功能)
int sock_fd = socket(AF_INET,SOCK_DGRAM,0);
//2.套接字开启广播功能
int bt = 1;
socklen_t len = sizeof(bt);
int ret = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &bt, len);
//选项级别 //级别中的选项名 //选项值 //选项值的大小
if(-1 == ret)
{
perror("set socket failed");
return -1;
}
//3.绑定地址
struct sockaddr_in local, broadcast;
local.sin_family = AF_INET;
local.sin_port = htons(atoi(argv[1]));
local.sin_addr.s_addr = INADDR_ANY;
broadcast.sin_family = AF_INET;
broadcast.sin_port = htons(atoi(argv[2]));
broadcast.sin_addr.s_addr = inet_addr("224.10.10.10");
socklen_t local_len = sizeof(local);
socklen_t broadcast_len = sizeof(broadcast);
bind(sock_fd,(struct sockaddr *)&local,local_len);
char *buf = malloc(100);
while(1)
{
memset(buf,0,100);
fgets(buf,100,stdin);
//4.向主播地址发送消息
sendto(sock_fd,buf,100,0,(struct sockaddr *)&broadcast,broadcast_len);
}
}
2.mulit_recv
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
/*
struct ip_mreq
{
struct in_addr imr_multiaddr; //组播地址
struct in_addr imr_interface; //本机地址
}
struct in_addr imr_multiaddr
{
s_addr;//组播地址
}
struct in_addr imr_interface
{
s_addr;//本机地址
}
*/
int main(int argc,char **argv)
{
if(argc != 3)
{
printf("请给我IP地址和端口号\n");
return -1;
}
int main(int argc,char **argv)
{
//1.创建套接字
int sock_fd = socket(AF_INET,SOCK_DGRAM,0);
//2.设置套接字的属性
//定义组播地址结构体,给组播地址成员赋值,给本机地址成员赋值
struct ip_mreq zubo;
zubo.imr_multiaddr.s_addr = inet_addr("224.10.10.10");//224.0.0.0-----238.255.255.255
zubo.imr_interface.s_addr = INADDR_ANY;
socklen_t zubo_len = sizeof(zubo);
setsockopt(sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &zubo, zubo_len);
//3.绑定
struct sockaddr_in local,opposite;
local.sin_family = AF_INET;
local.sin_port = htons(atoi(argv[1]));//10000
local.sin_addr.s_addr = INADDR_ANY;
socklen_t local_len = sizeof(local);
socklen_t broadcast_len = sizeof(broadcast);
bind(sock_fd,(struct sockaddr *)&local,local_len);
char *buf = malloc(100);
while(1)
{
memset(buf,0,100);
recvfrom(sock_fd,buf,100,0,(struct sockaddr *)&broadcast,&broadcast_len);
printf("from multi:%s\n",buf);
}
if (setsockopt(sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &zubo, zubo_len) == -1) {
perror("from multi:%s\n");
return 1;
}
}
3.mulit_send
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
int main(int argc,char **argv)
{
if(argc != 3)
{
printf("请给我IP地址和端口号\n");
return -1;
}
int main(int argc,char **argv)
{
//1.创建套接字
int sock_fd = socket(AF_INET,SOCK_DGRAM,0);
//2.绑定
struct sockaddr_in zubo;
zubo.sin_family = AF_INET;
zubo.sin_port = htons(atoi(argv[1]));
zubo.sin_addr.s_addr = inet_addr("224.10.10.10");
socklen_t zubo_len = sizeof(zubo);
bind(sock_fd,(struct sockaddr *)&zubo,zubo_len);
char *buf = malloc(100);
while(1)
{
memset(buf,0,100);
fgets(buf,100,stdin);
sendto(sock_fd,buf,100,0,(struct sockaddr *)&zubo,zubo_len);
}
}