Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. "No connection could be made because the target machine actively refused it"

"No connection could be made because the target machine actively refused it"

Scheduled Pinned Locked Moved C#
help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Megidolaon
    wrote on last edited by
    #1

    I get this error when trying to send something between two apps over TCP. The weird part is that I can send one time without any trouble, but whenever I try a second time, I get this error. I use this code for the sending app:

    private void B_Send_Click(object sender, EventArgs e)
    {
    string send = "";
    send = TB_Quotes.Text;

            TcpClient tcp = new TcpClient(TB\_Host.Text, (int)UD\_Port.Value);
            NetworkStream ns = tcp.GetStream();
            StreamWriter writer = new StreamWriter(ns);
    
            writer.WriteLine(send);
            writer.Flush();
           
            ns.Close();
            writer.Close();
            tcp.Close();
        }
    

    And this code for the receiving app:

    public Form1()
    {
    InitializeComponent();
    Thread treceive = new Thread(new ThreadStart(Listen));
    treceive.Start();
    }

        public void Listen()
        {
            int port = 2112;
            string result = "";
            IPAddress localaddress = IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(localaddress, port);
            listener.Start();
            
            TcpClient client = listener.AcceptTcpClient();
            NetworkStream ns = client.GetStream();
            StreamReader reader = new StreamReader(ns);
            
            result = reader.ReadToEnd();
            Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object\[\] {result});
        }
        public void UpdateDisplay(string text)
        {
            TB\_Quotes.Text = text;
        }
    

    I'm making sure the port is 2112 (if not, the first time would not be successful), but I don't understand why the 2nd time won't work. Thanks.

    M L 2 Replies Last reply
    0
    • M Megidolaon

      I get this error when trying to send something between two apps over TCP. The weird part is that I can send one time without any trouble, but whenever I try a second time, I get this error. I use this code for the sending app:

      private void B_Send_Click(object sender, EventArgs e)
      {
      string send = "";
      send = TB_Quotes.Text;

              TcpClient tcp = new TcpClient(TB\_Host.Text, (int)UD\_Port.Value);
              NetworkStream ns = tcp.GetStream();
              StreamWriter writer = new StreamWriter(ns);
      
              writer.WriteLine(send);
              writer.Flush();
             
              ns.Close();
              writer.Close();
              tcp.Close();
          }
      

      And this code for the receiving app:

      public Form1()
      {
      InitializeComponent();
      Thread treceive = new Thread(new ThreadStart(Listen));
      treceive.Start();
      }

          public void Listen()
          {
              int port = 2112;
              string result = "";
              IPAddress localaddress = IPAddress.Parse("127.0.0.1");
              TcpListener listener = new TcpListener(localaddress, port);
              listener.Start();
              
              TcpClient client = listener.AcceptTcpClient();
              NetworkStream ns = client.GetStream();
              StreamReader reader = new StreamReader(ns);
              
              result = reader.ReadToEnd();
              Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object\[\] {result});
          }
          public void UpdateDisplay(string text)
          {
              TB\_Quotes.Text = text;
          }
      

      I'm making sure the port is 2112 (if not, the first time would not be successful), but I don't understand why the 2nd time won't work. Thanks.

      M Offline
      M Offline
      Manfred Rudolf Bihy
      wrote on last edited by
      #2

      The Listen method ends after the Invoke bit at the end (also terminating the started thread). Thus it can not accept new connections (client = listener.AcceptTcpClient(). You'd have to have some kind of loop where the server continually accepts connections. From within the loop you could start threads that do the handling for each individual connection. Cheers Manfred

      M 1 Reply Last reply
      0
      • M Manfred Rudolf Bihy

        The Listen method ends after the Invoke bit at the end (also terminating the started thread). Thus it can not accept new connections (client = listener.AcceptTcpClient(). You'd have to have some kind of loop where the server continually accepts connections. From within the loop you could start threads that do the handling for each individual connection. Cheers Manfred

        M Offline
        M Offline
        Megidolaon
        wrote on last edited by
        #3

        Thanks. I added a loop and now it works just fine.

        M 1 Reply Last reply
        0
        • M Megidolaon

          Thanks. I added a loop and now it works just fine.

          M Offline
          M Offline
          Manfred Rudolf Bihy
          wrote on last edited by
          #4

          If the answer was helpful, I'd really appreciate a vote.

          1 Reply Last reply
          0
          • M Megidolaon

            I get this error when trying to send something between two apps over TCP. The weird part is that I can send one time without any trouble, but whenever I try a second time, I get this error. I use this code for the sending app:

            private void B_Send_Click(object sender, EventArgs e)
            {
            string send = "";
            send = TB_Quotes.Text;

                    TcpClient tcp = new TcpClient(TB\_Host.Text, (int)UD\_Port.Value);
                    NetworkStream ns = tcp.GetStream();
                    StreamWriter writer = new StreamWriter(ns);
            
                    writer.WriteLine(send);
                    writer.Flush();
                   
                    ns.Close();
                    writer.Close();
                    tcp.Close();
                }
            

            And this code for the receiving app:

            public Form1()
            {
            InitializeComponent();
            Thread treceive = new Thread(new ThreadStart(Listen));
            treceive.Start();
            }

                public void Listen()
                {
                    int port = 2112;
                    string result = "";
                    IPAddress localaddress = IPAddress.Parse("127.0.0.1");
                    TcpListener listener = new TcpListener(localaddress, port);
                    listener.Start();
                    
                    TcpClient client = listener.AcceptTcpClient();
                    NetworkStream ns = client.GetStream();
                    StreamReader reader = new StreamReader(ns);
                    
                    result = reader.ReadToEnd();
                    Invoke(new UpdateDisplayDelegate(UpdateDisplay), new object\[\] {result});
                }
                public void UpdateDisplay(string text)
                {
                    TB\_Quotes.Text = text;
                }
            

            I'm making sure the port is 2112 (if not, the first time would not be successful), but I don't understand why the 2nd time won't work. Thanks.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Megidolaon wrote:

            writer.Flush();

            one remark: you oinly need to flush when you don't close right away; here you want to close, so don't flush, there is no need to. However, you should close nested things in reverse order: since you opened ns and then writer, you should close writer, then ns. That way, no flush is required. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups