c# - .NETCF 异步 TCP 套接字正常关闭问题

标签 c# compact-framework activesync asyncsocket

我有一个 TCP 客户端/服务器应用程序,可以通过 ActiveSync 连接与 Windows CE 设备进行通信。客户端和服务器都使用异步套接字(即 Socket.Begin*Socket.End* 函数)。当客户端和服务器都在我的桌面上运行时,一切都按预期运行,但是当客户端在通过 ActiveSync 连接的 Windows CE 设备上运行时,我总是在调用后在 ReceiveCallback 上得到一个 SocketException Socket.Shutdown(当设备启动断开连接时)。完整的异常(exception)是:

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()

如果服务器(在桌面上运行)启动断开连接,一切似乎也能正常工作。我有几个关于如何避免这种情况的想法,包括不允许设备启动断开连接和在启动断开连接后忽略所有异常。但是,我想知道为什么会发生这种情况,以及是否有更好的处理方法。

Disconnect 和 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.
}

最佳答案

为了获得实际的异常,可能会尝试弄清楚它需要哪个库并部署它?

关于c# - .NETCF 异步 TCP 套接字正常关闭问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5638488/

相关文章:

c# - ASP.NET Core 中 Startup.cs 中的 Kestrel 关闭函数

c# - 这是使用 Web 服务激活串行 key 的好方法吗?

.NET CF 3.5 Forms 与仅同步网络设备对话

c# - 如何在MVC环境下实现DIP?

c# - Java 线程创建性能 vs C# 线程创建性能 vs C++( native 线程)?

c# - Windows Mobile 应用程序中 DataGrid 的 AutoSizeColumnsMode

.net - cf.net 排队等待 web 服务

iphone - 如何在 iphone 应用程序中实现 activesync 协议(protocol)?

iOS NSURLAuthenticationMethodClientCertificate 未请求与 ActiveSync 服务器

c++ - 不使用 activesync/WMDC 访问 Windows Mobile/CE 设备