I have a server and a client talking. My server handles each client with a thread. The server code looks like this:
Server::Server(int port)throw (const char*) {
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd<0) {
throw "socket failed";
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if ((::bind(fd, (struct sockaddr*) &server, sizeof(server))) < 0) {
throw "bind failed";
}
if(listen(fd,3)<0) {
throw "linsten failed";
}
}
void Server::start(ClientHandler& ch)throw(const char*){
t = new thread([&ch,this]() {
while (!stopAccept) {
cout<<"waiting for a client\n";
socklen_t clientSize = sizeof(client);
int aClient = ::accept(fd,(struct sockaddr *)&client, &clientSize);
if (aClient<0) {
throw "except failure";
}
cout<<"client connected\n";
ch.handle(aClient);
close(aClient);
cout<<"client closed\n";
}
close(fd);
});
}
void Server::stop(){
stopAccept = true;
t->join(); // do not delete this!
}
What happens is that after it finished dealing with 2 clients, I call the stop
function. But it right away prints "waiting for a client\n"
, before stopping.
Then, it of course will never stop, because it won't get any other message.
How can I end this program after the second client? A tip from my professor was to "implement a timeout on accept (for ex with alarm)". I have looked online a lot but can't seem to find a way to do this.
Advice?