2

I have a little MAC OS X Application that should send a UDP Datagram to a server.

I want to use the cocoa asyncudpsockets (http://code.google.com/p/cocoaasyncsocket/) to do this, but i have the problem that i call the "sendData" method but nothing happens.

[socket sendData:[NSData dataWithBytes:stream length:length] 
          toHost:host 
            port:(uint16)port 
     withTimeout:5 
             tag:1];

stream is a Byte* contains the datagram host is a ip-address as NSString.

Can anybody help me?

JWWalker
  • 22,385
  • 6
  • 55
  • 76
mabstrei
  • 1,200
  • 3
  • 13
  • 28

1 Answers1

1

Did you correctly set the delegate ?

[socket setDelegate:self];

Then :

[socket connectToHost:(NSString *)host
               onPort:(UInt16)port
          withTimeout:(NSTimeInterval)timeout
                error:(NSError **)errPtr];

Then in the delegate method :

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    [sock writeData:[NSData dataWithBytes:stream length:length] withTimeout:TIMEOUT_NONE tag:TAG_HEADER];
}

Hope it will help ;-)

nverinaud
  • 1,270
  • 14
  • 25
  • This sample code does not deal with error and has to be improve for your software release ;) – nverinaud Oct 27 '11 at 14:01
  • I did sockets = [[AsyncUdpSocket alloc] initWithDelegate:self] to set the delegate. Should be quite the same... I didn't connect the socket (cause udp is usually connectionless) and used sendData:... Do i have to connect with the udp sockets too? – mabstrei Oct 27 '11 at 14:19
  • You can try, it mays help. UDP is just a protocol with which you don't care about the information is received successfully. But it is a connection protocol anyway like TCP. – nverinaud Oct 28 '11 at 08:37
  • Thanks i will try that as soon as possilbe – mabstrei Oct 28 '11 at 13:28