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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:42:36+00:00 2026-06-17T03:42:36+00:00

I send the serialized data over LAN network but sometimes the information is lost!

  • 0

I send the serialized data over LAN network but sometimes the information is lost! Process is the following:

Sender:

  1. string mydata is being serialized
  2. string mydata is converted to byte[] bytes_of_mydata
  3. int size_of_mydata is the length of byte[] bytes_of_mydata
  4. int size_of_mydata itself is turned into byte[] bytes_size_of_mydata
  5. byte[] bytes_of_mydata and byte[] bytes_size_of_mydata are sent

Receiver:

  1. i first receive byte[] bytes_size_of_mydata
  2. retrieve length int size_of_mydata of the second message from byte[] bytes_size_of_mydata
  3. i then receive byte[] bytes_of_mydata, knowing exact length!
  4. i then convert byte[] bytes_of_mydata to string mydata
  5. deserialize string mydata

This approach usually works in most situations, but sometimes my data is not transmitted fully, so the string can’t be deserialized.

I’ve debugged the received byte[] on the "receiver" and here is what happens:

I get the size of second message:

int size_of_second_message = BitConverter.ToInt32(dataByteSize, 0); // 55185

I start to receive the second message to byte array:

Byte[] dataByte = new Byte[55185];

But starting from position 5840 I start to receive 0 (nulls), so the part "5840 – 55185" are all "0":

byte[5836] = 53;
byte[5837] = 57;
byte[5838] = 54;
byte[5839] = 49;
byte[5840] = 0; // information ends to flow
byte[5841] = 0;
byte[5842] = 0;
byte[5843] = 0;
//....
byte[55185] = 0;

The example from the above is taken from an actual debugger!

So what’s the problem? It’s like the connection is being lost during transmission!! Why is it happening and how do I counter this problem? It doesn’t happen on "every-time" basis.

And here comes the code

Send:

//text_message - my original message
//Nw - network stream
MemoryStream Fs = new MemoryStream(ASCIIEncoding.Default.GetBytes(text_message));
Byte[] buffer = Fs.ToArray(); // total 55185 bytes (as in example)
Byte[] bufferSize = BitConverter.GetBytes(Fs.Length); // 32 bytes represent size
bufferSize = GetNewByteSize(bufferSize);

Nw.Write(bufferSize, 0, bufferSize.Length); // send size
Nw.Flush();

Nw.Write(buffer, 0, buffer.Length); // send message
Nw.Flush();

Receive:

//get first(SIZE) bytes:
int ReadSize = 0; int maxSize = 32; // 32 - constant!
Byte[] dataByteSize = new Byte[maxSize];
int origsize;
using (var strm = new MemoryStream())
{
    ReadSize = Nw.Read(dataByteSize, 0, maxSize);
    strm.Write(dataByteSize, 0, ReadSize);
    strm.Seek(0, SeekOrigin.Begin);
    origsize = BitConverter.ToInt32(dataByteSize, 0); // origsize = 55185
}
Nw.Flush();

//get next(MESSAGE) bytes:
string message = ""; int thisRead = 0;
int max = Convert.ToInt32(origsize); // origsize = 55185
Byte[] dataByte = new Byte[max];

using (var strm = new MemoryStream())
{
    thisRead = Nw.Read(dataByte, 0, max);
    strm.Write(dataByte, 0, thisRead);
    strm.Seek(0, SeekOrigin.Begin);
    using (StreamReader reader = new StreamReader(strm))
    {
        message = reader.ReadToEnd();
    }
}
Nw.Flush();
// message - the message that is being transmitted partly (sometimes)! 

I didn’t want to post the code but you guys usually ask "show us what you’ve done", so here it is!

Edit

Temporary fix is to switch to StreamWriter, reader.

Receive + send (server):

NetworkStream Nw = new NetworkStream(handlerSocket.Client);
string toreceive = "";
StreamReader reader = new StreamReader(Nw);
toreceive = reader.ReadLine();
string text_message = "to send back";
StreamWriter writer = new StreamWriter(Nw);
writer.WriteLine(text_message);
writer.Flush();
Nw.Close();

Send + receive (client):

NetworkStream Nw = new NetworkStream(handlerSocket.Client);
StreamWriter writer = new StreamWriter(Nw);
writer.WriteLine("to send");
writer.Flush();

string toreceive = new StreamReader(Nw).ReadLine();
writer.Close();
Nw.Close();

I’m looking for a solution regarding original problem, but so far everything is working due to temporary fix.

  • 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-17T03:42:37+00:00Added an answer on June 17, 2026 at 3:42 am

    TCP is a stream based protocol which lets you read as much data has yet been received. Just because you send data in one step, there is no guarantee that the data will be received in the same block. You have to loop on the receiving side until you have received all the data you anticipate.

    By the way I think that your protocol with an explicit length field at the start is really good, it makes up for simple client code (as simple as it gets at least).

    Some time back I asked a question on built in functionality to wait until X bytes of data is available: .NET blocking socket read until X bytes are available?. The answer is unfortunately no.

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

Sidebar

Related Questions

I've seen lots of examples of sending serialized data over sockets in Java, but
I am trying to send my serialized sort data to my PHP script, but
I'm writing some code to serialize some data to send it over the network.
I am trying to send serialized,compressed data over a tcp connection using protobuf-net and
I am using JQUERY .ajax() to send serialized form data to MySQL using PHP.
I send 2 bytes of app data on the socket(blocking) every 10 seconds, but
I'm using TCPClient to send message (List of arrays) over LAN, so I have
How can I secure the serialized object if I send the serialized object over
I want to send a a Uint16 over the network. I've had a look
I'm writing some C++ code that will have to send data over TCP/IP. 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.