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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:49:37+00:00 2026-05-11T08:49:37+00:00

I am using a Socket to receive data via TCP, and TextReader.ReadLine to read

  • 0

I am using a Socket to receive data via TCP, and TextReader.ReadLine to read lines from the connection. There is a problem where a full line has not been received — TextReader.ReadLine returns an incomplete string. I want it to return null, indicating that a full line could not be read. How can I do this?

Basically, I have this data incoming:

'hello\nworld\nthis is a test\n' 

When I run ReadLine I get these in return:

'hello' 'world' 'this is a te' <null> <socket gets more data> 'st' <null> 

I do not want ‘this is a te’ returned. Rather, I want ‘this is a test’ to wait until the entire line has been received.

Code:

var endPoint = ...; var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); socket.Connect(endPoint);  var stream = new NetworkStream(socket, true);  var messageBuffer = new StringBuilder();  // Data received async callback (called several times). int bytesRead = stream.EndRead(result); string data = Encoding.UTF8.GetString(readBuffer.Take(bytesRead).ToArray()); messageBuffer.Append(data);  using(var reader = new StringReader(messageBuffer.ToString())) {     // This loop does not know that Message.Read reads lines.  For all it knows, it could read bytes or words or the whole stream.      while((Message msg = Message.Read(reader)) != null)  // See below.     {         Console.WriteLine(msg.ToString());    // See example input/echo above.     }      messageBuffer = new StringBuilder(reader.ReadToEnd()); }  // Method of Message. public static Message Read(TextReader reader) {     string line = reader.ReadLine();      if(line == null)         return null;      return Message.FromRawString(line); } 

Thanks.

  • 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. 2026-05-11T08:49:38+00:00Added an answer on May 11, 2026 at 8:49 am

    I decided to write my own ReadLine parser-ish kinda thing. Here’s the code:

    // Async callback. Message message;  while((message = Message.ReadBytes(messageBuffer)) != null) {     OnMessageReceived(new MessageEventArgs(message)); }  // Message class. public static Message ReadBytes(List<byte> data) {     int end = data.FindIndex(b => b == '\n' || b == '\r');      if(end == -1)         return null;      string line = Encoding.UTF8.GetString(data.Take(end).ToArray());      data.RemoveRange(0, end + 1);      if(line == '')         return ReadBytes(data);      if(line == null)         return null;      return Message.FromRawString(line); } 

    Many thanks to @Jon Skeet, @Noldorin, and @Richard for their very helpful suggestions. Your combined efforts led me to my final solution. =]

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

Sidebar

Ask A Question

Stats

  • Questions 82k
  • Answers 82k
  • 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 Detailed quick start: Partial output caching (some re-think here) ASP.NET… May 11, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer Throw an ArgumentException. See this related question for more detail:… May 11, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer Check out the excellent links below: ASP.NET Memory: If your… May 11, 2026 at 4:36 pm

Related Questions

My C# class must be able to process a high volume of events received
How should I check a (TCP) socket to find out whether it is connected?
I am writing a controller for an audio server on the iPhone. Each 'view'
I'm experimenting w/java sockets. I can connect to a socket and send/receive bytes of

Trending Tags

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

Top Members

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.