gpt4 book ai didi

c# - 调用 EndReceive 时抛出 ObjectDisposedException(异步客户端套接字)

转载 作者:行者123 更新时间:2023-12-03 11:59:15 26 4
gpt4 key购买 nike

我正在开发一个 C# 程序,我想在其中与 wifi 模块 ( RN 171 ) 交谈。

在我的 Program.cs 中,当我收到 RabbitMQ 消息时,我直接调用我的 Start 方法。

我从 this 开始示例(异步客户端套接字示例)。

该错误指示发生在以下行:

int bytesRead = client.EndReceive(ar);



但它真的出现在这一刻:

client.Shutdown(SocketShutdown.Both);
client.Close();



此解决方案有效(wifi 模块对我的命令使用react,但异常不令我满意)。这是我的代码,感谢您的帮助:
using System.Text;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace SDK_TestApp
{

public class Electricity
{
private const int port = 2000;
private static ManualResetEvent connectDone = new ManualResetEvent(false);
private static ManualResetEvent sendDone = new ManualResetEvent(false);
private static ManualResetEvent receiveDone = new ManualResetEvent(false);
private static String response = String.Empty;

public bool Start(String commande)
{
try
{

System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("192.168.1.17");

IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

Socket theClient = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

theClient.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), theClient);

connectDone.WaitOne();

Receive(theClient);
receiveDone.WaitOne();

Console.WriteLine("Connexion : {0}", response);

Send(theClient, commande);
sendDone.WaitOne();

Receive(theClient);
receiveDone.WaitOne();

Console.WriteLine("Response received : {0}", response);

theClient.Shutdown(SocketShutdown.Both);
theClient.Close();

return true;

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
}

private static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;

client.EndConnect(ar);

connectDone.Set();

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

private static void Receive(Socket client)
{
try
{

StateObject state = new StateObject();
state.workSocket = client;

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

private static void ReceiveCallback(IAsyncResult ar)
{
try
{

StateObject state = (StateObject)ar.AsyncState;

Socket clientReceiveCB = state.workSocket;

SocketError test;

int bytesRead = clientReceiveCB.EndReceive(ar, out test);

Console.WriteLine("test endReceive : " + test);

string testResponse = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
state.sb.Append(testResponse);

clientReceiveCB.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);

if (state.sb.Length > 1)
{
response = state.sb.ToString();
}

receiveDone.Set();

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

private static void Send(Socket client, String data)
{

byte[] byteData = Encoding.ASCII.GetBytes(data);
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);

}

private static void SendCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;

client.EndSend(ar);

sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

}

public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 256;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}

}

感谢您的帮助/解释:)

最佳答案

如果您阅读 documentation ,它指出 ObjectDisposedExceptionEndReceive() 的预期结果如果套接字在接收挂起时关闭,则方法。这是套接字通知您的应用程序 BeginReceive() 的原因的方式。操作结束是因为套接字已关闭。

除了 try..catch 真的什么都没有做异常,并执行您可能需要做的任何清理工作。

更新

我还注意到您对线程的处理有点不寻常。在这个应用程序中,您使用的是异步方法(BeginConnect()BeginReceive()),但是您手动阻塞了主线程,直到每个都使用 ManualResetEvents 完成。 .

从代码的外观来看,您似乎想要连接和阻塞,直到您读取服务器的响应,所以我建议将异步方法更改为同步方法( Connect()Receive() )并在Start方法。这不仅可以消除您在一个线程上关闭套接字而另一个线程正在读取它时遇到的问题,而且会使代码更加简洁。

关于c# - 调用 EndReceive 时抛出 ObjectDisposedException(异步客户端套接字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com