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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:07:26+00:00 2026-05-16T06:07:26+00:00

Using a StreamWriter to write to a NetworkStream , and a StreamReader to read

  • 0

Using a StreamWriter to write to a NetworkStream, and a StreamReader to read the response. The app is sending commands and reading responses to a news server.

Simplified code (sans error handling, etc.):

tcpClient = new TcpClient();
tcpClient.Connect(Name, Port);

networkStream = tcpClient.GetStream();
serverReader = new StreamReader(networkStream, Encoding.Default);
serverWriter = new StreamWriter(networkStream, Encoding.ASCII) {
                     AutoFlush = true
                   };

// reads the server's response to the connect:  "200 news.newsserver.com"
// commenting out these lines doesn't solve the problem
while (serverReader.Peek() > -1) {
    serverReader.ReadLine();
}

serverWriter.WriteLine("authinfo user username");

// expect response "381 more authentication required", but code just blocks
string response = serverReader.ReadLine();

The code blocks at that last line, presumably waiting for the network stream to send a response.

I can avoid hanging the app by setting a timeout loop using serverReader.Peek(), but I will always timeout; I never get a response.

If I telnet to the server and port directly and enter the commands, I get an immediate response.

If I call serverWriter.Flush() explicitly, instead of using the AutoFlush property, I still block and never get a response.

Any ideas why I’m not getting a response to the server using this approach?

Thanks!

Resolved:

The above code does work for me, so I went back and built upon that code to the code that wouldn’t work.

In the code that hangs, I was still using the timeout loop with serverReader.Peek(). Peek() always returns -1, even though there is data in the buffer to read!! Replacing the Peek() loop with a blocking call to ReadLine() solves my problem.

I put the timeout loop in originally because the app is multi-threaded, and I didn’t want to block. I will have to revisit this issue and see how I can resolve the thread timing without using Peek().

Thanks all, good answers!

  • 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-05-16T06:07:27+00:00Added an answer on May 16, 2026 at 6:07 am

    The above code does work for me, so I went back and built upon that code to the code that wouldn’t work.

    In the code that hangs, I was still using the timeout loop with serverReader.Peek(). Peek() always returns -1, even though there is data in the buffer to read!! Replacing the Peek() loop with a blocking call to ReadLine() solves my problem.

    I put the timeout loop in originally because the app is multi-threaded, and I didn’t want to block. I will have to revisit this issue and see how I can resolve the thread timing without using Peek().

    Thanks all, good answers!

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

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Look up \b (word boundary): Matches at the position between… May 17, 2026 at 3:08 am
  • Editorial Team
    Editorial Team added an answer You need to be member of the local Administrators group… May 17, 2026 at 3:08 am
  • Editorial Team
    Editorial Team added an answer I have created yahoo dynamic map, the address can be… May 17, 2026 at 3:08 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I would like to write a program to receive some data using tcpClient from
I am using following code to write the PATH, EXECUTABLE NAME and ARGUMENTS to
I write my debug information to a file using a separate thread. During startup,
I'm trying to read mails from my live.com account, via the POP3 protocol. I've
Here's my situation: I'm writing a chat client to connect to a chat server.
I've been doing a lot of research on how best to write correct network
Am I being dense here? StreamReader.ReadLine states that: A line is defined as a
I get the following message back when trying to retrieve a file using TCPClient
I'm trying to automate svnadmin dump using C# ProcessStartInfo. The way I've done it
I have recently starting porting a small app of mine to Facebook as 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.