UDP廣播,組播服務器

 

 

 

廣播

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <net/if.h>
#define SERVER_PORT 8000
#define CLIENT_PORT 9000
#define MAXLINE 1500
#define BROADCAST_IP "192.168.32.255"

int main(void) { int sockfd; struct sockaddr_in serveraddr, clientaddr; char buf[MAXLINE] = "xingwenpeng\n"; /* 構造用於UDP通訊的套接字 */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); bzero(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET;            /* IPv4 */ serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);         /* 本地任意IP INADDR_ANY = 0 */ serveraddr.sin_port = htons(SERVER_PORT); bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); int flag = 1; setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag)); /*構造 client 地址 IP+端口 192.168.32.255+9000 */ bzero(&clientaddr, sizeof(clientaddr)); clientaddr.sin_family = AF_INET; inet_pton(AF_INET, BROADCAST_IP, &clientaddr.sin_addr.s_addr); clientaddr.sin_port = htons(CLIENT_PORT); int i = 0; while (1) { sprintf(buf, "itcast %d\n", i++); //fgets(buf, sizeof(buf), stdin);
        sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr)); sleep(1); } close(sockfd); return 0; }

clientsocket

#include <netinet/in.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <net/if.h>
#define SERVER_PORT 8000
#define MAXLINE 4096

#define CLIENT_PORT 9000
int main(int argc, char *argv[]) { struct sockaddr_in serveraddr, localaddr; int confd; ssize_t len; char buf[MAXLINE]; //1.建立一個socket
    confd = socket(AF_INET, SOCK_DGRAM, 0); //2.初始化本地端地址
    bzero(&localaddr, sizeof(localaddr)); localaddr.sin_family = AF_INET; inet_pton(AF_INET, "0.0.0.0" , &localaddr.sin_addr.s_addr); localaddr.sin_port = htons(CLIENT_PORT); bind(confd, (struct sockaddr *)&localaddr, sizeof(localaddr)); socklen_t slen; while (1) { slen = sizeof(serveraddr); len = recvfrom(confd, buf, sizeof(buf), 0, (struct sockaddr *)&serveraddr, &slen); printf("%s", buf); //printf("%s\n", inet_ntop(AF_INET, &serveraddr.sin_addr.s_addr, buf, sizeof(buf))); //write(STDOUT_FILENO, buf, len);
 } close(confd); return 0; }

 

 

組播

組播組能夠是永久的也能夠是臨時的。組播組地址中,有一部分由官方分配的,稱爲永久組播組。永久組播組保持不變的是它的ip地址,組中的成員構成能夠發生變化。spa

永久組播組中成員的數量均可以是任意的,甚至能夠爲零。那些沒有保留下來供永久組播組使用的ip組播地址,能夠被臨時組播組利用。
code

224.0.0.0224.0.0.255爲預留的組播地址(永久組地址),地址224.0.0.0保留不作分配,其它地址供路由協議 使用; 224.0.1.0224.0.1.255是公用組播地址,能夠用於Internet; 224.0.2.0238.255.255.255爲用戶可用的組播地址(臨時組地址),全網範圍內有效; 239.0.0.0239.255.255.255爲本地管理組播地址,僅在特定的本地範圍內有效。
ip ad
查看網卡編號
if_nametoindex

 

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <net/if.h>
#define SERVER_PORT 8000
#define CLIENT_PORT 9000
#define MAXLINE 1500

#define GROUP "239.0.0.2"

int main(void) { int sockfd; struct sockaddr_in serveraddr, clientaddr; char buf[MAXLINE] = "xingwenpeng\n"; struct ip_mreqn group; /* 構造用於UDP通訊的套接字 */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); bzero(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET;            /* IPv4 */ serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);         /* 本地任意IP INADDR_ANY = 0 */ serveraddr.sin_port = htons(SERVER_PORT); bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); /*設置組地址*/ inet_pton(AF_INET, GROUP, &group.imr_multiaddr); /*本地任意IP*/ inet_pton(AF_INET, "0.0.0.0", &group.imr_address); /* eth0 --> 編號 * 命令:ip ad */ group.imr_ifindex = if_nametoindex("eth0"); setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF, &group, sizeof(group)); /*構造 client 地址 IP+端口 */ bzero(&clientaddr, sizeof(clientaddr)); clientaddr.sin_family = AF_INET; /* IPv4 239.0.0.2+9000 */ inet_pton(AF_INET, GROUP, &clientaddr.sin_addr.s_addr); clientaddr.sin_port = htons(CLIENT_PORT); int i = 0; while (1) { sprintf(buf, "itcast %d\n", i++); //fgets(buf, sizeof(buf), stdin);
        sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr)); sleep(1); } close(sockfd); return 0; }

clientserver

#include <netinet/in.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <net/if.h>
#define SERVER_PORT 8000
#define MAXLINE 4096

#define CLIENT_PORT 9000
#define GROUP "239.0.0.2"
int main(int argc, char *argv[]) { struct sockaddr_in serveraddr, localaddr; int confd; ssize_t len; char buf[MAXLINE]; /* 組播結構體 */
    struct ip_mreqn group; //1.建立一個socket
    confd = socket(AF_INET, SOCK_DGRAM, 0); //2.初始化本地端地址
    bzero(&localaddr, sizeof(localaddr)); localaddr.sin_family = AF_INET; inet_pton(AF_INET, "0.0.0.0" , &localaddr.sin_addr.s_addr); localaddr.sin_port = htons(CLIENT_PORT); bind(confd, (struct sockaddr *)&localaddr, sizeof(localaddr)); /*設置組地址*/ inet_pton(AF_INET, GROUP, &group.imr_multiaddr); /*本地任意IP*/ inet_pton(AF_INET, "0.0.0.0", &group.imr_address); /* eth0 --> 編號 //命令:ip ad */ group.imr_ifindex = if_nametoindex("eth0"); /*設置client 加入多播組 */ setsockopt(confd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group, sizeof(group)); while (1) { len = recvfrom(confd, buf, sizeof(buf), 0, NULL, 0); write(STDOUT_FILENO, buf, len); } close(confd); return 0; }
相關文章
相關標籤/搜索