多播(组播)
组播组可以是永久的也可以是临时的。组播组地址中,有一部分由官方分配的,称为永久组播组。永久组播组保持不变的是它的ip地址,组中的成员构成可以发生变化。永久组播组中成员的数量都可以是任意的,甚至可以为零。那些没有保留下来供永久组播组使用的ip组播地址,可以被临时组播组利用。
224.0.0.0~224.0.0.255 为预留的组播地址(永久组地址),地址224.0.0.0保留不做分配,其它地址供路由协议使用;
224.0.1.0~224.0.1.255 是公用组播地址,可以用于Internet;欲使用需申请。
224.0.2.0~238.255.255.255 为用户可用的组播地址(临时组地址),全网范围内有效;
239.0.0.0~239.255.255.255 为本地管理组播地址,仅在特定的本地范围内有效。
可使用ip ad命令查看网卡编号,如:
itcast$ ip ad
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
link/ether 00:0c:29:0a:c4:f4 brd ff:ff:ff:ff:ff:ff
inet6 fe80::20c:29ff:fe0a:c4f4/64 scope link
valid_lft forever preferred_lft forever
if_nametoindex 命令可以根据网卡名,获取网卡序号。
server-linux
#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 6666
#define CLIENT_PORT 9000
#define MAXLINE 1500
#define GROUP "239.0.0.2"
int main(void)
{
int sockfd, i ;
struct sockaddr_in serveraddr, clientaddr;
char buf[MAXLINE] = "itcast\n";
char ipstr[INET_ADDRSTRLEN]; /* 16 Bytes */
socklen_t clientlen;
ssize_t len;
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 */
inet_pton(AF_INET, GROUP, &clientaddr.sin_addr.s_addr);
clientaddr.sin_port = htons(CLIENT_PORT);
while (1) {
//fgets(buf, sizeof(buf), stdin);
sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr));
sleep(1);
}
close(sockfd);
return 0;
}
client-linux
#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 6666
#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;
confd = socket(AF_INET, SOCK_DGRAM, 0);
//初始化本地端地址
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;
}
server-windows
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
int iRet = 0;
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in addr;
addr.sin_family = AF_INET;
//addr.sin_addr.S_un.S_addr = INADDR_ANY;
addr.sin_addr.S_un.S_addr = inet_addr("192.168.0.104");
addr.sin_port = htons(9411);
bool bOptval = true;
iRet = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&bOptval, sizeof(bOptval));
if(iRet != 0){
printf("setsockopt fail:%d", WSAGetLastError());
return -1;
}
iRet = bind(sock, (sockaddr*)&addr, sizeof(addr));
if(iRet != 0){
printf("bind fail:%d", WSAGetLastError());
return -1;
}
printf("socket:%d bind success\n", sock);
// 加入组播
ip_mreq multiCast;
//multiCast.imr_interface.S_un.S_addr = INADDR_ANY;
multiCast.imr_interface.S_un.S_addr = inet_addr("192.168.0.104");
multiCast.imr_multiaddr.S_un.S_addr = inet_addr("234.2.2.2");
iRet = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast));
if(iRet != 0){
printf("setsockopt fail:%d", WSAGetLastError());
return -1;
}
printf("udp group start\n");
int len = sizeof(sockaddr);
char strRecv[1024] = {0};
while(true)
{
memset(strRecv, 0, sizeof(strRecv));
iRet = recvfrom(sock, strRecv, sizeof(strRecv) - 1, 0, (sockaddr*)&addr, &len);
if(iRet <= 0){
printf("recvfrom fail:%d", WSAGetLastError());
return -1;
}
printf("recv data:%s\n", strRecv);
}
closesocket(sock);
WSACleanup();
return 0;
}
client-windows
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
int iRet = 0;
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(AF_INET,SOCK_DGRAM,0);
int iFlag = 0; // 0-同一台主机 1-夸主机
iRet = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&iFlag, sizeof(iFlag));
if(iRet != 0){
printf("setsockopt fail:%d", WSAGetLastError());
return -1;
}
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = inet_addr("234.2.2.2");
addr.sin_family = AF_INET;
addr.sin_port = htons(9411);
char strSend[1024] = {0};
static int iIdx = 0;
while(1)
{
sprintf_s(strSend, "udp send group data:%d", iIdx++);
iRet = sendto(sock, strSend, strlen(strSend) + 1, 0, (sockaddr*)&addr, sizeof(sockaddr));
if(iRet <= 0){
printf("send fail:%d", WSAGetLastError());
}else{
printf("send data:%s\n", strSend);
}
Sleep(500);
}
closesocket(sock);
WSACleanup();
return 0;
}