请教组播问题!!!!!

wiresharker 2011-09-17 05:30:58
#include "stdafx.h"
#pragma comment(lib, "ws2_32.lib")

#include <WinSock2.h>
#include <WS2tcpip.h>
#include <windows.h>

#define GROUP_IP "239.255.255.254"
#define GROUP_PORT 1900

WORD wVersionRequested;
WSADATA wsaData;

void main()
{
WSAStartup(MAKEWORD(2,2),(LPWSADATA)&wsaData);

SOCKET s = ::socket(AF_INET, SOCK_DGRAM, 0);

// 允许其它进程使用绑定的地址
BOOL bReuse = TRUE;
int s1 =::setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&bReuse, sizeof(BOOL));
printf("s1 = %d\n", s1 );

// 绑定到端口
sockaddr_in si;
memset(&si, 0, sizeof(si));
si.sin_family = AF_INET;
si.sin_port = ::htons(GROUP_PORT);
//si.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
si.sin_addr.s_addr = htonl(INADDR_ANY);

s1 = ::bind(s, (struct sockaddr*)&si, sizeof(si));
printf("s1 = %d\n", s1 );

// 加入多播组
ip_mreq mcast;
//mcast.imr_interface.S_un.S_addr = htonl(INADDR_ANY);
//mcast.imr_multiaddr.S_un.S_addr = ::inet_addr(GROUP_IP);
mcast.imr_interface.s_addr = htonl(INADDR_ANY);
mcast.imr_multiaddr.s_addr = ::inet_addr(GROUP_IP);
s1 = ::setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mcast, sizeof(mcast));
printf("s1 = %d\n", s1);

// 发送多播数据
sockaddr_in to;
memset(&to, 0, sizeof(to));
to.sin_family = AF_INET;
to.sin_port = htons(GROUP_PORT);
//to.sin_addr.S_un.S_addr = ::inet_addr(GROUP_IP);
to.sin_addr.s_addr = ::inet_addr(GROUP_IP);

for(int i=0; i<3; i++ )
{
getchar();
int slen;
slen=sendto(s, "hello", 5, 0, (struct sockaddr*)&to, sizeof(to));
printf("slen=%d, to %s\n", slen, inet_ntoa(to.sin_addr));


char buf[1280];
int len;
sockaddr_in other;
int nRet = ::recvfrom(s, buf, sizeof(buf), 0, (sockaddr*)&other, &len);
printf("nRet = %d\n", nRet);
if(nRet != SOCKET_ERROR)
{
buf[nRet] = '\0';
printf("recv: %s from %s\n", buf, inet_ntoa(si.sin_addr));
}
}



::setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char*)&mcast, sizeof(mcast));
::closesocket(s);
printf("exit success!\n");
getchar();
}
上面的windows程序是向一个组发送数据包,但接收端始终收不到,同样的代码移植到linux上去发送,接收端可以收到。
这个代码哪里有问题啊。。
...全文
177 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Julien_Lion 2012-05-18
  • 打赏
  • 举报
回复
请问楼主是从哪里下载的组播发送与接收代码?请不吝指导一下,谢谢!
ps:代码是基于mfc界面的吗?
wiresharker 2011-09-21
  • 打赏
  • 举报
回复
谢谢楼上的回复。
今天下载了个组播发送与接收的代码,发现在同一个系统(即同一个pc)里运行没有问题。
但分别运行在两个pc时就收不到了。google了下好像是说需要交换机支持组播才行。
难道实现组播技术需要在每个交换机上做组播配置吗。。
5t4rk 2011-09-19
  • 打赏
  • 举报
回复
//这个是C#写的多播 没有任何问题 通过测试正确

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace UDP同步组播
{
public partial class Form1 : Form
{
EndPoint GroupEP;
IPAddress LocalIP, GroupIP;
Socket LocalSocket;
byte[] ReceiveBuffer = new byte[1024];
/// <summary>
/// 绑定事件
/// </summary>
public Form1()
{
InitializeComponent();
//定义本地接口
IPAddress[] IPS = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress myhost_ip in IPS)
{
if (myhost_ip.AddressFamily == AddressFamily.InterNetwork)
{
LocalIP = myhost_ip;
break;
}
}
//创建本地套接字并绑定本地接口
LocalSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
LocalSocket.Bind((EndPoint)new IPEndPoint(LocalIP,5033));
//指定远端接口
GroupIP = IPAddress.Parse("224.168.101.2");
//制定组播地址接口
GroupEP = (EndPoint)new IPEndPoint(GroupIP,5033);
}

#region 发送事件
private void SendBtn_Click(object sender, EventArgs e)
{
//配置组播选项
MulticastOption MO = new MulticastOption(GroupIP, LocalIP);

//预定组播地址
LocalSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, MO);
LocalSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface,
(int)IPAddress.HostToNetworkOrder(0));
//发送数据
LocalSocket.SendTo(Encoding.UTF8.GetBytes(ContentBox.Text), GroupEP);
//退出组播地址
LocalSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, MO);

//信息显示
MsgList.Items.Add(string.Format("发送:{0}({1})",ContentBox.Text,
DateTime.Now.ToString("yy-MM-dd h:m:s")));
}
#endregion

#region 接收事件
private void ReceiveBtn_Click(object sender, EventArgs e)
{
//配置组播选项
MulticastOption MO = new MulticastOption(GroupIP, LocalIP);

//预定组播地址
LocalSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, MO);
LocalSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface,
(int)IPAddress.NetworkToHostOrder(0));

//接收数据
IPEndPoint remoteIEP = new IPEndPoint(IPAddress.Any, 0);
EndPoint remoteEP = (EndPoint)remoteIEP;
int rf = LocalSocket.ReceiveFrom(ReceiveBuffer, ref remoteEP);

//退出组播地址
LocalSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, MO);

//信息显示
MsgList.Items.Add(string.Format("接受:{0}({1})",Encoding.UTF8.GetString(ReceiveBuffer,0,rf).Trim(),
DateTime.Now.ToString("yyyy-MM-dd h:m:s")));
}
#endregion

private void Form1_Load(object sender, EventArgs e)
{

}

}
}


楼主还是单步跟踪吧
5t4rk 2011-09-19
  • 打赏
  • 举报
回复
linux下可以 windows下不可以
wiresharker 2011-09-19
  • 打赏
  • 举报
回复
我也顶下。。。。。
无趣 2011-09-17
  • 打赏
  • 举报
回复
有难度,帮你顶下。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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