C ++ winsock多客户端 [英] C++ winsock multi client

查看:88
本文介绍了C ++ winsock多客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的代码中添加对许多客户端的支持。另外,如何以一种清晰且实际有意义的方式完成这项工作。

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.

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.

推荐答案

你有几个选择。你可以:



- 编写一个多线程应用程序,让一个线程监听传入的连接,一个或多个线程来处理连接的套接字。多线程代码真的很难正确编写(尽管使用C ++ 11和C ++ 14会更容易)所以当它因为你错过了一些细节而咬你时不要感到惊讶它们都会变成地狱。



- 使用 select()。此功能允许您阻止,等待组中的套接字被解除阻塞。它比多线程解决方案更容易使用,并且可能更高效。但是它需要你围绕 select()的调用来组织你的应用程序,这通常不是问题,但你的天真类需要一些修改才能使它工作。



关于C ++风格的几点建议:



- 不要乱用手册内存管理。你不需要它,你最终会得到一个更难以调试的程序,它运行速度较慢。



- 了解析构函数和常见的C ++习语资源管理。有一个谷歌的RAII,看看它如何改善您的代码。
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.


这篇关于C ++ winsock多客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆