I made a socket (Winsock2) in Visual Studio Pro C ++ to listen on the port for connections (TCP). It works fine, but I let it work in its thread, and I want to be able to close it with the hope of restarting it later. I can end the stream without problems, but this does not prevent the socket from accepting new clients (that is, it lingers on the receptions that it did before I closed the stream). I can connect new clients to it, but nothing happens ... it just accepts and does it. I want him not to listen and not accept, and then he could say that he will start again later on the same port. Trying to restart it now just tells me that the port is already done.
Here is the stream listen 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 related 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 tried both closesocket () and shutdown (), but both of them threw errors.
Thank you all for your time and help!
Sefu
source share