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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:52:46+00:00 2026-05-29T17:52:46+00:00

Okay, I actually don’t have code as of yet because i’m just picking out

  • 0

Okay, I actually don’t have code as of yet because i’m just picking out a framework for the time being, but i’m still a little baffled about how i wish to go about this :.

Server side, i wish to have a class where each instance has a socket and various information identifying each connection. each object will have it’s own thread for receiving data. I understand how i’ll be implementing most of that, but my confusion starts just as i get to the actual transfer of data between server and client. I’ll want to have a bunch of different message structs for specific cases, (for example CONNECT_MSG , DISCONNECT_MSG, POSTTEXT_MSG, etc) and then all i have to do is have a char * point at that struct and then pass it via the send() function.

But as i think on it, it gets a little complicated at that point. Any of those different message types could be sent, and on the receiving end, you will have no idea what you should cast the incoming buffer as. What i was hoping to do is, in the thread of each connection object, have it block until it receives a packet with a message, then dump it into a single queue object managed by the server(mutexes will prevent greediness) and then the server will process each message in FIFO order independent of the connection objects.

I havn’t written anything yet, but let me write a little something to illustrate my setup.

#define CONNECT 1000

struct GENERIC_MESSAGE
{
    int id;
}

struct CONNECT_MESSAGE : public GENERIC_MESSAGE
{
    m_username;
}

void Connection::Thread()
{

    while(1)
    {
         char buffer[MAX_BUFFER_SIZE];    // some constant(probably 2048)
         recv(m_socket, buffer, MAX_BUFFER_SIZE, 0);
         MESSAGE_GENERIC * msg = reinterpret_cast<MESSAGE_GENERIC *> (buffer);
         server->queueMessage(msg);
    }

}

void Server::QueueMessage(MESSAGE_GENERIC * msg)
{
    messageQueue.push(msg);
}

void Server::Thread()
{
    while(1)
    {
         if(!messageQueue.empty())
              ProcessMessages();
         else
              Sleep(1);
    }
}

void Server::ProcessMessages()
{
     for(int i = 0; i < messageQueue.size(); i++)
     {
          switch(messageQueue.front()->id)
          {
                case CONNECT:
                {
                     // the part i REALLY don't like
                     CONNECT_MESSAGE * msg = static_cast<CONNECT_MESSAGE *>(messageQueue.front() );
                     // do the rest of the processing on connect
                     break;
                }
                // other cases for the other message types
          }
          messageQueue.pop();
     }
}

Now if you’ve been following up until now, you realize just how STUPID and fragile this is. it casts to the base class, passes that pointer to a queue, and then just assumes that the pointer is still valid from the other thread, and even then whether or not the remaining buffer after the pointer for the rest of the derived class will always be valid afterward for casting, but i have yet to find a correct way of doing this. I am wide open for ANY suggestions, either making this work, or an entirely different messaging design.

  • 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-29T17:52:52+00:00Added an answer on May 29, 2026 at 5:52 pm

    Before you write even a line of code, design the protocol that will be used on the wired. Decide what a message will consist of at the byte level. Decide who sends first, whether messages are acknowledged, how receivers identify message boundaries, and so on. Decide how the connection will be kept active (if it will be), which side will close first, and so on. Then write the code around the specification.

    Do not tightly associate how you store things in memory with how you send things on the wire. These are two very different things with two very different sets of requirements.

    Of course, feel free to adjust the protocol specification as you write the code.

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

Sidebar

Related Questions

Okay, Now admittedly this sounds like a silly question; But, I actually have a
Okay, I think I'm just making a stupid mistake here, but I want to
Okay, I have done a bit of searching online and found this thread, but
Okay so i have a semi weridish problem with re.sub. Take the following code:
Okay, so the answer is probably obvious, but I don't know the correct way
Okay, this just feels plain nasty, but I've been directed to do it, and
Okay i know i have to use the JDBC etc, but im not sure
I don't know that the following piece of code is really relevant, but for
okay so this is probably a soft pitch question for sombody, but I want
Okay, before you guys go nuts -- this is just a small site, temporary

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.