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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:17:02+00:00 2026-05-16T16:17:02+00:00

I am writing on a small tcp chat server, but I am encountering some

  • 0

I am writing on a small tcp chat server, but I am encountering some problems I can´t figure out how to solve “elegantly”.

Below is the code for my main loop: it does:
1.Initiates a vector with the basic event, which is flagged, when a new tcp connection is made.
2. gets this connection and pushes it back into a vector, too. Then with the socket it creates a CSingleConnection object and passes the socket into it.
2.1. gets the event from the CSingleConnection, which is flagged when the connection receives data…
3. when it receives data. the wait is fullfilled and returns the number of the handle in the array… with all those other vectors it seems i can determine which one is sending now…

but as everybody can see: this methodology is really poorly… I cant figure out how to do all this better, with getting the connection socket, creating a single connection and so on :/…

Any suggestions, improvements, etc?…

void CServer::MainLoop()
{
    DWORD dwResult = 0;
    bool bMainLoop = true;
    std::vector<std::string> vecData;
    std::vector<HANDLE> vecEvents;              //Contains the handles to wait on
    std::vector<SOCKET> vecSocks;               //contains the sockets
    enum
    {
        ACCEPTOR = 0,           //First element: sequence is mandatory

        EVENTSIZE                   //Keep as the last element!
    };

    //initiate the vector with the basic handles
    vecEvents.clear();
    GetBasicEvents(vecEvents);

    while(bMainLoop)
    {
        //wait for event handle(s)
        dwResult = WaitForMultipleObjects(vecEvents.size(), &vecEvents[0], true, INFINITE);

        //New connection(s) made
        if(dwResult == (int)ACCEPTOR)
        {
            //Get the sockets for the new connections
            m_pAcceptor->GetOutData(vecSocks);

            //Create new connections
            for(unsigned int i = 0; i < vecSocks.size(); i++)
            {
                //Add a new connection
                CClientConnection Conn(vecSocks[i]);
                m_vecConnections.push_back(Conn);
                //Add event
                vecEvents.push_back(Conn.GetOutEvent());
            }
        }

        //Data from one of the connections
        if(dwResult >= (int)EVENTSIZE)
        {
            Inc::MSG Msg;
            //get received string data
            m_vecConnections[dwResult].GetOutData(vecData);

            //handle the data
            for(unsigned int i = 0; i < vecData.size(); i++)
            {
                //convert data into message
                if(Inc::StringToMessage(vecData[i], Msg) != Inc::SOK)
                    continue;
                //Add the socket to the sender information
                Msg.Sender.sock = vecSocks[dwResult];
                //Evaluate and delegate data and task
                EvaluateMessage(Msg);
            }
        }
    }
}
  • 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-16T16:17:03+00:00Added an answer on May 16, 2026 at 4:17 pm

    Do not re-invent the wheel, use Boost.ASIO. It is well optimized utilizing kernel specific features of different operating systems, designed the way which makes client code architecture simple. There are a lot of examples and documentation, so you just cannot get it wrong.

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

Sidebar

Related Questions

I'm writing a small client/server application in c++ with winsock and I can't explain
I'm writing a small web server in Python, using BaseHTTPServer and a custom subclass
I'm writing a small application in VB.NET and I would like some of the
I am writing a small glue tool for doing some testing and I would
I'm writing a very small and simple server that I want to listen on
I'm writing a small (C#) client application that sends data using a TCP/IP connection
when writing small functions I often have the case that some parameters are given
I am writing a small Java server, and a matching client in C++, which
I have a small TCP server that listens on a port. While debugging it's
Writing a small HTML web page with some very simple Javascript in it, 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.