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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:30:40+00:00 2026-05-23T03:30:40+00:00

I am working on a networking program and designing a Linux server using C++.

  • 0

I am working on a networking program and designing a Linux server using C++. This is quite straightforward to design the basic structure. I have a packet definition with a header that has a fixed size.

typedef enum{
     PACKET_LOGIN_REQ = 1,
     PACKET_LOGIN_RES,
     PACKET_STORE_REQ,
     PACKET_STORE_RES
}PACKET_TYPES;

typedef struct {
     PACKET_TYPES type;
     short bodySize,
     long long deviceId
}HEADER;

.
.

/*more definitions here*/

typedef struct{
     HEADER head;
     union BODY{
          LOGIN_REQ loginReq;
          LOGIN_RES loginRes;
          .
          .
          more types
     }
}

Whenever I added more packet types I would have to modify the switch statement to add more cases to handle the newly added packets.

I am using the union type so I don’t have to change the entire packet structure. Instead I can add the newly added packet types into the union structure.

However, when I am trying to parse the raw data to put into the packet using a switch statement then I have to add each switch statement every time.

I think this is not a good design pattern and I was wondering how I can design the structure in a more flexible way.

Is there a better way to handle this (better design pattern)? What about related tutorials or references?

  • 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-23T03:30:41+00:00Added an answer on May 23, 2026 at 3:30 am

    You should use classes and instead of switch use a polymorphic call.

    Something like that (pseudo-codish):

    class Packet{
    public:
       Packet();
       virtual ~Packet();
       virtual HandleMe()=0;
    protected:
       // fields 
    }
    
    PacketTypeOne: public Packet{
    public:
      virtual HandleMe(); // implementation for type 1
    }
    
    PacketType2: public Packet{
    public:
      virtual HandleMe(); // implementation for type 2
    }
    
    PacketTypeX: public Packet{
    public:
      virtual HandleMe(); // implementation for type X
    }
    

    and where you get the packets:

    ...
    Packet* newPacket = PacketFactory::CreateNewPacket(packetTypeX); // factory will create
                  // the right instance.
    ...
    // now handle it:
    newPacket->HandleMe();
    ...
    

    Done.

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

Sidebar

Related Questions

I am working on a networking program using epoll on linux machine and I
Working with some basic java apps on CentOS 5 linux and I have my
I am working on a networking program using C++ and I'd like to implement
I'm working on a social networking system that will have comments coming from several
I have a networking program setup that does a lot of what the nestat
This simple issue I know, in my program session variable is already working properly
Recently I have started working on a program that will monitor the packets of
i made a server via boost networking and its working good but when i
I am working in networking reliability simulation, I need to simulate packet dropping based
Recently I have been working on a multiplayer networking app. I had successfully implemented

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.