0

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

man_or_astroman
  • 648
  • 1
  • 17
  • 39
  • Why do you need a timer at all? I think that GCDAsyncSocket has completion functions which are called when data has been read. From there you can start the next read operation. – Martin R Apr 05 '13 at 16:19
  • How can i do this? Ive tryed to call only this [asyncSocket readDataWithTimeout:-1 tag:0]; when i connect but its not calling this one - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag when i get data, its only working with the interval – man_or_astroman Apr 05 '13 at 16:34
  • I haven't work actively with GCDAsyncSocket, but I have some questions: As I understand it, readDataWithTimeout is for TCP sockets. For UDP sockets, there is receiveOnce or beginReceiving (in GCDAsyncUdpSocket.h). You also have to set the delegate, otherwise the delegate function will not be called. – Martin R Apr 05 '13 at 16:53

1 Answers1

0

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
aronspring
  • 276
  • 2
  • 11