有用GCDAsyncUdpSocketjava 组播 发送接收收组播的么

AsyncUdpSocket如何监听udp网络 - 知乎4被浏览2896分享邀请回答0添加评论分享收藏感谢收起18:09 提问
UDP组播通信,发送数据正常,但是只能接受到本机发出的数据,为什么?
UDP组播通信,发送数据正常,但是只能接受到本机发出的数据,收不到其他机器发出的数据。这是代码:enter code hereSend_ACT_time_interval_set();
int nlen=sizeof(sact);
memcpy(sendbuf,(char*)&sact,sizeof(sendbuf));
int nlenm=sizeof(sendbuf);
int nlenn=sendto(socketMul,sendbuf,nlenm,0,(SOCKADDR*)&addrMul,sizeof(addrMul));
char recvbuf[1024];
if(nlenn&0)
printf("success send sock\n");
//closesocket(socketMul);
//Sleep(1000);
int len=sizeof(addrMul);
int mlen=strlen(recvbuf);
int nret=recvfrom(socketMul,recvbuf,strlen(recvbuf),0,(SOCKADDR*)&addrMul,&len);
if(nlen&0)
printf("success recv sock\n");
closesocket(socketMul);
WSACleanup();
请高手指点一下!!!
按赞数排序
帮顶,我也遇到同样的问题,设置组播路由还是没有解决
sudo ifconfig
eth0 allmuti
sudo ifconfig eth0 allmulti
其他相关推荐8946人阅读
iphone(160)
客户端开发一般是发送数据给服务端。这次反过来了,把客户端作为服务端了,监听对应端口并接收数据.
直接上代码:
.......h文件
@interface ListenServerData : NSObject {
- (id)initWithD
........m文件
#import &ListenServerData.h&
#import &GCDAsyncUdpSocket.h&
@interface ListenServerData () {
@property (strong,
nonatomic) GCDAsyncUdpSocket *gcdUdpS
@implementation ListenServerData
- (id)initWithD
& & if (self = [super init]) {
& & & & self.gcdUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
& & & & NSError *
& & & & [_gcdUdpSocket bindToPort:88898 error:&error];
& & & & if (nil != error) {
& & & & & & NSLog(@&failed.:%@&,[error description]);
& & & & [_gcdUdpSocket enableBroadcast:YES error:&error];
& & & & if (nil != error) {
& & & & & & NSLog(@&failed.:%@&,[error description]);
& & & & //组播224.0.0.2地址,如果地址大于224的话,就要设置GCDAsyncUdpSocket的TTL
(默认TTL为1)
& & & & [_gcdUdpSocket joinMulticastGroup:@&224.0.0.2& error:&error];
& & & & if (nil != error) {
& & & & & & NSLog(@&failed.:%@&,[error description]);
& & & & [_gcdUdpSocket beginReceiving:&error];
& & & & if (nil != error) {
& & & & & & NSLog(@&failed.:%@&,[error description]);
& & return self;
- (void)dealloc
& & if (_gcdUdpSocket) {
& & & & [_gcdUdpSocket close];
#pragma mark -GCDAsyncUdpsocket Delegate
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
&& & NSLog(@&Reciv Data len:%d&,[data length]);
- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error
& & NSLog(@&udpSocketDidClose Error:%@&,[error description]);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1092707次
积分:9513
积分:9513
排名:第1961名
原创:114篇
转载:120篇
评论:97条
(3)(2)(1)(1)(1)(1)(2)(1)(4)(4)(2)(1)(8)(8)(3)(11)(2)(1)(7)(13)(4)(8)(3)(6)(6)(4)(7)(11)(10)(11)(2)(7)(7)(1)(1)(2)(1)(9)(1)(1)(1)(1)(2)(7)(1)(1)(5)(1)(9)(1)(1)(2)(1)(1)(3)(1)(1)(3)(2)(1)(1)(2)(1)(3)(1)(1)(1)(2)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'最近用wcf 服务 给ios和安卓做接口,做了几个ios的项目 &用udp 组播 让ios多终端接收和刷新方法
做一个简单的小例子会把工程给大家下载的
  c#代码:
& & &ios代码:
先用c#做发送
组播IP范围为&224.0.0.0~239.255.255.255
建一个控制台应用程序
private static IPAddress GropuAddress = IPAddress.Parse("224.0.0.2");//IP
private static int GrupPort = 12001;//端口
static void Main(string[] args)
for (int i = 0; i & 1000; i++)
System.Threading.Thread.Sleep(3000);//等待3秒再发
Send("sendMessage"+i.ToString()+"个!");
public static void Send(string message)
        //不在一个网段也可以收到
        Socket server = new Socket(AddressFamily.InterNetwork,
        SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint iep = new IPEndPoint(IPAddress.Any, 12002);
        IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("224.0.0.2"), 12001);
        server.Bind(iep);
        byte[] data = Encoding.ASCII.GetBytes(message);
        server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
        new MulticastOption(IPAddress.Parse("224.100.0.1")));
        server.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive, 50);
        server.SendTo(data, iep2);
        server.Close();
& & & & & & & &}
  顺便把c#接收也说一下(这个可以不用看,因为我们要做的是ios接收)
static void Main(string[] args)
StartListener();
Console.ReadLine();
private static void StartListener()
byte[] b = new byte[10240];
while (true)
System.Threading.Thread.Sleep(500);
String multiAddress = "224.0.0.2";//Datagrams.getMultiIPAddress("192.168.2.106");
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 12001);
s.Bind(ipep);
IPAddress ip = IPAddress.Parse(multiAddress);
s.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(ip, IPAddress.Any));
s.ReceiveTimeout = 5000;
s.Receive(b);
string sss = System.Text.Encoding.UTF8.GetString(b).Replace("\0", "").Trim();
Console.WriteLine(sss);
s.Close();
catch (Exception ex)
Console.WriteLine("receive multicast exception:" + ex.ToString());
ios 用到的一个类库AsyncUdpSocket 这个类库就是发送和接收收 &组播的 用起来很方便网上有好多例子我就简单说一下
建一个Single View Application
把AsyncUdpSocket.h 和AsyncUdpSocket.m加到工程里
窗体上放一个文本显示收到的信息
在ViewController.h里加入
#import "AsyncUdpSocket.h"@interface ViewController : UIViewController&AsyncUdpSocketDelegate&
@property (strong, nonatomic) IBOutlet UITextField *MyResaveT//页面上的文本
@property (nonatomic,strong) AsyncUdpSocket *udpS
-(void)openUDPS
在ViewController.m 里实现
#import "ViewController.h"
@interface ViewController ()
@implementation ViewController
@synthesize udpSocket,MyResaveT
- (void)viewDidLoad
[super viewDidLoad];
[self openUDPServer];
// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
-(void) openUDPServer
//初始化udp
AsyncUdpSocket *tempSocket=[[AsyncUdpSocket alloc] initWithDelegate:self];
self.udpSocket=tempS
//绑定端口
NSError *error =
[self.udpSocket bindToPort:12001 error:&error];
//发送广播设置
[self.udpSocket enableBroadcast:YES error:&error];
//加入群里,能接收到群里其他客户端的消息
[self.udpSocket joinMulticastGroup:@"224.0.0.2" error:&error];
//启动接收线程
[self.udpSocket receiveWithTimeout:-1 tag:0];
-(BOOL) onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
NSString * info=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
MyResaveTxt.text=
[self.udpSocket receiveWithTimeout:-1 tag:0];//启动接收线程
return YES;
  c#代码:
& & &ios代码:
补一下发汉字会有乱码
byte[] data =System.Text.Encoding.Default.GetBytes(message);
NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_);
NSString * info=[[NSString alloc] initWithData:data encoding:strEncode ];
ios &URL中文转码
NSString *url =@"/这是中文";
NSStringEncoding chineseEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_);
url = [url stringByAddingPercentEscapesUsingEncoding:chineseEncoding];
NSLog(@"%@",url);
    NSMutableURLRequest *request = [[NSMutableURLRequestalloc] init];
& & & & & & // 设置URL
& & & & & & [request setURL:[NSURL URLWithString:url]];
& & & & & & // 设置HTTP方法
& & & & & & [request setHTTPMethod:@"GET"];
& & & & & & // 发送同步请求, 这里得returnData就是返回得数据
& & & & & & NSData *data = [NSURLConnectionsendSynchronousRequest:request
&& & & & & & & & & & & & & & & & & & & & & & & & & & & returningResponse:nil error:nil];&
& & & & & &&
- (NSString *)URLEncodedString
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)self,NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8);
[result autorelease];
阅读(...) 评论()

我要回帖

更多关于 java 组播 发送接收 的文章

 

随机推荐