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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:29:00+00:00 2026-05-20T13:29:00+00:00

I have a client-server program. I’m sending data like this: private void Sender(string s,TcpClient

  • 0

I have a client-server program.

I’m sending data like this:

private void Sender(string s,TcpClient sock)
{
   try
   {
      byte[] buffer = Encoding.UTF8.GetBytes(s);
      sock.Client.Send(buffer);
   }catch{}
}

and on the client side receiving like this:

byte[] buffer = new byte[PacketSize];
int size = client.Client.Receive(buffer);
String request = Encoding.UTF8.GetString(buffer, 0, size);

The problem is that data is not fully received always, sometimes it’s only part of what I have sent. PacketSize is 10240 which is more than the bytes I send. I have also set SendBufferSize and ReceiveBufferSize at both sides.

The worst part is that sometimes data is fully received!

What might be the problem?

  • 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-20T13:29:01+00:00Added an answer on May 20, 2026 at 1:29 pm

    The size value returned by TcpClient.Receive is not the same as the length of the buffered string you sent. This is because there is no guarantee that when calling Receive once you will get back all the data that you sent with Send call. This behavior is intrinsic to the way TCP works (it’s a stream-, not a message-based data protocol).

    You cannot solve the problem by using bigger buffers, as the buffers you provide can only limit the amount of data that Receive returns. Even if you provide a 1MB buffer and there is 1MB of data to read, Receive can legitimately return any number of bytes (even just 1).

    What you need to do is make sure that you have buffered all the data before calling Encoding.GetString. To do that, you need to know how much data there is in the first place. So at the very least, you need to write the length of the string bytes when sending:

      byte[] buffer = Encoding.UTF8.GetBytes(s);
      byte[] length = BitConverter.GetBytes(buffer.Length);
      sock.Client.Send(length);
      sock.Client.Send(buffer);
    

    When receiving, you will first read the length (which has a known fixed size: 4 bytes) and then start buffering the rest of the data in a temp buffer until you have length bytes (this might take any number of Receive calls, so you ‘ll need a while loop). Only then can you call Encoding.GetString and get your original string back.

    Explanation of the behavior you observe:

    Even though the network stack of the OS makes pretty much no guarantees, in practice it will usually give you the data one TCP packet brings with one Receive call. Since the MTU (maximum packet size) for TCP allows around 1500 bytes for payload, naive code will work fine as long as the strings are less than this size. More than that and it will get split into multiple packets, and one Receive will then return only part of the data.

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

Sidebar

Related Questions

I have a client/server program in TCP written in C, and I would like
I have a Client program by tcp connection that send data to a server.
I have a Client-Server program in C#. Here is the Server's code: ... String
i have application with client-server architecture. client (C program): generate various DER encoded data
Exposition: I'm familiar with C/C++/Scheme & OpenGL. I have this client/server program written in
Hi I am writing a simple client-server program. In this program I have to
I have an application using sockets.. a client and server program like mySql.. I
Writing a client / server program and previously was using calls like this: TcpListener.AcceptTcpClient()
So this is my question. I have a client and a server java program.
all. I'm having a bit of weird problem with client server program. I have

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.