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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:24:59+00:00 2026-06-11T17:24:59+00:00

I am using the server-client model for communicating with a hardware board using socket

  • 0

I am using the server-client model for communicating with a hardware board using socket programing.

I receive data from board using “read()” method of “NetworkStream” class which reads a buffer with specified maximum size and returns the length of valid data in buffer. I have considered the maximum size of buffer with a enough big number.

The board sends a set of messages every 100ms. Each message consists a 2-byte constant header and a variable number of bytes as its data after the header bytes.

The problem is that I do not receive the messages one by one! Instead, I receive a buffer may contains 2 or 3 messages or one message is scattered between two buffer.

Currently, I am using a DFA which gather the content of messages using the constant header bytes (We do not know the length of messages, we just know the header bytes) but the problem is that the data bytes may contains the header bytes randomly !!

Is there any efficient way to gather the bytes of each message from buffers using any specific stream or class? How can I overcome to this 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-06-11T17:25:01+00:00Added an answer on June 11, 2026 at 5:25 pm

    You need to add an additional buffer component between your consumer DFA and the socket client.

    Whenever data is avaliable from the NetworkStream the buffer component will read it and append it to its own private buffer, incrementing an “available bytes” counter. The buffer component needs to expose at least the following functionality to its users:

    1. a BytesAvailable property — this returns the value of the counter
    2. a PeekBytes(int count) method — this returns the first count bytes of the buffer, if that much is available at least, and does not modify the counter or the buffer
    3. a ReadBytes(int count) method — as above, but it decrements the counter by count and removes the bytes read from the buffer so that subsequent PeekBytes calls will never read them again

    Keep in mind that you don’t need to be able to service an arbitrarily high count parameter; it is enough if you can service a count as long as the longest message it would be possible to receive at all times.

    Obviously the buffer component needs to keep some kind of data structure that allows “wrapping around” of some kind; you might want to look into a circular (ring) buffer implementation, or you can just use two fixed buffers of size N where N is the length of the longest message and switch from one to the other as they become full. You should be careful so that you stop pulling in data from the NetworkStream if your buffers become full and only continue pulling after the DFA has called ReadBytes to free up some buffer space.

    Whenever your DFA needs to read data, it will first ask your buffer stage how much data it has accumulated and then proceed accordingly. It would look something like this:

    if BytesAvailable < 2
        return; // no header to read, cannot do anything
    
    // peek at the header -- do not remove it from the buffer!
    header = PeekBytes(2);
    
    // calculate the full message length based on the header
    // if this is not possible from just the header, you might want to do this
    // iteratively, or you might want to change the header so that it is possible
    length = X; 
    
    if BytesAvailable < X
        return; // no full message to read, cannot continue
    
    header = ReadBytes(2); // to remove it from the buffer
    message = ReadBytes(X); // remove the whole message as well
    

    This way your DFA will only ever deal with whole messages.

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

Sidebar

Related Questions

I am trying to implement a Client/Server model using TCpClient , with its Networkstream.Write()/Read()
I am using the zeromq client server model. My client takes options from the
This is my first linux socket program. I am using the server client model.
i developed client server program using c++,so i want to receive more than 500kb
My application is a client/server solution using an ADO.NET Data Service. The Data Service
I am developing a client-server application using .Net Remoting. From my server I want
I am implementing a really basic server-client model in Java, by using UDP sockets
I am using pyhton's multiprocessing package in a standard client-server model. I have a
I am building a client/server model but using sockets, using named pipes, with mkfifo().
I'm looking to create a rich-featured application using a client/server model, using WCF as

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.