iOS 应用程序只能发送 125 个 UDP 消息?

iOS app only able to send 125 UDP messages?

我目前正在使用 GCDAsyncUdpSocket 库并成功将 UDP 消息发送到服务器,但是当发送了 125 条消息后,应用程序停止发送消息但没有崩溃或抛出任何错误。

这是一个问题,因为我不断发送消息来控制机器人的运动。

这是发送消息的视图的 .m 文件。

非常感谢任何帮助。

#import "MovementVC.h"
#import "GCDAsyncUdpSocket.h"

@interface MovementVC ()

@end

@implementation MovementVC



- (void)viewDidLoad {
     [super viewDidLoad];
    f, r, l, b = false;
    timer = [NSTimer scheduledTimerWithTimeInterval: 0.1 target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)RightTouchDown:(id)sender {
    r = true;
}

- (IBAction)RightTouchUpIn:(id)sender {
    r = false;
}

- (IBAction)RightTouchUpOut:(id)sender {
    r = false;
}


- (IBAction)ForwardTouchDown:(id)sender {
    f = true;
}

- (IBAction)ForwardTouchUpIn:(id)sender {
    f = false;
}

- (IBAction)ForwardTouchUpOut:(id)sender {
    f = false;
}


- (IBAction)LeftTouchDown:(id)sender {
    l = true;
}

- (IBAction)LeftTouchUpIn:(id)sender {
    l = false;
}

- (IBAction)LeftTouchUpOut:(id)sender {
    l = false;
}

- (IBAction)ReverseTouchDown:(id)sender {
    b = true;
}

- (IBAction)ReverseTouchUpIn:(id)sender {
    b = false;
}

- (IBAction)ReverseTouchUpOut:(id)sender {
    b = false;
}


-(void) sendUDPMessage: (NSString*)messageToSend
{
    GCDAsyncUdpSocket *udpSocket ; // create this first part as a global variable
    udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSData *data = [messageToSend dataUsingEncoding:NSUTF8StringEncoding];

    [udpSocket sendData:data toHost:@"172.31.0.1" port:43211 withTimeout:-1 tag:1];
}


-(void) targetMethod:(id)sender
{
    if (r == TRUE)
    {
        [self sendUDPMessage: @"1"];
    }
    else if (l == TRUE)
    {
        [self sendUDPMessage: @"11"];
    }
    else if(f == TRUE)
    {
        [self sendUDPMessage: @"111"];
    }
    else if (b == TRUE)
    {
        [self sendUDPMessage: @"1111"];
    }
}

@end

您需要保留一个指向您的 UDP 套接字的指针,而不是持续创建新的套接字,1 个应该几乎总是足够的。