나는 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.
}
CF 및 전체 프레임 워크가 동일하게 작동한다고 가정하지 마십시오. http://blog.opennetcf.com/ctacke/2008/10/02/BehavioralDifferencesBetweenTheCFAndFFx.aspx – ctacke
@ctacke, 그래, 난 정말 ... 그것이 고통만큼, 그것을 알아 내기 위해 시작 했어 – chezy525
는 당신이 얻을 않는 디버깅하는 동안 예외가 발생 했습니까? (WinCE 용 원격 디버거가 있습니까?) 또한 자세한 내용이있는 InnerException이 있습니까? – user1859022