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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:42:01+00:00 2026-05-29T21:42:01+00:00

I implemented TCP client to connect to the server using TcpClient ( C# .NET

  • 0

I implemented TCP client to connect to the server using TcpClient (C# .NET 4):

// TCP client & Connection
TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse(IP), PORT));
NetworkStream clientStream = client.GetStream();

// Get message from remote server
byte[] incomingBuffer = new byte[1024];
Int32 bytes = clientStream.Read(incomingBuffer, 0, incomingBuffer.Length);
string problesWithThis = System.Text.Encoding.ASCII.GetString(incomingBuffer, 0, bytes);

Connection to server works well. But I can read only part of the answer from the server and undelivered part of the message is read at the next connection attempt.

I tried to set NetworkStream timeout:

// No change for me   
clientStream.ReadTimeout = 10000;

Then I tried to simulate timeout:

// This works well, the client has enough time to read the answers. But it's not the right solution.

// ....

NetworkStream clientStream = client.GetStream();
Thread.Sleep(TimeSpan.FromSeconds(1));

// Read stream ....
  • 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-29T21:42:03+00:00Added an answer on May 29, 2026 at 9:42 pm

    Data is transported over TCP in packets, which arrive serially (but not necessarily in the correct sequence). Immediately when data is available, i.e. when the logically (if not chronolgically) next packet is received, clientStream.Read() will return with the data in this (and maybe any other o-o-sequence) packet(s) – no matter if this is all data the sending side has sent or not.

    Your Thread.Sleep() makes the program wait for a second – in this time more than one packets arrive and are buffered on the system level, so the call to clientStream.Read() will return immediately with the available data.

    The correct way to handle this is to loop your Read() until BytesAvailable() becomes zero or a complete application-layer protocol element is detected.

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

Sidebar

Related Questions

I implemented a TCP Client using a thread opening a socket to a server
I just implemented a simple WCF server using net.tcp . First, I use 127.0.0.1
For a TCP client connect() call to a TCP server.. UNIX® Network Programming book
I've crafted a basic TCP client using EventMachine. Code: # run.rb EventMachine::run do EventMachine::connect
I'm in the designing stages of a custom tcp/ip protocol for mobile client-server communication.
I am using QTcpsocket and QtcpServer to implement client server resp. my client program
I have a server and a client application, here's how it's currently implemented: The
I have the following algorithm implemented in Java which uses TCP/IP: -Client request a
I have a network server that was implemented using Jboss Netty. It servers the
I need to implement a TCP client application. The client and the server send

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.