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

  • SEARCH
  • Home
  • 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 8916025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:07:44+00:00 2026-06-15T05:07:44+00:00

I’m making a basic C# TCP Console Server that accepts connections from 2 different

  • 0

I’m making a basic C# TCP Console Server that accepts connections from 2 different clients. After that the server needs to be able to receive messages from the two clients after they have connected, this should be able to go on until the connection has been terminated.
So far I can send messages to the clients, but I’m in trouble with having two clients getting to talk to the server at once. I’m pretty sure I just have to add some threads in here but I’m new to it so I’m not sure how to get it working. That’s where I need assistance.

string str1 = "";
            string str2 = "";
            Socket soc1;
            Socket soc2;
            NetworkStream straumur1;
            NetworkStream straumur2;
            BinaryReader lesari1;
            BinaryReader lesari2;
            BinaryWriter skrifari1;
            BinaryWriter skrifari2;
            IPAddress ipTala = IPAddress.Parse("127.0.0.1");
            TcpListener hlustari = new TcpListener(ipTala, 50000);
            Lesa lesa = new Lesa();
            hlustari.Start();


            //Notandi 1 tengist
            soc1 = hlustari.AcceptSocket();
            straumur1 = new NetworkStream(soc1);
            skrifari1 = new BinaryWriter(straumur1);
            lesari1 = new BinaryReader(straumur1);
            skrifari1.Write("Leikmaður 1");
            Console.WriteLine(">> Leikmaður 1 tengdist");

            //Notnadi 2 tengist
            soc2 = hlustari.AcceptSocket();
            straumur2 = new NetworkStream(soc2);
            skrifari2 = new BinaryWriter(straumur2);
            lesari2 = new BinaryReader(straumur2);
            skrifari2.Write("Leikmaður 2");
            Console.WriteLine(">> Leikmaður 2 tengdist");

            do //this is the part that needs fixing.
            {
                str1 = lesari1.ReadString();
                skrifari1.Write(str1);
                Console.WriteLine(str1);
            } while (true);

            do
            {
                str2 = lesari2.ReadString();
                skrifari2.Write(str2);
                Console.WriteLine(str2);
            } while (true);
  • 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-15T05:07:45+00:00Added an answer on June 15, 2026 at 5:07 am

    You’ve blocked the execution flow by infinite looping with the first BinaryReader. A simple apporoach would be looping them in different threads, and from the main thread, waiting both. I don’t have access to Visual Studio right now, but something like this might work:

    //create the threads with an anonymous method (you may extract them to named methods, if you prefer)
    Thread t1 = new Thread(new ThreadStart(()=>{
        do //this is the part that needs fixing.
            {
                str1 = lesari1.ReadString();
                Console.WriteLine(str1);
            } while (true);
        }
       ));
    
     Thread t2 = new Thread(new ThreadStart(()=>{
        do //this is the part that needs fixing.
            {
                str2 = lesari2.ReadString();
                Console.WriteLine(str2);
            } while (true);
        }
       ));
    
      t1.Start(); //start threads
      t2.Start();
      t1.Join(); //wait for them to finish
      t2.Join();
    

    Again, I’m not sure if there are any typos or naming errors, but something like this would do the trick. Also, whenever you access shared data (an object that you can access from both threads), be careful not to have any race conditions. This is a wonderful tutorial/resource for .NET threading: http://www.albahari.com/threading/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.