How can i make this multithreaded?
Hi,
I am creating a windows socket application whereby client can connect to the server and the server responds with a series of messages. This is a test application, and i have got it working, but only one client can connect. I want to enable multiple connections. I need to change the code here:-
Code:
#ifndef __TCPSERVER_H
#define __TCPSERVER_H
#include "Sockets.h"
#include <string>
using namespace std;
class TCPServer {
protected:
ListeningSocket *listener;
int backlog;
sockaddr_in server_addr;
size_t localPort;
public:
TCPServer(size_t port, int backLog = 1);
virtual ~TCPServer();
string receive(TCPSocket *connSocket, int len);
bool send(TCPSocket *connSocket, string sent_str);
void processLoop();
virtual bool run(TCPSocket *connSocket,
sockaddr_in *client_addr) = 0;
};
#endif //__TCPSERVER_H
Is there a way i can do this? I think i need to check if the thread is running first and then enable multiple connections but dont really know how to do it.
Hope someone can help..
If you needs the Sockets.h file i will provide this.
Regards
Billy
Re: How can i make this multithreaded?
you don't really need to make it multithreaded, although you could create a worker thread for each client that connects (waste of time and resources though)
Usually you listen on a port, on an incoming connection, accept, and shunt it to a different port, then create another socket listening on your server port. You could have a (worker) thread that just listens for connections and then creates client sockets for your main thread to issue data to.
Re: How can i make this multithreaded?
well i was thinking i would need to make it multi-threaded to enable multiple connections?
Re: How can i make this multithreaded?
http://www.madwizard.org/programming/tutorials/netcpp/
Here you will find a very good tutorial about sockets.