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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:16:28+00:00 2026-05-24T14:16:28+00:00

we have the exercise to program a webserver in object oriented style. So we

  • 0

we have the exercise to program a webserver in object oriented style.
So we created a class for WinSockets. We wanted to loop the main part (from accept till send) to handle connections one by one (just to start up; multithreading will be implemented later).

Problem: The first time, a connection got established everything is fine, but then, the server doesn’t wait for the next connection to accept. It says, it got a connection, but that descriptor throws an error, with the errornr “No Error”.

main:

NetInterface *socket;
#ifdef __unix__
    socket = new UnixSocket();
#elif __WIN32__ || _MSC_VER
    socket = new WinSocket();
#else 
    printf("Ihr System wird nicht Unterstützt");
#endif

socket->socketInit(PORT);

printf("server: waiting for connections...\n");
while(1) { // main accept() loop
    char *their_addr = socket->akzeptieren();
    if(their_addr == NULL)  {
        continue;
    }

    printf("server: got connection from %s\n", s);

    socket->empfangen();

    cout << socket->getInPacket() << endl;
}

WinSocket

class WinSocket : virtual public NetInterface
{
private:
    WSADATA wsaData;
    int iResult;

    SOCKET sockfd;
    SOCKET new_fd;

    struct addrinfo *servinfo;
    struct addrinfo hints;
    struct addrinfo *p;

    int iSendResult;
    string incoming;
    int recvbuflen;

    char s[INET6_ADDRSTRLEN];

    struct sockaddr_storage their_addr; // connector's address information
    socklen_t sin_size;

    int rv;

public:
    WinSocket();
    int socketInit(const char *port);
    char *akzeptieren();
    void empfangen();
    void senden(string s);
    string getInPacket();
    void *get_in_addr(struct sockaddr *sa);
};

[....]

char *WinSocket::akzeptieren(){
    sin_size = sizeof(their_addr);
    new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    if (new_fd == INVALID_SOCKET) {
        perror("accept");
        return NULL;
    }
    inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
    return s;       
}
  • 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-24T14:16:29+00:00Added an answer on May 24, 2026 at 2:16 pm

    I think you’re confused. You should normally have 2 sockets: 1 for accepting connections (you already have this one) and 1 for exchanging data (returned as new_fd from call to accept() in WinSocket:akzeptieren()).

    In most frameworks, a big distinction is made between listener sockets and stream sockets:

    // oversimplified interface.
    class Listener 
    {
    public:
        Listener ( const std::string& host, const unsigned short port );
        Stream * accept ();
    };
    
    // oversimplified interface.
    class Stream
    {
    public:
        const std::string peer () const;
        size_t send ( const void * data, size_t size );
        size_t recv (       void * data, size_t size );
    };
    

    So, your code should look like:

    const std::string host = "127.0.0.1";
    const unsigned short port = 1234;
    Listener listener(host, port);
    while ((stream = listener.accept())
    {
        std::cout
            << "Connection from '" << stream->peer() << "'."
            << std::endl;
        stream->send("Hello, world!", 13);
        delete stream; // close and shutdown.
    }
    

    Then, you can have:

    class WinListener;
    class WinStream;
    

    And make the whole thing multi-threaded.

    Note: this seems to be a requirement (assignment?), so I won’t suggest that you do otherwise. However, in a real production system, this is not a good server design. You may eventually want to read about the I/O completion port, epoll and kqueue subsystems for efficient asynchronous I/O.

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

Sidebar

Related Questions

people. For a academic exercise i have to implement a program in c for
I have a C program for an exercise and it has a strange issue
I'm writing a program for an exercise that will read data from a file
query level: beginner As part of a learning exercise I have written code that
I have been making a matrix class (as a learning exercise) and I have
I'm reading through Illustrated C and the first exercise question asks: Program MATMUL multiplies
I have a program that sorts input lines lexicographically, and I've come to this
I have a computer program that reads in an array of chars that operands
I have set up this programming exercise. using System; using System.Collections.Generic; using System.Linq; using
I am building my wife a Contacts Manager program as a fun exercise in

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.