.NET UDPClient: Error: An existing connection was forcibly closed by the remote host

Dr.YSG

I had a perfectly fine working console program that uses UdpClient.send to send messages to another program on the localhost (over port 7777). (which oddly enough is an almost identical version this C# script, but running in unity3d, and it has no trouble receiving with the same code).

Now I need to get replies from that program. I added a thread (see bottom) which listens on port 7778 for messages. But I am getting an error when starting saying that:

An existing connection was forcibly closed by the remote host

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()
        {
            Byte[] data = new byte[0];
            client.Client.Bind(inPoint);
            while (true)
            {
                try
                {
                    data = client.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);
            }
        }
    }
}
Dr.YSG

Ok, I had seen some examples of folks using a single client object for both send and receive (as well as the same port). But then I saw a different port was needed if they were on the same host. Now I see you need a separate 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);
            }
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

C# HttpClient An existing connection was forcibly closed by the remote host

From Dev

How to catch this Python exception: error: [Errno 10054] An existing connection was forcibly closed by the remote host

From Dev

ServiceModel error logged on server when closing client application: existing connection was forcibly closed by the remote host

From Dev

Can't run app on my phone for testing: Adb connection Error:An existing connection was forcibly closed by the remote host

From Dev

Error: Adb connection Error:An existing connection was forcibly closed by the remote host

From Dev

SVN error running context: An existing connection was forcibly closed by the remote host

From Dev

Connection was forcibly closed by the remote host

From Dev

"An existing connection was forcibly closed by the remote host" error in IIS, but not VS dev server

From Dev

TcpClient - An existing connection was forcibly closed by the remote host

From Dev

android I/O Error: An existing connection was forcibly closed by the remote host

From Dev

"An existing connection was forcibly closed by the remote host" while executing mongorestore on localhost

From Dev

TotoiseHG: An existing connection was forcibly closed by the remote host

From Dev

Boost.Asio error code for "An existing connection was forcibly closed by the remote host"

From Dev

urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))

From Dev

System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host

From Dev

Error: Adb connection Error:An existing connection was forcibly closed by the remote host

From Dev

Cannot Checkout, "An existing connection was forcibly closed by the remote host"

From Dev

Python exception: error: [Errno 10054] An existing connection was forcibly closed by the remote host

From Dev

High Availability Error “An error occurred while receiving data: '10054(An existing connection was forcibly closed by the remote host.)'.”

From Dev

How to return IEnumerable in WCF ? An existing connection was forcibly closed by the remote host

From Dev

"An existing connection was forcibly closed by the remote host" while executing mongorestore on localhost

From Dev

"An existing connection was forcibly closed by the remote host" when listening for incoming data

From Dev

.NET UDPClient: Error: An existing connection was forcibly closed by the remote host

From Dev

Boost.Asio error code for "An existing connection was forcibly closed by the remote host"

From Dev

Hmm An existing connection was forcibly closed by the remote host

From Dev

An existing connection was forcibly closed by the remote host in authorize.net

From Dev

Existing connection was forcibly closed by the remote host

From Dev

Connection was forcibly closed by the remote host on receive

From Dev

An existing connection was forcibly closed by the remote host [BigChainDB]

Related Related

  1. 1

    C# HttpClient An existing connection was forcibly closed by the remote host

  2. 2

    How to catch this Python exception: error: [Errno 10054] An existing connection was forcibly closed by the remote host

  3. 3

    ServiceModel error logged on server when closing client application: existing connection was forcibly closed by the remote host

  4. 4

    Can't run app on my phone for testing: Adb connection Error:An existing connection was forcibly closed by the remote host

  5. 5

    Error: Adb connection Error:An existing connection was forcibly closed by the remote host

  6. 6

    SVN error running context: An existing connection was forcibly closed by the remote host

  7. 7

    Connection was forcibly closed by the remote host

  8. 8

    "An existing connection was forcibly closed by the remote host" error in IIS, but not VS dev server

  9. 9

    TcpClient - An existing connection was forcibly closed by the remote host

  10. 10

    android I/O Error: An existing connection was forcibly closed by the remote host

  11. 11

    "An existing connection was forcibly closed by the remote host" while executing mongorestore on localhost

  12. 12

    TotoiseHG: An existing connection was forcibly closed by the remote host

  13. 13

    Boost.Asio error code for "An existing connection was forcibly closed by the remote host"

  14. 14

    urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))

  15. 15

    System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host

  16. 16

    Error: Adb connection Error:An existing connection was forcibly closed by the remote host

  17. 17

    Cannot Checkout, "An existing connection was forcibly closed by the remote host"

  18. 18

    Python exception: error: [Errno 10054] An existing connection was forcibly closed by the remote host

  19. 19

    High Availability Error “An error occurred while receiving data: '10054(An existing connection was forcibly closed by the remote host.)'.”

  20. 20

    How to return IEnumerable in WCF ? An existing connection was forcibly closed by the remote host

  21. 21

    "An existing connection was forcibly closed by the remote host" while executing mongorestore on localhost

  22. 22

    "An existing connection was forcibly closed by the remote host" when listening for incoming data

  23. 23

    .NET UDPClient: Error: An existing connection was forcibly closed by the remote host

  24. 24

    Boost.Asio error code for "An existing connection was forcibly closed by the remote host"

  25. 25

    Hmm An existing connection was forcibly closed by the remote host

  26. 26

    An existing connection was forcibly closed by the remote host in authorize.net

  27. 27

    Existing connection was forcibly closed by the remote host

  28. 28

    Connection was forcibly closed by the remote host on receive

  29. 29

    An existing connection was forcibly closed by the remote host [BigChainDB]

HotTag

Archive