【发布时间】:2013-03-01 15:28:45
【问题描述】:
我遇到以下问题:
一旦我关闭我的 WM6 应用程序,然后尝试再次启动它,我就会收到以下错误: 在 System.Net.Sockets.Socket.Bind(EndPoint localEP) 通常只允许使用每个套接字地址(协议/网络地址/端口) 在 System.Net.Sockets.Socket.TcpListener.Start() ...
我认为这是由于连接超时的时间间隔,所以我想关闭所有打开的连接并强制它创建一个新连接,这是正确的继续方式还是有不同的方式处理这个?
下面是开始监听的代码:
/// <summary>
/// Listens Asynchronously to Clients, creates a recieveMessageHandler to process the read.
///
/// Check WIKI, TODOS
/// </summary>
/// <returns></returns>
public void Listen()
{
myTcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
try
{
TcpClient myTcpClient = myTcpListener.AcceptTcpClient();
DateTime now = DateTime.Now;
//Test if it's necessary to create a client
ClientConnection client = new ClientConnection(myTcpClient, new byte[myTcpClient.ReceiveBufferSize]);
// Capture the specific client and pass it to the receive handler
client.NetworkStream.BeginRead(client.Data, 0, myTcpClient.ReceiveBufferSize, r => receiveMessageHandler(r, client), null);
}
catch (Exception excp)
{
Debug.WriteLine(excp.ToString());
}
}
}
【问题讨论】:
-
关闭时关闭 myTcpListener。
-
我愿意,但是我正在调用一些 Dll,这些 Dll 有时会导致应用程序崩溃并且不会调用关闭代码。
-
您需要更好地处理该错误。只是崩溃并留下您的非托管资源,您会收到此错误。尽管 CLR 为您处理托管对象的内存管理,但您需要为非托管对象执行此操作,例如文件和网络连接。
-
我的问题是我没有办法处理该方的 DLL,而且用户可能会使应用程序崩溃。我愿意接受有关如何处理第三方崩溃以及如何处理用户进入任务管理器并关闭应用程序的建议。
标签: c# .net windows-mobile compact-framework