- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
以下代码,尽管显然关闭了 UDP 套接字,但仍使其挂起并且无法重新连接到相同的地址/端口。
这些是我使用的类变量:
Thread t_listener;
List<string> XSensAvailablePorts;
private volatile bool stopT_listener = false;
volatile UdpClient listener;
IPEndPoint groupEP;
我创建并启动了一个新线程,该线程使用一种方法来处理 Socket 连接和监听:
private void startSocketButton_Click(object sender, RoutedEventArgs e)
{
stopT_listener = false;
closeSocketButton.IsEnabled = true;
startSocketButton.IsEnabled = false;
t_listener = new Thread(UDPListener);
t_listener.Name = "UDPListenerThread";
t_listener.Start();
}
方法如下(我在 Receive
上使用超时,以便在套接字上没有发送任何内容时不让它阻塞,并且 Stop
正在发行):
private void UDPListener()
{
int numberOfPorts = XSensAvailablePorts.Count();
int currAttempt = 0;
int currPort = 0;
bool successfullAttempt = false;
while (!successfullAttempt && currAttempt < numberOfPorts)
{
try
{
currPort = Int32.Parse(XSensAvailablePorts[currAttempt]);
listener = new UdpClient(currPort);
successfullAttempt = true;
}
catch (Exception e)
{
currAttempt++;
}
}
if (successfullAttempt)
{
//timeout = 2000 millis
listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000);
//Tried with and without, no change: listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
statusTB.Dispatcher.BeginInvoke((Action)delegate() { statusTB.Text = "Connected on port:" + currPort; });
groupEP = new IPEndPoint(IPAddress.Parse("143.225.85.130"), currPort);
byte[] receive_byte_array;
try
{
while (!stopT_listener)
{
try
{
receive_byte_array = listener.Receive(ref groupEP);
if (receive_byte_array.Length == 0 || receive_byte_array == null)
continue;
ParseData(receive_byte_array, samplingDatagramCounter);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.TimedOut)
continue;
}
}
}
catch (Exception e)
{
Debug.Print(e.Message);
}
finally
{
if (listener != null)
{
listener.Client.Shutdown(SocketShutdown.Both);
listener.Close();
}
}
}
statusTB.Dispatcher.BeginInvoke((Action)delegate() { statusTB.Text = "Not Connected"; });
return;
}
我用这个方法命令线程/套接字停止:
private void closeSocketButton_Click(object sender, RoutedEventArgs e)
{
stopT_listener = true;
closeSocketButton.IsEnabled = false;
startSocketButton.IsEnabled = true;
t_listener.Join();
if (listener.Client != null)
{
listener.Client.Shutdown(SocketShutdown.Both);
listener.Close();
}
if (t_listener.IsAlive)
{
t_listener.Abort();
statusTB.Text = "Aborted";
}
else
statusTB.Text = "Not Connected";
}
尽管在调试中检查套接字已关闭,但如果我重试连接到同一个端口,我将无法这样做,因为它引发了一个 SocketException
表示只有一个端口/地址通常是允许的。
最佳答案
我把你提供的代码放在一个简单的表格中运行它......我不能直接重现你的问题。虽然我没有向客户端发送任何数据,但据我所知,它不应该改变任何东西,因为它是 UDP,我们正在调查(重新)打开套接字,而不是传输数据。
当单击开始/停止按钮时,套接字始终正确打开和关闭,重新打开按预期工作。对我来说,强制执行您提到的 SocketException 的唯一方法是引入一些明显的套接字逻辑滥用:
我对您的代码所做的唯一更改是删除 ParseData(...) 行并将一些端口添加到 XSensAvailablePorts 列表。
你能检查端口在你明显关闭后是否仍然打开吗?您可以使用 netstat -an
,或者一个优秀的工具 ProcessExplorer。您还可以检查 t_listener 线程是否正确终止(标准任务管理器或 ProcessExplorer 可以帮助您)。
关于c# - (显然)优雅地关闭 UDPClient 使套接字被阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23564939/
我有一个小问题和一个解决方案,但我想了解到底发生了什么。根据 UdpClient 的构造函数,我使用 EndReceive() 是否抛出套接字异常(10054 - 现有连接已被远程主机关闭(翻译自德语
从本质上讲,除了明显的区别之外,它们之间还有哪些区别?我什么时候应该使用哪种形式? class What { public Go() { Thread thread =
我正在维护其他人的代码及其使用类 UdpClient .该代码声明了 UdpClient 的一个实例并使用 UdpClient.Receive() 连续接收数据. 当接收到数据时,在另一个线程中进行处
我正在使用下面的代码,似乎有时套接字未释放 我是怎么发现的? 通过使用进程浏览器(来自sysinternal), 申请的专有权 然后进入“TCP/IP”选项卡。 通过在控制台中输入“netstat -
我正在尝试构建一个客户端服务器套接字库,它将抽象发送和接收数据的复杂性。我想知道你是否有一个 UDP 服务器设置来拥有一个多播组。如果需要,它仍然可以向特定客户端发送和接收单播请求吗?还是您必须打开两
我一直在试验 C# 中的 UDP 发送和接收,但遇到了一个奇怪的问题。该代码在控制台应用程序中运行良好,但当我尝试在服务中使用完全相同的代码时,client.Receive 方法被阻止。该服务正常运行
我们有一个从远程设备收集遥测数据的生产系统。此数据以合理的频率发送,我们最终在高峰时段每秒接收多达数千条消息。每条消息的有效负载约为 80 字节。我开始对各种存储机制进行一些性能测试,但我想首先我会尝
我是 C# UDP 编码的新手,在我的电脑上本地使用 UDP 客户端时,我有一些“奇怪”的行为。我想将 UDP 数据从我电脑上的一个端口 (11000) 发送到同一台电脑上的另一个端口 (12000)
在 c# 中,我正在使用 UdpClient.Receive 函数: public void StartUdpListener(Object state) { try
我在使用 C# 中的 UdpClient 时遇到问题。我正在两个客户端之间通过互联网流式传输音频。 在我的麦克风上,采样率为 16khz,我发送带有音频的 UDP 数据包,每个数据包 6400 字节。
在尝试处理 UdpClient 时,我发现这是不可能的。对于以下内容: UdpClient udpClient = new UdpClient(); udpClient.Dispose(); Visu
我只是在玩 UdpClient 并注意到将 UdpClient.EnableBroadcast 设置为 true 或 false 不会没有任何(副作用)效果,我可以用任何一种方式进行广播: using
我正在实现一个简单的本地网络发现协议(protocol),因此我调用了 UdpClient.Send,然后调用了 UdpClient.BeginReceive。如果有多个响应未决,我会在回调结束时调用
我正在尝试将 LAN 服务器发现添加到我的 Unity 游戏中。我正在使用 UDP 广播请求。如果网络上的任何人是服务器,他们将响应一些服务器数据。我已经弄清楚了这部分,但我需要使用一些 Unity
我正在创建一个聊天应用程序,其中用户位于路由器 (NAT) 后面。所以主要问题是向这些客户发送消息。服务器接收消息没有问题。 这是我的协议(protocol)(规则): UdpServer监听来自 c
我正在尝试运行一个基本的 UDPCLient-Server 程序。当我编译 UDPClient 程序时,会导致以下错误。虽然我的服务器编译成功。 有人可以指导出什么问题吗? 错误: UDPClient
public void send_multicast(string message) { UdpClient c = new UdpClient(10102);
我在构建指定 IPv4 系列的 UdpClient 对象时遇到异常。这只发生在一台 Windows 7 64 位机器上,其他具有相同操作系统的机器工作正常。 确切的异常(exception)是: Sy
我有一个服务于某些线程的类实例(我们称它为 A),该实例仅通过 UdpClient 类发送 UDP 数据包。它在其构造函数中初始化 UdpClient,仅用于发送数据包。 它看起来像: public
美好的一天。我使用 UdpClient 并对其进行包装。 为了阅读我有异步方法: private async Task Receive(UdpClient client, CancellationTo
我是一名优秀的程序员,十分优秀!