2011-04-06 1 views
2

asyncsocket을 사용하여 목표 C로 소켓 연결을 만듭니다. "connectToHost"메서드를 사용하여이 작업을 수행합니다. 소켓 연결이 실패한 경우를 처리하려고합니다. "connectToHost"는 성공적으로 연결되면 "예"를 반환하고 그렇지 않으면 "아니요"를 반환합니다. 웬일인지, 그것은 항상 긍정을 돌려주고있다. 심지어 빈 문자열을 호스트로 제공하고 여전히 예를 반환합니다. 이견있는 사람?asyncsocket "connectToHost"는 항상 성공하고 실패를 반환하지 않습니다.

감사합니다,

로빈 header file

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

:

// 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: 메시지가 표시됩니다."

비동기 소켓 라이브러리의 동기 동작을 기대하지 마십시오.

+0

점을 찍었습니다. 기분이 나아진다면 소스를 살펴 보았지만 분명히 충분하지는 않았습니다. 감사 :) – Robin