C# .NET UDPClient:错误:远程主机强制关闭了现有连接

C# .NET UDPClient:错误:远程主机强制关闭了现有连接,c#,.net,udpclient,C#,.net,Udpclient,我有一个非常好的工作控制台程序,它使用UdpClient.send将消息发送到本地主机上的另一个程序(通过端口7777)。(奇怪的是,这个C#脚本的版本几乎完全相同,但是运行在unity3d中,并且它可以用相同的代码接收) 现在我需要那个程序的回复。我添加了一个线程(见下图),用于侦听端口7778上的消息。但当我开始这样说时,我犯了一个错误: 远程主机强制关闭了现有连接 使用Newtonsoft.Json; 使用制度; Net系统; 使用System.Net.Sockets; 使用系统文本; 使

我有一个非常好的工作控制台程序,它使用UdpClient.send将消息发送到本地主机上的另一个程序(通过端口7777)。(奇怪的是,这个C#脚本的版本几乎完全相同,但是运行在unity3d中,并且它可以用相同的代码接收)

现在我需要那个程序的回复。我添加了一个线程(见下图),用于侦听端口7778上的消息。但当我开始这样说时,我犯了一个错误:

远程主机强制关闭了现有连接

使用Newtonsoft.Json;
使用制度;
Net系统;
使用System.Net.Sockets;
使用系统文本;
使用系统线程;
命名空间爆炸程序
{
职业爆破师
{
UDP客户;
i端点输出点;
i端点输入点;
公共港口=7777;
公共国际机场=7778;
公共字符串hostName=“localhost”;
public int stepNum=0;
常数整数率=1000;
公共系统计时器计时器时钟;
线程侦听器;
静态void Main(字符串[]参数)
{
爆炸机b=新爆炸机();
b、 run();
}
爆破工()
{
client=新的UdpClient();
outPoint=新的IPEndPoint(Dns.GetHostAddresses(主机名)[0],端口);
inPoint=新的IPEndPoint(Dns.GetHostAddresses(主机名)[0],iPort);
}
无效运行()
{
this.stepNum=0;
侦听器=新线程(新线程开始(translater));
listener.IsBackground=true;
listener.Start();
Console.WriteLine(“按Enter键执行发送循环…\n”);
Console.ReadLine();
WriteLine(“开始于{0:HH:mm:ss.fff}”,DateTime.Now);
start();
控制台写入线(“按回车键停止”);
Console.ReadLine();
停止();
client.Close();
}
无效停止()
{
时钟停止();
clock.Dispose();
}
void start()
{
时钟=新系统。计时器。计时器(速率);
时钟已过+=发送;
clock.AutoReset=true;
clock.Enabled=true;
}
无效发送(对象源,System.Timers.ElapsedEventArgs e)
{
WriteLine(“发送:{0}”,stepNum);
Byte[]sendBytes=Encoding.ASCII.GetBytes(message());
尝试
{
Send(sendBytes,sendBytes.Length,outPoint);
}
捕获(异常错误)
{
Console.WriteLine(err.ToString());
}
}
字符串消息()
{
数据包p=新数据包();
p、 id=“汽车”;
p、 start=DateTime.Now;
p、 x=1.2f;
p、 y=0.4f;
p、 z=4.5f;
p、 step=stepNum++;
字符串json=JsonConvert.SerializeObject(p);
返回json;
}
void translator()
{
字节[]数据=新字节[0];
client.client.Bind(inPoint);
while(true)
{
尝试
{
数据=客户端接收(参考输入点);
}
捕获(异常错误)
{
Console.WriteLine(“Blaster.translater:接收数据错误:“+err.Message”);
client.Close();
返回;
}
string json=Encoding.ASCII.GetString(数据);
Console.WriteLine(json);
数据包p=JsonConvert.DeserializeObject(json);
}
}
}
}

好的,我看到过一些例子,人们在发送和接收(以及同一端口)中使用单个客户端对象。但后来我发现,如果它们在同一台主机上,则需要另一个端口。现在我看到您需要一个单独的udpClient

using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Blaster
{
    class Blaster
    {
        UdpClient client;
        IPEndPoint outPoint;
        IPEndPoint inPoint;
        public int oPort = 7777;
        public int iPort = 7778;
        public string hostName = "localhost";
        public int stepNum = 0;
        const int rate = 1000;
        public System.Timers.Timer clock;
        Thread listener;

        static void Main(string[] args)
        {
            Blaster b = new Blaster();
            b.run();
        }
        Blaster()
        {
            client = new UdpClient();
            outPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], oPort);
            inPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], iPort);
        }
        void run()
        {
            this.stepNum = 0;
            listener = new Thread(new ThreadStart(translater));
            listener.IsBackground = true;
            listener.Start();
            Console.WriteLine("Press Enter to do a send loop...\n");
            Console.ReadLine();
            Console.WriteLine("started at {0:HH:mm:ss.fff}", DateTime.Now);
            start();
            Console.WriteLine("Press Enter to stop");
            Console.ReadLine();
            stop();
            client.Close();
        }
        void stop()
        {
            clock.Stop();
            clock.Dispose();
        }
        void start()
        {
            clock = new System.Timers.Timer(rate);
            clock.Elapsed += send;
            clock.AutoReset = true;
            clock.Enabled = true;
        }
        void send(Object source, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine("sending: {0}", stepNum);
            Byte[] sendBytes = Encoding.ASCII.GetBytes(message());
            try
            {
                client.Send(sendBytes, sendBytes.Length, outPoint);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }
        }
        string message()
        {
            Packet p = new Packet();
            p.id = "car";
            p.start = DateTime.Now;
            p.x = 1.2f;
            p.y = 0.4f;
            p.z = 4.5f;
            p.step = stepNum++;
            string json = JsonConvert.SerializeObject(p);
            return json;
        }
        void translater()
        {
            UdpClient server = new UdpClient();
            Byte[] data = new byte[0];
            server.Client.Bind(inPoint);
            while (true)
            {
                try
                {
                    data = server.Receive(ref inPoint);
                }
                catch (Exception err)
                {
                    Console.WriteLine("Blaster.translater: recieve data error: " + err.Message);
                    client.Close();
                    return;
                }
                string json = Encoding.ASCII.GetString(data);
                Console.WriteLine(json);
                Packet p = JsonConvert.DeserializeObject<Packet>(json);
            }
        }
    }
}
使用Newtonsoft.Json;
使用制度;
Net系统;
使用System.Net.Sockets;
使用系统文本;
使用系统线程;
命名空间爆炸程序
{
职业爆破师
{
UDP客户;
i端点输出点;
i端点输入点;
公共港口=7777;
公共国际机场=7778;
公共字符串hostName=“localhost”;
public int stepNum=0;
常数整数率=1000;
公共系统计时器计时器时钟;
线程侦听器;
静态void Main(字符串[]参数)
{
爆炸机b=新爆炸机();
b、 run();
}
爆破工()
{
client=新的UdpClient();
outPoint=新的IPEndPoint(Dns.GetHostAddresses(主机名)[0],端口);
inPoint=新的IPEndPoint(Dns.GetHostAddresses(主机名)[0],iPort);
}
无效运行()
{
this.stepNum=0;
侦听器=新线程(新线程开始(translater));
listener.IsBackground=true;
listener.Start();
Console.WriteLine(“按Enter键执行发送循环…\n”);
Console.ReadLine();
WriteLine(“开始于{0:HH:mm:ss.fff}”,DateTime.Now);
start();
控制台写入线(“按回车键停止”);
Console.ReadLine();
停止();
client.Close();
}
无效停止()
{
时钟停止();
clock.Dispose();
}
void start()
{
时钟=新系统。计时器。计时器(速率);
时钟已过+=发送;
clock.AutoReset=true;
clock.Enabled=true;
}
无效发送(对象源,System.Timers.ElapsedEventArgs e)
{
WriteLine(“发送:{0}”,stepNum);
Byte[]sendBytes=Encoding.ASCII.GetBytes(message());
尝试
{
client.Send(sendBytes,
using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Blaster
{
    class Blaster
    {
        UdpClient client;
        IPEndPoint outPoint;
        IPEndPoint inPoint;
        public int oPort = 7777;
        public int iPort = 7778;
        public string hostName = "localhost";
        public int stepNum = 0;
        const int rate = 1000;
        public System.Timers.Timer clock;
        Thread listener;

        static void Main(string[] args)
        {
            Blaster b = new Blaster();
            b.run();
        }
        Blaster()
        {
            client = new UdpClient();
            outPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], oPort);
            inPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], iPort);
        }
        void run()
        {
            this.stepNum = 0;
            listener = new Thread(new ThreadStart(translater));
            listener.IsBackground = true;
            listener.Start();
            Console.WriteLine("Press Enter to do a send loop...\n");
            Console.ReadLine();
            Console.WriteLine("started at {0:HH:mm:ss.fff}", DateTime.Now);
            start();
            Console.WriteLine("Press Enter to stop");
            Console.ReadLine();
            stop();
            client.Close();
        }
        void stop()
        {
            clock.Stop();
            clock.Dispose();
        }
        void start()
        {
            clock = new System.Timers.Timer(rate);
            clock.Elapsed += send;
            clock.AutoReset = true;
            clock.Enabled = true;
        }
        void send(Object source, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine("sending: {0}", stepNum);
            Byte[] sendBytes = Encoding.ASCII.GetBytes(message());
            try
            {
                client.Send(sendBytes, sendBytes.Length, outPoint);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }
        }
        string message()
        {
            Packet p = new Packet();
            p.id = "car";
            p.start = DateTime.Now;
            p.x = 1.2f;
            p.y = 0.4f;
            p.z = 4.5f;
            p.step = stepNum++;
            string json = JsonConvert.SerializeObject(p);
            return json;
        }
        void translater()
        {
            UdpClient server = new UdpClient();
            Byte[] data = new byte[0];
            server.Client.Bind(inPoint);
            while (true)
            {
                try
                {
                    data = server.Receive(ref inPoint);
                }
                catch (Exception err)
                {
                    Console.WriteLine("Blaster.translater: recieve data error: " + err.Message);
                    client.Close();
                    return;
                }
                string json = Encoding.ASCII.GetString(data);
                Console.WriteLine(json);
                Packet p = JsonConvert.DeserializeObject<Packet>(json);
            }
        }
    }
}