2011-04-20 64 views
2

我想通過使用AsyncUdpSocket library通過UDP發送一些小數據。有很多關於TCP連接的文檔,但沒有關於UDP連接的文檔。如何使用AsyncUdpSocket在Objective C中發送UDP數據?

我寫了這個類發送5個字節到遠程主機,但它看起來沒有什麼是實際去電線。我正在使用wireshark監視網絡,但我看不到傳出數據包。委託方法「didSendDataWithTag」從未被稱爲:(

任何想法我忘了嗎?

#import "UDPController.h" 

@implementation UDPController 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     socket = [[AsyncUdpSocket alloc] initWithDelegate:self]; 
    } 
    return self; 
} 


- (void) sendUDPTest { 
    NSLog(@"%s", __PRETTY_FUNCTION__); 

    NSString * string = @"R/103"; 
    NSString * address = @"192.168.1.130"; 
    UInt16 port = 21001; 
    NSData * data = [string dataUsingEncoding:NSUTF8StringEncoding]; 

    [socket sendData:data toHost:address port:port withTimeout:-1 tag:1];   
} 


/** 
* Called when the datagram with the given tag has been sent. 
**/ 
- (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag { 
     NSLog(@"%s", __PRETTY_FUNCTION__); 
} 


- (void)dealloc 
{ 
    [socket release]; 
    [super dealloc]; 
} 

@end 

歡呼

回答

1

這裏的問題是,這個代碼是不是在「應用程序循環」運行,主要是

+1

嘿,哥們,我有個退出AsyncUdpSocket來得及發送數據包之前,如果它放在一個應用

相同的代碼工作正常。同樣的問題,我只是不明白你的解釋。說「放入應用程序」是什麼意思? – 2012-01-10 14:14:06