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: :

私が直面しているシナリオは、まったく異なり、奇妙です。受信がうまくいくときとうまくいかないときがあります。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

私が直面したシナリオは本当に異なり、奇妙であるため、この質問に答えています。受信がうまくいくときとうまくいかないときがあります。2 つのアプリをインストールすると、データが受信されませんでした。送信のみ成功しました。アプリをオンのままにして、しばらくするとアプリがデータを受信し始めました。場合によっては、しばらくすると受信が停止しました。

実際、イーサネットに接続されているMacの共有インターネット接続を使用していました。WiFi ネットワークを適切なブロードバンド ネットワークに変更したところ、問題なく動作しました。

これが同様のシナリオの誰かに役立つことを願っています:)

于 2016-06-08T09:22:13.460 に答える