Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How would it be possible to add support for many clients into my code. Also how can this be done in a way that is clear and actually make sense.
C++
class classServer
{
private:
	char *buffer;
	WSADATA theWSA;
	SOCKET theSocket,theOtherSocket;
public:
	classServer();
	void makeSocketLoad()
	{
		WSAStartup(MAKEWORD(2,2),&theWSA);
		theSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);		
	}	
	void sockConnect(int port)
	{
		struct sockaddr_in serverInf;
		serverInf.sin_family=AF_INET;
		serverInf.sin_addr.s_addr=INADDR_ANY;
		serverInf.sin_port=htons(port);
		if(bind(theSocket,(SOCKADDR*)&serverInf,sizeof(serverInf)) == SOCKET_ERROR)
		{
			std::cout<<"Unable to bind socket!\r\n";
			WSACleanup();
			system("PAUSE");
		}
	}
	void getDataStream()
	{
		listen(theSocket,1);

		SOCKET TempSock=SOCKET_ERROR;
		while(TempSock==SOCKET_ERROR)
		{
			std::cout<<"Waiting for incoming connections...\r\n";
			TempSock=accept(theSocket,NULL,NULL);
		}
		theSocket=TempSock;

		std::cout<<"Client connected!\r\n\r\n";
		char *buffer = new char[1002];
		std::string data;
		int inDataSize;
		while(true)
		{
			inDataSize = recv(theSocket,buffer,1000,0);
			data = buffer;
			if(inDataSize > 0)
			{
				std::cout << data << std::endl;
				if(data == "exit")
				{
					std::cout << "EXIT NOW" << std::endl;
					exit(0);
					break;
				}
			}	
		}
	}
	void killWinsock()
	{
		closesocket(theOtherSocket);	
		closesocket(theSocket);
		WSACleanup();
	}
};

classServer::classServer()
{
	buffer = new char[1002];
}


What I have tried:

Looking through large amounts of confusing code, to try to implement this.
Posted
Updated 23-Feb-16 6:01am
Comments
Richard MacCutchan 21-Feb-16 13:11pm    
You would need to start a new thread for each connection and process the communication to that client inside the new thread. Your main thread would then go back to listening for new connections.
Sergey Alexandrovich Kryukov 21-Feb-16 20:30pm    
That can work but...
Just two threads on server side would be quite enough. One for listening to incoming connection, another working with all connected sockets, say, in a round-robin way, or using some other scheduling rules. Added one or two thread for each connected remote client is a pretty well known architectural mistake. By the way, I once met a whole Web club of people who use this fact to draw a weird conclusion that threading is bad and pledge to not use threads and try to convince other people not to use them, which is, of course, utterly stupid.
—SA

1 solution

You've got a couple of options. You can either:

- Write a multi-threaded application and have one thread to listen for incoming connections and one or more to handle connected sockets. Multi-threaded code is really hard to write correctly (although it's a lot easier with C++11 and C++14) so don't be surprised when it bites you as you've missed some detail and it all goes to hell.

- Use select(). This function allows you to block, waiting for a socket in a group to be unblocked. it can be easier to use than a multi-threaded solution and potentially more efficient as well. However it requires you to organise your application around the call to select(), which isn't generally a problem but your naive class will need a bit of mangling to make it work.

A couple of pointers on your C++ style:

- Don't mess about with manual memory management. You don't need it and you'll end up with a far harder program to debug which runs slower to boot.

- Learn about destructors and common C++ idioms for resource management. Have a google for RAII and look at how it can improve your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900