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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:28:49+00:00 2026-05-13T15:28:49+00:00

I am trying to write a Server application wrapper, as I would with any

  • 0

I am trying to write a Server application wrapper, as I would with any application and I’ve searched for over a week for a at least decent guide or tutorial on asynchronous sockets( this wrapper has to be asynchronous ) and so far what i could do is this:

#ifndef _SERVER_H
#define _SERVER_H

#include "asynserv.h" // header file with the important lib includes
#include <map>

namespace Connections
{
    DWORD WINAPI MainThread(LPVOID lParam); // Main thread
    DWORD WINAPI DataThread(LPVOID lParam); // thread that will be created for each client
    struct ClientServer // struct to keep a server and a client pair.
    {
    public:
        struct Client* Client;
        class Server* Server;
    };
    struct Client // a struct wrapper to keep clients
    {
    public:
        Client(SOCKET Connection, int BufferSize, UINT ID);
        ~Client();
        SOCKET WorkerSocket;
        char Buffer[255];
        bool Connected;
        int RecvSize;
        UINT UID;
        void Send(char * Data);
        void Disconnect();
    };
    class Server
    {
    private:
        SOCKET WorkerSocket;
        SOCKADDR_IN EndPnt;
        UINT ID;
        int CBufferSize;
    public:
        Server(int Port, int Backlog, int BufferSize);
        ~Server();
        __event void ClientRecieved(Client* Clientr, char * RecData);
        bool Enabled;
        int Port;
        int Backlog;
        HWND ReqhWnd;
        std::map<UINT, Client*> ClientPool;
        void WaitForConnections(Server*);
        void WaitForData(Client*);
        void InvokeClientDC(UINT);
        void Startup();
        void Shutdown();
    };
}
#endif

Server.cpp:

#include "Server.h"

namespace Connections
{
    void Server::Startup()
    {
        WSADATA wsa;
        WSAStartup(0x0202, &wsa);
        this->WorkerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        this->EndPnt.sin_addr.s_addr = ADDR_ANY;
        this->EndPnt.sin_family = AF_INET;
        this->EndPnt.sin_port = htons(this->Port);
        this->Enabled = true;
        this->ID = 0;
        bind(this->WorkerSocket, (SOCKADDR*)&this->EndPnt, sizeof(this->EndPnt));
        printf("[AsynServer]Bound..\n");
        listen(this->WorkerSocket, this->Backlog);
                CreateThread(NULL, NULL, &MainThread, this, NULL, NULL);
    }
    void Server::WaitForConnections(Server * Ser)
    {
        WSAEVENT Handler = WSA_INVALID_EVENT;
        while(Ser->Enabled)
        {
            Handler = WSACreateEvent();
            WSAEventSelect(Ser->WorkerSocket, Handler, FD_ACCEPT);
            WaitForSingleObject(Handler, INFINITE);
            SOCKET accptsock = accept(Ser->WorkerSocket, NULL, NULL);
            Client * NewClient = new Client(accptsock, 255, Ser->ID++);
            NewClient->Connected = true;
            printf("[AsynServer]Client connected.\n");
            ClientServer * OurStruct = new ClientServer();
            OurStruct->Server = Ser;
            OurStruct->Client = NewClient;
            CreateThread(NULL, NULL, &DataThread, OurStruct, NULL, NULL);
        }
    }
    void Server::WaitForData(Client * RClient)
    {
        WSAEVENT Tem = WSA_INVALID_EVENT;
        Tem = WSACreateEvent();
        WSAEventSelect(RClient->WorkerSocket, Tem, FD_READ);
        while(RClient->Connected)
        {
            WaitForSingleObject(Tem, INFINITE);
            RClient->RecvSize = recv(RClient->WorkerSocket, RClient->Buffer, 255, NULL);
            if(RClient->RecvSize > 0)
            {
                RClient->Buffer[RClient->RecvSize] = '\0';
                __raise this->ClientRecieved(RClient, RClient->Buffer);
                Sleep(50);
            }
        }
        return;
    }
    DWORD WINAPI MainThread(LPVOID lParam)
    {
        ((Server*)lParam)->WaitForConnections((Server*)lParam);
        return 0;
    }
    DWORD WINAPI DataThread(LPVOID lParam)
    {
        ClientServer * Sta = ((ClientServer*)lParam);
        Sta->Server->WaitForData(Sta->Client);
        return 0;
    }
}

Now after creating the server instance and creating the main thread, i can accept clients simultaneously and read data they send, but after TWO connections my CPU lags up till 100% usage..
I guess my method is incorrect, so my question is can someone point out a possible flaw in my code, or just point me out a decent guide for asynchronous sockets, provided that i have already searched for over a week with no results( probably my despair is hindering me from choosing the correct keywords 😐 ).
Thanks in advance and sorry about the huge piece of code, trimmed it as long as it allowed.

Mfg,
SimpleButPerfect.

  • 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-13T15:28:49+00:00Added an answer on May 13, 2026 at 3:28 pm

    1st bug:
    In Server::WaitForConnections you create the HEVENT (Handler = WSACreateEvent() ) every time the while loop executes without destroying it. Move the HEVENT creation outside the while loop.

    2nd bug:
    Your buffer is 255 long (char) but you do this:
    RClient->Buffer[RClient->RecvSize] = ‘\0’;
    where RClient->RecvSize can be the exaclty of the size of your buffer – that means you make a classis “buffer overrun”.

    I hope this helps.
    Dominik

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

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As the user will use the virtual machine to run… May 14, 2026 at 7:05 pm
  • Editorial Team
    Editorial Team added an answer It means the call stack as reported by Ruby's caller()… May 14, 2026 at 7:05 pm
  • Editorial Team
    Editorial Team added an answer WebLogic doesn't provide an embedded API so, even if it's… May 14, 2026 at 7:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.