我的程序抛出现有连接被远程主机异常强行关闭

发布于 2025-02-12 04:20:10 字数 1351 浏览 0 评论 0原文

我编写了一个小程序,该程序将获得一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2025-02-19 04:20:10

此错误通常意味着您需要在代码中实现TLS 1.2。幸运的是,这是一个单线,您需要在之前添加任何地方您的应用程序尝试到达端点。

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

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.

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文