0

我正在尝试创建一个 UDP 套接字并将数据发送到广播端口,以便我可以在同一 WiFi 网络中的其他设备上接收相同的数据。我正在计算此处接受的答案中给出的广播端口的 IP 地址。

之后,我编写了一些连接代码,如下所示:

self.udpSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue() socketQueue:nil];
NSError *error;
[self.udpSocket enableReusePort:YES error:&error];
[self.udpSocket enableBroadcast:YES error:&error];

- (IBAction)sendMessageToBroadcastPort:(id)sender {
 [self.udpSocket sendData:[@"Hi" dataUsingEncoding:NSUTF8StringEncoding] toHost:[self getUDPBroadcastAddress] port:5556 withTimeout:-1 tag:1];
}

我成功发送数据作为委托方法 didSendData: 被调用。

请指导我在这里缺少什么。

谢谢!

更新: 用于从广播端口接收数据的 Cpde:

- (void)listenForPackets
{
dispatch_queue_t dQueue = dispatch_queue_create("client udp socket", NULL);
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dQueue socketQueue:nil];
NSError *error = nil;

if (![udpSocket bindToPort:5556 error:&error]) {
    NSLog(@"Error binding: %@",error);//not connecting to host
    return;
}
if (![udpSocket beginReceiving:&error]) {
    NSLog(@"Error receiving: %@",error);
    return;
}
NSLog(@"Socket Ready");
}

 - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
  fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (msg)
{
    NSLog(@"RCV: %@", msg);
}
else
{
    NSString *host = nil;
    uint16_t port = 0;
    [GCDAsyncUdpSocket getHost:&host port:&port fromAddress:address];
    NSLog(@"Unknown message from : %@:%hu", host, port);
}
}

我计算后得到的广播IP是192.168.2.255

更新2::

我面临的情况真的很不同而且很奇怪。接收有时有效,有时无效。当我安装两个应用程序时,没有收到任何数据。只有发送成功。我保持应用程序打开,一段时间后应用程序开始接收数据。在某些情况下,它会在一段时间后停止接收或继续接收而没有任何问题。可能是什么问题?

4

2 回答 2

1

尝试如下:

创建一个套接字:

-(void)createClientUdpSocket
{
    dispatch_queue_t dQueue = dispatch_queue_create("client udp socket", NULL);

    sendUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue: dQueue socketQueue: nil];
    [sendUdpSocket receiveOnce:nil];
    [sendUdpSocket joinMulticastGroup:[self getIPAddress] error:nil]; // Put your IP Address
    NSLog(@"Ready");
}

Socket 的委托方法。

-(void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext
{
     NSString *ip = [GCDAsyncUdpSocket hostFromAddress:address];
     uint16_t port = [GCDAsyncUdpSocket portFromAddress:address];
     NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
     // Continue to wait for a message to receive the next
     NSLog (@ "The server receives the response [%@:% d]%@", ip, port, s);
     [sock receiveOnce:nil];

     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     [self sendBackToHost:ip port:port withMessage:s];
   });
}

-(void)sendBackToHost:(NSString *)ip port:(uint16_t)port withMessage:(NSString *)s{

      [sendUdpSocket sendData:yourData toHost:yourIP port:5556 withTimeout:60 tag:200];
       NSLog(@"sent message to server");
}

希望这会有所帮助。它为我工作:)

于 2016-06-01T05:16:21.240 回答
0

我正在回答这个问题,因为我面临的情况真的很不同而且很奇怪。接收有时有效,有时无效。当我安装两个应用程序时,没有收到任何数据。只有发送成功。我保持应用程序打开,一段时间后应用程序开始接收数据。在某些情况下,它会在一段时间后停止接收。

实际上,我使用的是连接到以太网的 Mac 的共享 Internet 连接。我将我的 WiFi 网络更改为适当的宽带网络,从那时起它就可以正常工作了。

希望这可以帮助有类似情况的人:)

于 2016-06-08T09:22:13.460 回答