我的程序抛出现有连接被远程主机异常强行关闭
我编写了一个小程序,该程序将获得一个UDP地址,然后将数据发送到TCP地址。当我以CMD运行程序并进行两个地址时,我会遇到错误。
这是代码:
static void Main(string[] args)
{
IPEndPoint listening = new IPEndPoint(IPAddress.Any, int.Parse(args[0]));
UdpClient udp = new UdpClient(listening);
System.Console.WriteLine($"Listening on {listening}");
var parts = args[1].Split(':');
IPEndPoint newrelic = new IPEndPoint(IPAddress.Parse(parts[0]), int.Parse(parts[1]));
TcpClient tcp = new TcpClient();
bool stop=false;
Console.CancelKeyPress+=(object? sender, ConsoleCancelEventArgs e) =>
{
stop = true;
tcp.Dispose();
udp.Close();
udp.Dispose();
};
tcp.Connect(newrelic);
System.Console.WriteLine($"Connected to {newrelic}");
var dummy = new IPEndPoint(IPAddress.Any,0);
while (!stop)
{
var bytes = udp.Receive(ref dummy);
if (bytes.Length > 0)
{
if (bytes[0] != 0x02)
{
System.Console.WriteLine($"Received {bytes.Length} bytes");
var sent = tcp.Client.Send(bytes);
}
}
}
}
我遇到的错误是:未经治疗的异常。 System.NET.Sockets.SocketException(10054):现有连接被远程主机强行关闭。 在c:\ tcptoudp \ program.cs中的program.main(string [] args):第0行,
有人知道为什么吗?
I have written a little program that takes one udp address and sends the data to a tcp address instead. When i run the program in CMD and taking the two addresses i get an error.
Here is the code:
static void Main(string[] args)
{
IPEndPoint listening = new IPEndPoint(IPAddress.Any, int.Parse(args[0]));
UdpClient udp = new UdpClient(listening);
System.Console.WriteLine(quot;Listening on {listening}");
var parts = args[1].Split(':');
IPEndPoint newrelic = new IPEndPoint(IPAddress.Parse(parts[0]), int.Parse(parts[1]));
TcpClient tcp = new TcpClient();
bool stop=false;
Console.CancelKeyPress+=(object? sender, ConsoleCancelEventArgs e) =>
{
stop = true;
tcp.Dispose();
udp.Close();
udp.Dispose();
};
tcp.Connect(newrelic);
System.Console.WriteLine(quot;Connected to {newrelic}");
var dummy = new IPEndPoint(IPAddress.Any,0);
while (!stop)
{
var bytes = udp.Receive(ref dummy);
if (bytes.Length > 0)
{
if (bytes[0] != 0x02)
{
System.Console.WriteLine(quot;Received {bytes.Length} bytes");
var sent = tcp.Client.Send(bytes);
}
}
}
}
The error i am getting is this: Unhandled exception. System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
at Program.Main(String[] args) in C:\TcpToUdp\Program.cs:line 0
Does anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此错误通常意味着您需要在代码中实现TLS 1.2。幸运的是,这是一个单线,您需要在之前添加任何地方您的应用程序尝试到达端点。
This error generally means that you need to implement TLS 1.2 in your code. Fortunately it's a one-liner that you need to add anywhere before your application attempts to reach the endpoint.