简体   繁体   中英

ios Read data at fast rate in UDP

i am establishing a UDP connection using GCDAsyncSocket(ios device). Everything working fine and im able to send and receive messages, my problem is that i want to exchange data fast. I can send pretty fast data from my iphone to a pc but i cant get at that speed data from pc, more specific i want to be able to get data every 100ms. I use this function when i connect successfully:

-(void)startRead {
   [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startRead) userInfo:nil repeats:YES];
   [asyncSocket readDataWithTimeout:-1 tag:0];
}

With this i can read data with 1sec interval but if i try to put 0.1 seconds my program freezes.(Same with values under 1second) Im sure that im doing something wrong here and there will be a way to achieve what i want so if anybody know plz help!! thanx

I believe the above comment is correct, you've not set the Delegate correctly on init. The socket creation should be something like this

GCDAsyncUdpSocket* udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSError *error = nil;

    if (![udpSocket bindToPort:0 error:&error])
    {
        [self logError:[NSString stringWithFormat:@"Error binding: %@", error]];
        return;
    }
    if (![udpSocket beginReceiving:&error])
    {
        [self logError:[NSString stringWithFormat:@"Error receiving: %@", error]];
        return;
    }

    NSString *_host = nil;
    uint16_t _port = 0;
    [GCDAsyncUdpSocket getHost:&_host port:&_port fromAddress:udpSocket.localAddress];
    [self logInfo:[NSString stringWithFormat:@"Socket setup on host %@:%d", _host, _port]];

    [self logInfo:@"Socket created successfully."];

Unless you're using a different version of GCDAsyncUdpSocket than I'm familiar with, the correct callback method is actually the below method. This is called automatically when the delegate is set and a packet is received on the correct port.

- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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