C#.NET套接字连接问题-每个套接字地址通常只允许使用一次
我有以下问题: 一旦我关闭WM6应用程序,然后再次尝试启动它,就会出现以下错误: 在System.Net.Sockets.socket.Bind(EndPoint localEP)中,通常只允许对每个套接字地址(协议/网络地址/端口)使用一次 在 System.Net.Sockets.Socket.TcpListener.Start() … 我认为这是由于连接超时的时间间隔造成的,所以我想关闭所有打开的连接并强制它创建一个新连接,这是正确的继续方式还是有不同的处理方式 以下是用于开始侦听的代码:C#.NET套接字连接问题-每个套接字地址通常只允许使用一次,c#,.net,windows-mobile,compact-framework,C#,.net,Windows Mobile,Compact Framework,我有以下问题: 一旦我关闭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());
}
}
}
//
///异步侦听客户端,创建ReceiveMessageHandler以处理读取。
///
///查看WIKI,TODOS
///
///
公共图书馆
{
myTcpListener.Start();
while(true)
{
//阻止,直到客户端连接到服务器
尝试
{
TcpClient myTcpClient=myTcpListener.AcceptTcpClient();
DateTime now=DateTime.now;
//测试是否需要创建客户端
ClientConnection client=newclientconnection(myTcpClient,新字节[myTcpClient.ReceiveBufferSize]);
//捕获特定的客户端并将其传递给接收处理程序
client.NetworkStream.BeginRead(client.Data,0,myTcpClient.ReceiveBufferSize,r=>receiveMessageHandler(r,client),null);
}
捕获(异常excp)
{
Debug.WriteLine(excp.ToString());
}
}
}
是的,您的服务器套接字可能处于等待时间状态
您可以访问底层,然后使用并指定。我在这里猜测
ClientConnection
是您的DLL,因为我看不到CF中已经包含了它
不过,如果你申报的话,你并不真的需要它
要使代码真正流畅,还应创建自己的类:
很简单。有了这个新的WmTcpEventArgs类和,您应该可以接收可以发布到控件之类的数据:
与其在代码中编写while(true)
,我更喜欢包含一个小的布尔变量
private bool abortListener;
代码如下所示:
public void Listen() {
listener.Start();
while (!abortListener) {
try {
using (var client = listener.AcceptTcpClient()) {
int MAX = client.ReceiveBufferSize;
var now = DateTime.Now;
using (var stream = client.GetStream()) {
Byte[] buffer = new Byte[MAX];
int len = stream.Read(buffer, 0, MAX);
if (0 < len) {
string data = Encoding.UTF8.GetString(buffer, 0, len);
MethodInvoker method = delegate { NetworkResponder(this, new WmTcpEventArgs(data)); };
abortListener = ((form1 == null) || form1.IsDisposed);
if (!abortListener) {
form1.Invoke(method);
}
}
}
}
} catch (Exception err) {
Debug.WriteLine(err.Message);
} finally {
listener.Stop();
}
}
}
public void Listen(){
listener.Start();
而(!abortListener){
试一试{
使用(var client=listener.AcceptTcpClient()){
int MAX=client.ReceiveBufferSize;
var now=DateTime.now;
使用(var stream=client.GetStream()){
字节[]缓冲区=新字节[MAX];
int len=stream.Read(缓冲区,0,最大值);
如果(0
请注意,您仍在捕获异常,但您也停止了。在关机时关闭myTcpListener。我会,但我正在调用一些DLL,这些DLL有时会使应用程序崩溃,并且不会调用关机代码。您需要更好地处理该错误。通过崩溃&让非托管资源闲置,您会得到这个错误。虽然CLR为您处理托管对象的内存管理,但您需要为非托管对象(如文件和网络连接)执行此操作。我遇到的问题是,我没有办法处理此参与方DLL,而且用户可能会使应用程序崩溃。我愿意接受关于如何处理第三方崩溃以及如何处理用户转到任务管理器并关闭应用程序的建议。我将尝试一下。。。我发现Compact Framework在sockets方面有一些怪癖,所以我会尝试看看是否可用。因此,在创建listner之后,我应该做:myTcpListener.Server.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true);
private void NetworkResponder(object sender, WmTcpEventArgs e) {
textBox1.Text = e.Data;
}
private bool abortListener;
public void Listen() {
listener.Start();
while (!abortListener) {
try {
using (var client = listener.AcceptTcpClient()) {
int MAX = client.ReceiveBufferSize;
var now = DateTime.Now;
using (var stream = client.GetStream()) {
Byte[] buffer = new Byte[MAX];
int len = stream.Read(buffer, 0, MAX);
if (0 < len) {
string data = Encoding.UTF8.GetString(buffer, 0, len);
MethodInvoker method = delegate { NetworkResponder(this, new WmTcpEventArgs(data)); };
abortListener = ((form1 == null) || form1.IsDisposed);
if (!abortListener) {
form1.Invoke(method);
}
}
}
}
} catch (Exception err) {
Debug.WriteLine(err.Message);
} finally {
listener.Stop();
}
}
}