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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:00:28+00:00 2026-05-29T05:00:28+00:00

I am in the process of writing a TCP server using Berkely SOCKET API

  • 0

I am in the process of writing a TCP server using Berkely SOCKET API under linux. Now the clients have a set of specifications and each message send by the client is based on those specifications i-e the message sent from the client corresponds to one of the many structs specified by the specs. Now the scenario is that the server doesn’t know which message the client is going to send at what time. Once we get the message we can figure out what it is but not before that. The messages sent by the clients have variable lengths so we can not know in advance what message we are going to get. To solve this I have used the following method:

const char *buf[4096] = { 0 };
          if ( recv (connected, buf, 4096, 0) == -1)
             {
               printf ("Error in recvng message\n");
               exit (-1);
             }

That is I use a default buffer of size 4096 (no message from the client can be larger than this size). receive in that buffer and then afterwards I Check the message type and take the corresponding action as follows:

struct ofp_header *oph;
oph=(struct ofp_header *)buf;
switch (oph->type)
{
case example_pkt:
handle_example_pkt();
break;
}

This works fine but I just wanted to confirm that is it an appropriate method or is there something else that could be better than this. All help much appreciated.

Thanks.

  • 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-29T05:00:31+00:00Added an answer on May 29, 2026 at 5:00 am

    TCP is stream-based. This means if you use a buffer larger than your message you may receive part of the next message as well.

    This means you will need to know the size of the message and incorporate any additional data into the next message. There are two obvious ways to do this:

    1. Modify the protocol to send the size of each message as the first few bytes of the message. Read the size first, then only read that many bytes.

    2. Since you know the size of each message, keep track of how many bytes you read. Process the first message in the buffer then subtract the size of that message from the remaining bytes in the buffer. Continue to repeat this process until you either A. Don’t have enough bytes left to identify the message type or B. Don’t have enough bytes for a message of the detected type. Save any remaining bytes and call recv again to read more data.

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

Sidebar

Related Questions

I am writing a Codec to process messages sent over TCP using a bespoke
Currently in the process of writing some TCP socket code and running into a
Consider a server process being contacted by multiple client processes using UNIX socket as
I have implemented an asynchronous TCP server that is spawned by another process. It
I'm in the process of writing a Java 2D game. I'm using the built-in
I'm trying to implement TCP hole punching with windows socket using mingw toolchain. I
I am in the process of writing some test for a server that is
I'm currently writing a multi-process network game server (one gatekeeper process which tells players
I'm writing a TCP server that functions very much like a chatroom and came
I have a small TCP server that listens on a port. While debugging it's

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.