Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:29:27+00:00 2026-06-13T02:29:27+00:00

I’m not looking for a complete written solution, I only want to know what

  • 0

I’m not looking for a complete written solution, I only want to know what is going wrong, as it’s part of a school assignment. Both classes are written by the teacher, so I assume something is going wrong on my computer, but I have no idea where to look. I searched for some other solutions and can’t find any which is different from this one, except low-level solutions but I also got a low-level solution from my teacher and that also doesn’t work.

The server:

var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener server = new TcpListener(ipAddress, clientPort);
server.Start();
TcpClient client = server.AcceptTcpClient(); // The server gets here
StreamReader clientIn = new StreamReader(client.GetStream());
StreamWriter clientOut = new StreamWriter(client.GetStream());
clientOut.AutoFlush = true;
while (true)
{
    string msg = clientIn.ReadLine();
    Console.WriteLine(msg);
    clientOut.WriteLine(msg);  
}

The client:

TcpClient client = new TcpClient("localhost", serverPort); //The client gets here
StreamReader clientIn = new StreamReader(client.GetStream());
StreamWriter clientOut = new StreamWriter(client.GetStream());

clientOut.AutoFlush = true;

while (true)
{
    clientOut.WriteLine(Console.ReadLine());
    Console.WriteLine(clientIn.ReadLine());
}

As the client is in a try-catch block, which is in a loop until he has the connection, he tries to connect to the server many times. The server isn’t catched, as AcceptTcpClient just waits for a connection, but while they are on the same ip and on the port of the other process, they never reach any connection.

The connections are started in separate threads, but the main thread seems to wait until one is finished, which is not what I expected. I tried to let them both sleep on various methods (with Thread.Sleep(1000), Thread.Sleep(0) (documentation said that another thread would be scheduled if you’d do that) and while(stopwatch<1000ms) {for(i<100000)}), none of them helped. The main thread only has some progress at the moment that the sleep of the connection-thread is gone and the connection-thread again creates the client.

The problem also arises on another W7 64 bits computer.

Does anybody know what the problem is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:29:28+00:00Added an answer on June 13, 2026 at 2:29 am

    The problem almost certainly resides in the fact that you’re using IPAddress.Any when building the server. The reason that’s an issue is because that will not necessarily resolve to localhost, though you may get lucky, it’s not consistent. So, I would recommend starting up the server with an IP address like this:

    var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
    TcpListener server = new TcpListener(ipAddress, clientPort);
    

    Next, though I’m confident you’re doing this, ensure that the ports are the same for both clientPort and serverPort.

    Next, the while (true) loop is very suspect to me so in my example below I’m going to change that. Unless impossible always avoid while (true), you are literally begging for a problem.

    Finally, surrounding how you will do threading here, you’re going to need to build two separate threads somehow and I’m going to recommend the BackgroundWorker class (others may recommend async-await but I don’t know enough about it yet to recommend that and you would need to be using .NET 4.5 and I don’t know if you are).

    So, you could build a BackgroundWorker like this for the server (and you can build another one similar for the client):

    var worker = new BackgroundWorker();
    worker.WorkerReportsProgress = true;
    worker.WorkerSupportsCancellation = true;
    
    worker.ProgressChanged += (s, args) =>
    {
        Console.WriteLine(args.UserState);
    }
    
    worker.DoWork += (s, args) =>
    {
        // startup the server on localhost
        var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
        TcpListener server = new TcpListener(ipAddress, clientPort);
        server.Start();
    
        while (!worker.CancellationPending)
        {
            // as long as we're not pending a cancellation, let's keep accepting requests
            TcpClient client = server.AcceptTcpClient();
    
            StreamReader clientIn = new StreamReader(client.GetStream());
            StreamWriter clientOut = new StreamWriter(client.GetStream());
            clientOut.AutoFlush = true;
    
            while ((string msg = clientIn.ReadLine()) != null)
            {
                worker.ReportProgress(1, msg);  // this will fire the ProgressChanged event
                clientOut.WriteLine(msg);
            }
        }
    }
    

    Finally, somewhere you’ll need to startup these workers by calling RunWorkerAsync like this:

    worker.RunWorkerAsync();
    

    Update

    Alright, below is a fully working console application connecting on 2104. One thing that you need to note is that when using var ipAddress = Dns.GetHostEntry("localhost").AddressList[0]; we are getting an IP address that looks like this ::1 and that is the problem. However, if we listen on 127.0.0.1:2104 the client is able to connect because that what it’s trying to connect to when issuing var result = client.BeginConnect("localhost", 2104, null, null);, which would be the same as issuing new TcpClient("localhost", 2104);.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Net.Sockets;
    using System.ComponentModel;
    using System.Threading;
    using System.Net;
    
    namespace ConsoleApplication13
    {
        class Program
        {
            static void Main()
            {
                var worker = new BackgroundWorker();
                worker.WorkerReportsProgress = true;
                worker.WorkerSupportsCancellation = true;
    
                worker.ProgressChanged += (s, args) =>
                {
                    Console.WriteLine(args.UserState);
                };
    
                worker.DoWork += (s, args) =>
                {
                    // startup the server on localhost 
                    var ipAddress = IPAddress.Parse("127.0.0.1");
                    TcpListener server = new TcpListener(ipAddress, 2104);
                    server.Start();
    
                    while (!worker.CancellationPending)
                    {
                        Console.WriteLine("The server is waiting on {0}:2104...", ipAddress.ToString());
    
                        // as long as we're not pending a cancellation, let's keep accepting requests 
                        TcpClient attachedClient = server.AcceptTcpClient();
    
                        StreamReader clientIn = new StreamReader(attachedClient.GetStream());
                        StreamWriter clientOut = new StreamWriter(attachedClient.GetStream());
                        clientOut.AutoFlush = true;
    
                        string msg;
                        while ((msg = clientIn.ReadLine()) != null)
                        {
                            Console.WriteLine("The server received: {0}", msg);
                            clientOut.WriteLine(string.Format("The server replied with: {0}", msg));
                        }
                    }
                };
    
                worker.RunWorkerAsync();
    
                Console.WriteLine("Attempting to establish a connection to the server...");
    
                TcpClient client = new TcpClient();
    
                for (int i = 0; i < 3; i++)
                {
                    var result = client.BeginConnect("localhost", 2104, null, null);
    
                    // give the client 5 seconds to connect
                    result.AsyncWaitHandle.WaitOne(5000);
    
                    if (!client.Connected)
                    {
                        try { client.EndConnect(result); }
                        catch (SocketException) { }
    
                        string message = "There was an error connecting to the server ... {0}";
    
                        if (i == 2) { Console.WriteLine(message, "aborting"); }
                        else { Console.WriteLine(message, "retrying"); }
    
                        continue;
                    }
    
                    break;
                }
    
                if (client.Connected)
                {
                    Console.WriteLine("The client is connected to the server...");
    
                    StreamReader clientIn = new StreamReader(client.GetStream());
                    StreamWriter clientOut = new StreamWriter(client.GetStream());
    
                    clientOut.AutoFlush = true;
    
                    string key;
                    while ((key = Console.ReadLine()) != string.Empty)
                    {
                        clientOut.WriteLine(key);
                        Console.WriteLine(clientIn.ReadLine());
                    }
                }
                else { Console.ReadKey(); }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.