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 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

Related Questions

I am using socket to send data from local machine to remote in TCP,
I'm writting a client using socket to receive EEG data from a recording PC
I have a client server situation in which I receive data using read(socket, char_buf,
I am using vector<char> to send and receive data via socket. In this vector
I have a application which uses socket connection to send and receive data from
I am using the Naga library to read data from a socket, which produces
I'm downloading 6kB of data from a test instrument connected via TCP socket. I'm
I'm trying perform a relatively basic TCP socket send/receive operation on Windows CE using
I have a Php application using stream_socket_client(), to get data through tcp from a
I'm using Python 2.7 sockets to receive data: data = self.socket.recv(4096) How do 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.