2011-04-06 66 views
2

我正在使用asyncsocket創建與目標C的套接字連接。我使用「connectToHost」方法執行此操作。我正在嘗試處理套接字連接失敗的情況。 「connectToHost」在連接成功時應返回「YES」,否則返回NO。出於某種原因,它總是返回一個yes。我甚至提供了一個空白字符串作爲主機,它仍然返回yes。有什麼想法嗎?asyncsocket「connectToHost」總是成功並且永遠不會返回失敗

感謝,

羅賓

BOOL connectStatus = NO; //used to check if connection attempt succeeded 
testSocket = [[AsyncSocket alloc] initWithDelegate: self]; 
connectStatus = [testSocket connectToHost: @"" onPort: 5000 error: nil]; 

if(connectStatus == NO) 
{ 
    NSLog(@"Failed to connect to socket "); 

} 

else { 
    NSLog(@"Connected to socket sucessfully, connectStatus = %d", connectStatus); 
} 

回答

6

header file

// Once one of the accept or connect methods are called, the AsyncSocket instance is locked in 
// and the other accept/connect methods can't be called without disconnecting the socket first. 
// If the attempt fails or times out, these methods either return NO or 
// call "onSocket:willDisconnectWithError:" and "onSockedDidDisconnect:". 

如果檢查the source - 爲聖萬物的愛,與開源軟件工作時,使用源! - ,只有在啓動連接過程失敗時,纔會看到您調用的方法返回NO。的YES返回值只是說,「好吧,我試圖連接:

  • 」如果出了問題,我會讓你知道通過調用onSocket:willDisconnectWithError:onSocketDidDisconnect:。 「
  • 」如果一切順利,您會收到onSocket:didConnectToHost:port:消息,mmkay?「

不要期待異步套接字庫的同步行爲。

+0

需要點。如果它讓你感覺更好,我確實看過了源頭,但還不夠清楚。謝謝 :) – Robin 2011-04-07 06:31:23

相關問題