我当前正在使用 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/