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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:42:51+00:00 2026-06-12T03:42:51+00:00

I’ve got a program and it’s listening on port 200. After a client is

  • 0

I’ve got a program and it’s listening on port 200. After a client is connected to it, all the received data from client is written to std::cout. If any error occures while getting data from client, I would like to restart hosting.

ServerSocket sock;
while(true)
{
    try
    {
        uint8 buff[1000];
        std::cout << "hosting..." << std::endl;
        sock.Host(port);

        while(true)
        {
            size_t ret = sock.GetData(buff, 1000);
            buff[ret] = '\0';
            std::cout << buff << std::endl;
        }
    }
    catch(const SocketException& se)
    {
        std::cout << se.What() << std::endl;
    }
}

The first time I connect to this everything is fine. If I close the client a SocketException occures (and therefore new hosting begins).

the host function:

    m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

m_address.sin_family = AF_INET;
m_address.sin_port = htons(port);
m_address.sin_addr.S_un.S_addr = INADDR_ANY;

int optval = 1;
setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, sizeof(int));

int retval = bind(m_socket, reinterpret_cast<sockaddr*>(&m_address), sizeof(m_address));

if(retval == SOCKET_ERROR)
    throw SocketException("bind() error", __FUNCTION__, WSAGetLastError());

retval = listen(m_socket, SOMAXCONN);

if(retval == SOCKET_ERROR)
    throw SocketException("listen() error", __FUNCTION__, WSAGetLastError());

m_socket = accept(m_socket, NULL, NULL);

if(m_socket == INVALID_SOCKET)
    throw SocketException("accept() error", __FUNCTION__, WSAGetLastError());

I need the setsockopt line becouse I’d like to bind instantly after an error (and the last hosting socket is in TIME_WAIT state?). Without setsockopt I’ve got bind() errors.
So I use setsockopt and it let’s me host on the port without problem. However when I start the client again, and try to connect to the host no error occures and I can send data again.
However the program does not accept any incoming connections (it’s still stuck at accept(m_socket, NULL, NULL); line waiting for incoming connection). I assume that the client can connect somehow to the old socket. Is that possible?

(note:
If I add WSACleanup();
WSAStartup(MAKEWORD(2, 2), &m_wsaData);
lines to the host function, then everything is working as intended. I have no idea why.
WSACleanup();WSAStartup(MAKEWORD(2, 2), &m_wsaData); helps me out only in my example program I made to investigate this issue. In the original program that 2 lines is not working.)

  • 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-06-12T03:42:53+00:00Added an answer on June 12, 2026 at 3:42 am

    You’re calling bind() and listen() for each connection. You should do bind() and listen() once when your server starts, and then call only accept() repeatedly.

    Also to do it properly you should spawn a new thread for each accept() call, otherwise people will be able to DOS your server by just opening a connection to it and then not sending anything.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

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.