shadedecho
Programmer
I've written a single-threaded (though not intentionally) socket server in C++ on a linux box. It binds to a specific port, listens indefinitely for incoming connections (like via telnet), and sends/receives some data on that connection, and then calls "close()" on that connection when a certain command is issued.
it's structured right now as an infinite outer loop (while(true)) that sets up an accept for a connection, and then an "infinite" inner loop which reads/writes data until that connection is closed.
It works fine in a single-threaded situation... meaning if i telnet to it, i get the interaction i expect. However, if i have one telnet session open, and then from another machine try to do another, the second one "waits" until I exit the first, hence I assume this is a single-threaded setup.
I don't need a socket library or anything else like that, obviously I have the socket part figured out fine. I just need someone to help me understand how to adjust this to work in a multi-threaded set up, so that it is able to handle any number of connections.
I think what i need to do is have a parent program which is basically just the outer loop, which acts as the daemon, and each time a new connection comes in, it forks a new process to handle just the connection (written as a separate program, i guess).
Can someone help me understand how to do this straightforwardly in c++ on a linux system?
it's structured right now as an infinite outer loop (while(true)) that sets up an accept for a connection, and then an "infinite" inner loop which reads/writes data until that connection is closed.
It works fine in a single-threaded situation... meaning if i telnet to it, i get the interaction i expect. However, if i have one telnet session open, and then from another machine try to do another, the second one "waits" until I exit the first, hence I assume this is a single-threaded setup.
I don't need a socket library or anything else like that, obviously I have the socket part figured out fine. I just need someone to help me understand how to adjust this to work in a multi-threaded set up, so that it is able to handle any number of connections.
I think what i need to do is have a parent program which is basically just the outer loop, which acts as the daemon, and each time a new connection comes in, it forks a new process to handle just the connection (written as a separate program, i guess).
Can someone help me understand how to do this straightforwardly in c++ on a linux system?