UDP广播:一对多数据发送

一、怎么样进行UDP一对多数据发送?

1.很容易想到的一种方法是:如果一个client 要向其他其他100个server发送数据,那应该怎么办?如果我知道其他100个 server的ip地址 和 端口号就可以了,那么我们在一对一的进行发送就可以了。但是这种方法有点低效率了,接下来介绍高效的方式。

二、UDP通信中的广播

1.广播是向同一网络中的所有主机传输数据的方法

2.广播类型

        2.1 直接广播:IP地址中除网络地址外,其余主机地址均设置为1。比如:一个ip地址为:192.168.1.xxx 这个ip地址的前三个字节 192.168.1 为网络字节(net id),后面一个字节xxx 为主机字节(host id),这个时候需要把 xxx 设置成255(其余主机地址均设置为1)

        2.2 本地广播:无需要知道网络地址,使用 255.255.255.255 作为IP地址使用

3.区别:本地广播不经过路由器寻址,直接发送到本地主机

 

/* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
   into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
   actual length.  Returns 0 on success, -1 for errors.  */
extern int getsockopt (int __fd, int __level, int __optname,
               void *__restrict __optval,
               socklen_t *__restrict __optlen) __THROW;

/* Set socket FD's option OPTNAME at protocol level LEVEL
   to *OPTVAL (which is OPTLEN bytes long).
   Returns 0 on success, -1 for errors.  */
extern int setsockopt (int __fd, int __level, int __optname,
               const void *__optval, socklen_t __optlen) __THROW;

server.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>int main()
{int server = 0;struct sockaddr_in saddr = {0};int client = 0;struct sockaddr_in remote = {0};socklen_t asize = 0;int len = 0;char buf[32] = "Software";int r = 0;int brd = 1;server = socket(PF_INET, SOCK_DGRAM, 0);if( server == -1 ){printf("server socket error\n");return -1;}saddr.sin_family = AF_INET;saddr.sin_addr.s_addr = htonl(INADDR_ANY);saddr.sin_port = htons(8888);if( bind(server, (struct sockaddr*)&saddr, sizeof(saddr)) == -1 ){printf("udp server bind error\n");return -1;}printf("udp server start success\n");remote.sin_family = AF_INET;// remote.sin_addr.s_addr = inet_addr("192.168.3.255");remote.sin_addr.s_addr = 0xFFFFFFFF;remote.sin_port = htons(9000);// brd = 0;setsockopt(server, SOL_SOCKET, SO_BROADCAST, &brd, sizeof(brd));while( 1 ){len = sizeof(remote);r = strlen(buf);sendto(server, buf, r, 0, (struct sockaddr*)&remote, len);sleep(1);}close(server);return 0;
}

client.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>int main()
{int sock = 0;struct sockaddr_in addr = {0};struct sockaddr_in remote = {0};int len = 0;char buf[128] = {0};char input[32] = {0};int r = 0;sock = socket(PF_INET, SOCK_DGRAM, 0);if( sock == -1 ){printf("socket error\n");return -1;}addr.sin_family = AF_INET;addr.sin_addr.s_addr = htonl(INADDR_ANY);addr.sin_port = htons(9000);if( bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1 ){printf("udp bind error\n");return -1;}remote.sin_family = AF_INET;remote.sin_addr.s_addr = inet_addr("127.0.0.1");remote.sin_port = htons(8888);while( 1 ){r = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr*)&remote, &len);if( r > 0 ){buf[r] = 0;printf("Receive: %s\n", buf);}else{break;}}close(sock);return 0;
}

本文链接:https://my.lmcjl.com/post/9565.html

展开阅读全文

4 评论

留下您的评论.