简体   繁体   English

Obj-C:如何在GCDAsyncUdpSocket中使UDP数据包的源端口一致? 源端口始终更改

[英]Obj-C: How to make UDP packet's Source Port consistent in GCDAsyncUdpSocket? source port alway change

I have using following code to crate UDP packets.I using bindToPort to setup Source Port. 我使用以下代码创建UDP数据包。我使用bindToPort设置源端口。

[asyncUdpSocket bindToPort:7701
                     error:&socketError])  

However , ! 但是 ,! found only first time I go through those codes can get a right source port. 发现只有我第一次通过这些代码时,才能获得正确的源端口。 Rest times, they are all wrong!! 休息时间,他们都是错的! My bind to port should be 7701 . 我绑定到端口应该是7701

Wireshark捕获的图像

 GCDAsyncUdpSocket *asyncUdpSocket;
    asyncUdpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self
                                                   delegateQueue:dispatch_get_main_queue()];
    [asyncUdpSocket setPreferIPv4];
    if ([asyncUdpSocket bindToPort:7701
                             error:&socketError]){
        NSLog(@"Bind to Port fail");
    }
    [asyncUdpSocket enableBroadcast:NO error:&socketError];
    [asyncUdpSocket sendData:data
                      toHost:@"192.168.16.77"
                        port:7701
                 withTimeout:-1
                         tag:0];

Question: 题:

How to make UDP packet's Source Port consistent in GCDAsyncUdpSocket? 如何在GCDAsyncUdpSocket中使UDP数据包的源端口一致?

The reason of variable port number is I am using ARC. 端口号可变的原因是我正在使用ARC。 The instance might not be released at next time alloc and init. 该实例可能不会在下次分配和初始化时释放。

Everytime I call this part code I create a new GCDAsyncUdpSocket object, then try to bind the port and then send the message. 每次我调用此零件代码时,我都会创建一个新的GCDAsyncUdpSocket对象,然后尝试绑定端口,然后发送消息。 These objects are being dealloced only at a later time when I use ARC. 仅在以后使用ARC时才重新分配这些对象。 So, actually I am trying to bind to the port several times which is not possible. 因此,实际上我试图多次绑定到端口,这是不可能的。 Once the port is bound, I cannot bind it any more. 绑定端口后,我将无法再绑定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM