2011-04-12 5 views
4

나는 ActiveSync 연결을 통해 Windows CE 장치와 통신하기 위해 TCP 클라이언트/서버 응용 프로그램을 가지고있다. 클라이언트와 서버 모두 비동기 소켓 (즉, Socket.Begin*Socket.End* 기능)을 사용합니다. 클라이언트와 서버는 예상대로 정확히 내 바탕 화면의 모든 기능에서 실행되지만 클라이언트가 ActiveSync를 통해 연결된 Windows CE 장치에서 실행되는 경우 장치가 개시 될 때, 난 항상 (Socket.Shutdown를 호출 한 후 ReceiveCallback에 SocketException을 얻을 때 연결 해제). 전체 예외는 다음과 같습니다..NETCF 비동기 TCP 소켓 정상 종료 문제

System.Net.Sockets.SocketException: An error message cannot be displayed because an optional resource assembly containing it cannot be found 
at System.Net.Sockets.Socket.ReceiveNoCheck() 
at ReceiveAsyncRequest.doRequest() 
at AsyncRequest.handleRequest() 
at WorkerThread.doWork() 
at WorkerThread.doWorkI() 
at WorkItem.doWork() 
at System.Threading.Timer.ring() 

서버 (데스크톱에서 실행 중)가 연결을 끊으면 모든 것이 올바르게 작동하는 것 같습니다. 이 문제를 피하는 방법에 대한 몇 가지 아이디어가 있습니다. 여기에는 장치 시작 연결 해제를 허용하지 않고 연결 해제를 시작한 후 모든 예외를 무시하는 방법이 포함됩니다. 그러나, 왜 이런 일이 일어나고 있는지, 그리고 더 나은 처리 방법이 있는지 알고 싶습니다.

끊기 및 ReceiveCallbacks (서버와 클라이언트 모두에서 운영 체제는 동일)은 다음과 같습니다

실제 예외가 어쩌면이 필요로하는 라이브러리 파악 및 배포하려고 얻기 위하여
public bool Disconnect(StateObject state) 
{ 
    try{ 
    if(state.isDisconnecting) return false; 

    state.isDisconnecting = true; 
    state.sock.Shutdown(SocketShutdown.Both); 
    //Wait for sending and receiving to complete, but don't wait more than 10 seconds 
    for(int i=0; i<100 && (state.isSending || state.isReceiving); i++){ 
     System.Threading.Thread.Sleep(100); 
    } 

    /* Log the disconnect */ 

    state.sock.Close(); 

    return true; 
    } catch (Exception e){ 
    /* Log the exception */ 

    return false; 
    } 
} 

private void ReceiveCallback(IAsyncResult iar) 
{ 
    StateObject state = (StateObject)iar.AsyncState; 
    try{ 
    //handle the new bytes in the buffer. 
    int recv = state.sock.EndReceive(iar); 

    //Handle the buffer, storing it somewhere and verifying complete messages. 

    if(recv > 0){ 
     //start listening for the next message 
     state.sock.BeginReceive(state.recvBuffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveCallback), state); 
    } else { 
     state.isReceiving = false; 
     Disconnect(state); 
    } 
    } catch (Exception e){ 
    /* Log the exception */ 
    } 
} 

//For reference, A StateObject looks kinda like this: 
public class StateObject 
{ 
    public const int BUFFER_SIZE = 1024; 

    public Socket sock = null; 
    public bool isSending = false; 
    public bool isReceiving = false; 
    public bool isDisconnecting = false; 

    public byte[] recvBuffer = new byte[BUFFER_SIZE]; 
    public byte[] sendBuffer = new byte[BUFFER_SIZE]; 

    //Other stuff that helps handle the buffers and ensure complete messages, 
    // even when TCP breaks up packets. 
} 
+2

CF 및 전체 프레임 워크가 동일하게 작동한다고 가정하지 마십시오. http://blog.opennetcf.com/ctacke/2008/10/02/BehavioralDifferencesBetweenTheCFAndFFx.aspx – ctacke

+0

@ctacke, 그래, 난 정말 ... 그것이 고통만큼, 그것을 알아 내기 위해 시작 했어 – chezy525

+0

는 당신이 얻을 않는 디버깅하는 동안 예외가 발생 했습니까? (WinCE 용 원격 디버거가 있습니까?) 또한 자세한 내용이있는 InnerException이 있습니까? – user1859022

답변

0

그것? 메시지가 장치에 설치되어 있지 않기 때문에 메시지 connot이 장치에 표시하는 경우

0

. netcf.messages.cab을 설치해야합니다. 당신은 여기에서 그것을 발견 할 것이다 :

C:\Program Files (x86)\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE\Diagnostics 

이 CAB 파일을 설치 한 후, 다시 응용 프로그램을 실행하고 당신이 얻을 새로운 오류를 게시 할 수 있습니다.