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

The Archive Base Latest Questions

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

I made a socket (Winsock2) in Visual Studio Pro C++ to listen on a

  • 0

I made a socket (Winsock2) in Visual Studio Pro C++ to listen on a port for connections (TCP). It works perfectly, but I let it run in its own thread, and I want to be able to shut it down with the hopes of restarting it later. I can terminate the thread without problems, but doing so does not stop the socket from accepting new clients (that is, it lingers on the accepts it was doing before I closed the thread). I can connect new clients to it but nothing happens… it just accepts and that’s it. What I want is to stop it from listening and accepting and then be able to tell it to start up again later on the same port. Attempting to restart it now just tells me the port is already taken.

Here is the listen thread function:

DWORD WINAPI ListeningThread(void* parameter){
TCPServer *server = (TCPServer*)parameter;

try{
    server = new TCPServer(listen_port);
}catch(char* err){
    cout<<"ERROR: "<<err<<endl;
    return -1;
}

int result = server->start_listening();
if(result < 0){
    cout<<"ERROR: WSA Err # "<<WSAGetLastError()<<endl;
    return result;
}
cout<<"LISTENING: "<<result<<endl<<endl;
while(true){
    TCPClientProtocol *cl= new TCPClientProtocol(server->waitAndAccept());
    HANDLE clientThread = CreateThread(0, 0, AcceptThread, cl, 0, 0);
    cout<<"Connection spawned."<<endl;
}

return 0;
}

Here are the relevant functions in TCPServer:

TCPServer::TCPServer(int port){
listening = false;
is_bound = false;

//setup WSA
int result = WSAStartup(MAKEWORD(2, 2), (LPWSADATA) &wsaData);
if(result < 0){
    throw "WSAStartup ERROR.";
    return;
}

//create the socket
result = (serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP));
if(result < 0){
    throw "Socket Connect ERROR.";
    return;
}

//bind socket to address/port
SOCKADDR_IN sin;
sin.sin_family = PF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = INADDR_ANY;

result = bind(serverSocket, (LPSOCKADDR) &sin, sizeof(sin));
if(result < 0){
    throw "Could not Bind socket - Make sure your selected PORT is available.";
    return;
}

is_bound = true;
}

int TCPServer::start_listening(){
int result = -1;
if(is_bound){
    //SOMAXCONN parameter (max) is a backlog:
    //  how many connections can be queued at any time.
    result = listen(serverSocket, SOMAXCONN);
    if(result >= 0)
        listening = true;
}
return result;
}

SOCKET TCPServer::waitAndAccept(){
if(listening)
    return accept(serverSocket, NULL, NULL);
else
    return NULL;
}

I have tried both closesocket() and shutdown() but both of those threw errors.

Thank you all for your time and help!

  • 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-24T09:29:00+00:00Added an answer on May 24, 2026 at 9:29 am

    First, make sure you set SO_REUSEADDR option on the server socket to be able to re-start listening.

    Then, I’m guessing, your problem is that accept() blocks and you cant stop it when you need to, so you are killing the thread. Bad solution. The right answer here is asynchronous I/O, i.e. select() or poll() or their Windows counterparts. Take a look at Advanced Winsock Samples.

    A quick-and-dirty solution in a multithreaded app is to check some is_it_time_to_stop_accepting_connections flag before each accept(), then when it’s time to stop, flip the flag and connect to the listening port (yes, in the same program). That will unblock the accept() and allow you to do proper closesocket() or whatever.

    But seriously, read up on asynchronous I/O.

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

Sidebar

Related Questions

I made a custom server that accepts TCP connections on a certain port. I
I have made a java socket listener which listens on port 80. And what
I have a very simple Winsock2 TCP client - full listing below - which
So, I made a simple socket server using python. And now I'm trying to
I made a comet chat server with Erlang and Mochiweb. And I run the
I have a socket app that passes data between 2 processes. It works fine
I am writing on a small tcp chat server, but I am encountering some
i made a server via boost networking and its working good but when i
I have made a TCP server which I have been testing locally and it
I'm using TCP socket connetion between a server program and a client program. Multiple

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.