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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:45:47+00:00 2026-06-11T18:45:47+00:00

New to TCP socket communications in C#, having a problem where I send some

  • 0

New to TCP socket communications in C#, having a problem where I send some data across the network stream from my client app, then read it from my server app. Data comes through fine the first time, but the next time I try to send data, the old data is still in the network stream and just gets overridden up to whatever the length of the new data is. This is causing issues when trying to parse the data in the stream. I have tried adding a null termination but it doesn’t seem to have any effect.

How can I empty the network stream before sending more data across?

We send a string such as this:

1|0|bob|cornell|9/14/2012 12:49:34 AM

Then another one like this:

1|0|jim|horne|9/14/2012 12:49:34 AM

But the second string goes through as :

1|0|jim|horne|9/14/2012 12:49:34 AMAM

…followed by a bunch of \0.

The last chunk is a DateTime, and it is failing to convert the string to a DateTime because of the extra AM. Even when appending \0 to the end of the string we are sending across the stream, it won’t work. For example:

1|0|jim|horne|9/14/2012 12:49:34 AM\0M

It seems to treat the null termination as just another character, rather than a signal to stop reading the string.

It seems that “Flush” does nothing on NetworkStream types. Must be a dumb problem here, but any help is appreciated.

The client does this:

private static void writeToServer(MessagePacket message, NetworkStream clientStream)
    {
        clientStream.Write(message.ToBytes(), 0, message.ToBytes().Length);
    }

The server does this:

byte[] rawMessage = new byte[4096];

        while (true)
        {

            try
            {
                clientStream.Read(rawMessage, 0, 4096);
                clientStream.Flush();
            }

            catch
            {
                break;
            }


            newPacket = new MessagePacket(rawMessage);
        }
  • 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-11T18:45:49+00:00Added an answer on June 11, 2026 at 6:45 pm

    Stream.Read() returns the number of received bytes, the bytes after after that are undefined.

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

Sidebar

Related Questions

I have a TCP socket client receiving messages (data) from a server. messages are
I need to use the tcp socket connection to get the data from a
Does Android TCP Socket Client read one more line the response?? inputStreamReader = new
I am trying to create a new TCP socket in Android emulator. Below is
I'm using this kind of code for my TCP/IP connection: sock = new Socket(host,
I am new to TCP programming so here is a few simple questions: Client
My client application uses a boost::asio::ip::tcp::socket to connect to a remote server. If the
The socket API is the de-facto standard for TCP/IP and UDP/IP communications (that is,
I am new to socket programming and ran into a puzzling problem: I have
Im trying out the new additions of Socket-communication (TCP & UDP) in the Beta

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.