组播问题,200分……急切

OpenHero 2008-07-16 01:16:15
组播接收的时候,不同的ip组 相同的port
例如:
224.1.1.3 12345
224.1.1.2 12345
接受的时候,会出现什么问题?
遇到这个问题,怎么解决?
...全文
486 46 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
46 条回复
切换为时间正序
请发表友善的回复…
发表回复
otisyf 2008-07-23
  • 打赏
  • 举报
回复
请教:
组播可以通过IP_MULTICAST_LOOP禁止本地的回环,对于广播怎样禁止回环呢?
fierygnu 2008-07-22
  • 打赏
  • 举报
回复
[Quote=引用 40 楼 OpenHero 的回复:]
引用 39 楼 fierygnu 的回复:
是什么问题?如何解决的?

我的blog上已经写了,client端也需要加上multicast 路由,还得打开multicast支持,不然就是走的broadcast,代码没有错。。。。
[/Quote]

奇怪,昨天试了一下,client不加multicast路由是可以的。。。

fuqd273 2008-07-22
  • 打赏
  • 举报
回复
mark
jufeng2309 2008-07-22
  • 打赏
  • 举报
回复
学习。。。
OpenHero 2008-07-22
  • 打赏
  • 举报
回复
[Quote=引用 44 楼 fierygnu 的回复:]
引用 40 楼 OpenHero 的回复:
引用 39 楼 fierygnu 的回复:
是什么问题?如何解决的?

我的blog上已经写了,client端也需要加上multicast 路由,还得打开multicast支持,不然就是走的broadcast,代码没有错。。。。


奇怪,昨天试了一下,client不加multicast路由是可以的。。。


[/Quote]
……不会吧~~奇怪了,那就。。。。
linuxguy 2008-07-21
  • 打赏
  • 举报
回复
收藏了!
OpenHero 2008-07-21
  • 打赏
  • 举报
回复
[Quote=引用 39 楼 fierygnu 的回复:]
是什么问题?如何解决的?
[/Quote]
我的blog上已经写了,client端也需要加上multicast 路由,还得打开multicast支持,不然就是走的broadcast,代码没有错。。。。
OpenHero 2008-07-19
  • 打赏
  • 举报
回复
问题解决了:
http://blog.csdn.net/OpenHero/archive/2008/07/19/2675605.aspx
fierygnu 2008-07-19
  • 打赏
  • 举报
回复
是什么问题?如何解决的?
OpenHero 2008-07-17
  • 打赏
  • 举报
回复
本机的路由需要加什么吗?
OpenHero 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 33 楼 fierygnu 的回复:]
改成了什么样?把代码整理一下贴上来吧,看着头晕。
[/Quote]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#include <winsock2.h>
#include <io.h>
#include <Ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")

#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#define closesocket close
#endif

#define BUF_SIZE 0x1000

char * host_name = "224.1.1.3";
int port = 12345;

int main(int argc, char* argv[])
{
int loop = 1;
/* 多播循环 */
int iter = 0;
int from_len;
char buffer[BUF_SIZE];
int socket_descriptor;
struct ip_mreq command;
struct sockaddr_in loc;
struct sockaddr_in rem;

int rec_len =0;
int ptr_point = 0;
size_t bytes_written = 0;
int loop_times = 0;

int rcv_port_0 = 0;
int rcv_port_1 = 0;
int first = 0;

#ifdef _WIN32
WSADATA wsaData;

if( WSAStartup(MAKEWORD(2,2), &wsaData) != 0 )
{
printf("Error in WSAStartup\n");
exit(1);
}
#endif

if (argv[1])
{
host_name = argv[1];
printf("%s \n", host_name);
}
if (argv[2])
{
port = atoi(argv[2]);
printf("%d \n", port);
}

memset(&loc, 0, sizeof(loc));
loc.sin_family = AF_INET;
loc.sin_addr.s_addr = inet_addr(host_name);
loc.sin_port = htons(port);

if((socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}

/*can use the same port in the pc*/
loop = 1;
if(setsockopt(socket_descriptor, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0)
{
perror("setsockopt:SO_REUSEADDR.\n");
exit(EXIT_FAILURE);
}else
{
printf("setsockopt:SO_REUSEADDR.\n");
}
loop = 0x1000000;
if( setsockopt( socket_descriptor, SOL_SOCKET, SO_RCVBUF, (void *) &loop, sizeof( loop ) ) < 0 )
{
perror("setsockopt:SO_RCVBUF.\n");
exit(EXIT_FAILURE);
}

#if defined( WIN32 ) || defined( UNDER_CE )
/*
* Under Win32 and for multicasting, we bind to INADDR_ANY.
* This is of course a severe bug, since the socket would logically
* receive unicast traffic, and multicast traffic of groups subscribed
* to via other sockets. How this actually works in Winsock, I don't
* know.
*/
if( IN_MULTICAST( ntohl( loc.sin_addr.s_addr ) ) )
{
struct sockaddr_in stupid = loc;
stupid.sin_addr.s_addr = INADDR_ANY;

if( bind( socket_descriptor, (struct sockaddr *)&stupid, sizeof( stupid ) ) < 0 )
{
perror("bind socket.\n");
close( socket_descriptor );
return 0;
}
}
else
#endif
if(bind(socket_descriptor, (struct sockaddr *)&loc, sizeof(loc)) < 0)
{
perror("bind socket.\n");
exit(EXIT_FAILURE);
}



{
printf("%s,%d\n", host_name, port);
command.imr_multiaddr.s_addr = inet_addr(host_name);
command.imr_interface.s_addr = htonl(INADDR_ANY);
if(command.imr_multiaddr.s_addr == -1)
{
perror("224.1.1.3 not a legal multicast address.\n");
exit(EXIT_FAILURE);
}
if (setsockopt(socket_descriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0)
{
perror("setsockopt:IP_ADD_MEMBERSHIP. error\n");
}
}


printf("start reciving...\n");


from_len = sizeof(rem);
while(1)
{
memset(&rem, 0, from_len);
memset(buffer, 0, BUF_SIZE);
if( (rec_len = recvfrom(socket_descriptor, buffer, sizeof(char) * BUF_SIZE, 0, (struct sockaddr *)&rem, &from_len)) == -1) {
perror("recvfrom.\n");
}

printf( "From host:%s port:%d, len: %d\n", inet_ntoa(rem.sin_addr), ntohs(rem.sin_port), rec_len);
}
if(setsockopt(socket_descriptor, IPPROTO_IP, IP_DROP_MEMBERSHIP, &command, sizeof(command)) < 0) {
perror("setsockopt:IP_DROP_MEMBERSHIP.\n");
}
closesocket(socket_descriptor);
exit(EXIT_SUCCESS);
}
fierygnu 2008-07-17
  • 打赏
  • 举报
回复
改成了什么样?把代码整理一下贴上来吧,看着头晕。
playmud 2008-07-17
  • 打赏
  • 举报
回复
交换机上的还是bub上的?是不是这个问题呢?
OpenHero 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 fierygnu 的回复:]
loc.sin_addr.s_addr = htonl(INADDR_ANY);
//此处应绑定多播地址,否则,虽然你加入了多播组,但所有到达该端口的报文都会被接收。
Windows的协议栈实现可能不同。

[/Quote]

修改了,还是不行……
fierygnu 2008-07-17
  • 打赏
  • 举报
回复
loc.sin_addr.s_addr = htonl(INADDR_ANY);
//此处应绑定多播地址,否则,虽然你加入了多播组,但所有到达该端口的报文都会被接收。
Windows的协议栈实现可能不同。
威廉-丁 2008-07-17
  • 打赏
  • 举报
回复
帮顶
OpenHero 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 fierygnu 的回复:]
只是收,不需要路由。

问题很奇怪,有时间试试看。
[/Quote]
555555 现在有时间吗……,这个很奇怪的问题
fierygnu 2008-07-17
  • 打赏
  • 举报
回复
只是收,不需要路由。

问题很奇怪,有时间试试看。
OpenHero 2008-07-16
  • 打赏
  • 举报
回复
网络高手,帮忙解决~~~
linux下的组播~
OpenHero 2008-07-16
  • 打赏
  • 举报
回复
现在的情况:
在client端,同时起两个client 分别等待224.1.1.3 12345 和224.1.1.2 12345
在windows中,可以分别接受到这两个组的数据;
在linux中,两个client混合接收到这两个组的数据;

当停掉一个server;
windows中,负责监听相应的client就会停止;
linux中,两个继续运行……crazy!!! 都接受另一个server的数据;

为什么会这样?
加载更多回复(26)

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧