iOS 应用只能发送 125 条 UDP 消息?

标签 ios networking udp

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

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

这是发送消息的 View 的 .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 几乎总是足够的。

关于iOS 应用只能发送 125 条 UDP 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548572/

相关文章:

ios - 我怎样才能普遍禁用横向?

.net - 证书创建失败

networking - 从另一台计算机连接到 Windows localhost (IIS)

c - 通过网络发送不同大小数据的最佳实践

mobile - 通过移动网络进行实时多人游戏?

node.js - 连接到 torrent 跟踪器/对等点

ios - 图像元数据中的经度始终为零

ios - 当不可见单元格时获取单元格 Collection View 的值

java - 连接到广播 IP 地址

iphone - 在 iPhone 上检索当前本地时间?