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 8862011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:37:30+00:00 2026-06-14T15:37:30+00:00

TcpClient client = new TcpClient(); client.Connect(IPAddress.Parse(HOST_IP), HOST_PORT); Stream stream = client.GetStream(); var reader =

  • 0
TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse(HOST_IP), HOST_PORT);
Stream stream = client.GetStream();
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true;
while (true) {
  string line;
  while((line = reader.ReadLine())!=null) {
    Console.WriteLine(line);
  }
  writer.WriteLine(Console.ReadLine());
}

My server sends two strings to the client:

var stream = new NetworkStream(soc);
var reader = new StreamReader(stream);
var writer = new StreamWriter(stream);
writer.AutoFlush = true
writer.WriteLine("Welcome: ");
writer.WriteLine("Bla...: ");
reader.ReadLine();

My client get will show

Welcome
Bla..

But it doesn’t run the command writer.WriteLine(Console.ReadLine());

  • 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-14T15:37:31+00:00Added an answer on June 14, 2026 at 3:37 pm

    You need to decide how this is all going to shutdown, but as indicated in the comments, I’d suggest you use a second thread, to get both sides (the send and receive sides on the client) able to run without blocking:

    TcpClient client = new TcpClient();
    client.Connect(IPAddress.Parse(HOST_IP), HOST_PORT);
    Stream stream = client.GetStream();
    var reader = new StreamReader(stream);
    var writer = new StreamWriter(stream);
    writer.AutoFlush = true;
    var t = new Thread(() => {
        while(true) {
            writer.WriteLine(Console.ReadLine());
        }
    });
    t.Start();
    while (true) {
      string line;
      while((line = reader.ReadLine())!=null) {
        Console.WriteLine(line);
      }
    }
    

    Unlike in your comment, you wouldn’t want to create the threads in a loop – there’s always a possibility that one side (e.g. receiving from the server) can process multiple times without any work for the other side (sending to the server).

    For shutdown, I’d usually create a ManualResetEvent that gets set (by whatever criteria are appropriate) when it’s time to shutdown. I’d then make testing the event object the condition for the while loops.

    The only issue is that Console.ReadLine() is blocking. So your read console/send to server thread wouldn’t see that the shutdown event has been set unless it receives new input. If your read/send thread isn’t responsible for initiating shutdown, you could make it a background thread (assuming it doesn’t mutate any global state) and then just deal with shutting down your receive/write thread (i.e. the main thread) at an appropriate time.

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

Sidebar

Related Questions

Part of my n00b server: TcpClient client = tcpListener.AcceptTcpClient(); Thread clientThread = new Thread(new
I implemented TCP client to connect to the server using TcpClient ( C# .NET
Client Code: TcpClient client = new TcpClient(); NetworkStream ns; private void Form1_Load(object sender, EventArgs
I want to use StreamReader and StreamWriter to receive and send data over TCPClient.NetworkStream.
I am trying to connect to a standalone desktop tcp server(java) from tcp client
I have a TcpClient application that send a message from client to the server.
I created a client and server that connect to each other. The server is
This is one of my first issues. Whenever I exit out the program, tcpClient.Connect()
Say I have the following code: public static Client Connect(string hostname, int port, bool
I have a server app and sometimes, when the client tries to connect, I

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.