C ++多线程服务器 [英] C++ multithreaded server

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

问题描述

感谢您的快速响应.
我这次将尝试澄清有关多线程服务器的所有信息.
我们被要求实现一个单线程或多线程的TCP服务器,但多线程会获得更多的信用",该TCP服务器必须连接到由教授指定为.exe文件的客户端,我无权访问该代码",服务器应响应客户端的请求,这些请求包括echo,reverse echo,时间戳,同步和错误.
只需提及我们正在使用三个库"winsock.dll,WS2_32和WSOCK32"

thanks for these quick responds .
I will try this time to clarify everything regarding the multithreaded server.
we were asked to implement a TCP server single or multithreaded "but the multithreaded will gain more credits",this TCP server has to connect to a client which is given as .exe file by the professor"i have no access for the code", the server should respond to the client requests which are echo ,reverse echo ,time stamp , synchronise and error.
just to mention that we are using three libraries "winsock.dll , WS2_32 AND WSOCK32"

if (connSock  = listener->Accept(client_addr)){
            ThreadData *data = new ThreadData ( this,
                                                connSock,
                                                client_addr );
            DWORD pth1 = _beginthreadex( NULL,
                                         0,
                                         ThreadedTCPServer::thread,
                                         data,
                                         0,
                                         &thID );
        }
        else
            break;


这是我启动新线程的部分,它将由新的连接器套接字而不是同一个列表器套接字处理...
所以现在,如果我尝试检查其是否正常工作,我只是添加了cou<< connsock;
但是对于连接到服务器的每个客户端来说都是相同的.
我现在要问的是如何检查我的代码是否以多线程运行?以及它是否是多线程的,那么我是否必须使用Roundrobin,Semaphore或Mutex?我应该使用它们,因为就我而言可以告诉我不需要调度CPU,因为每个客户端都连接到服务器并将请求发送到服务器,并立即收到响应并断开连接.如果您需要更多详细信息,请随时询问任何问题.


this is the part were I start the new thread and it will be handled by new connector socket not the same listner socket ...
so now if i try to check if its working properly i just added cou<<connsock;
but it was the same for each client that connects to the server .
all I am asking now is "how can I check if my code running as multithreaded or not ?? and if its multithreaded then do I have to use Roundrobin , Semaphore or Mutex ??and for what shall I use them because as far as i can tell i dont need to schedule the CPU because the each client connect to the server and send request to the server and recieve response and disconnect right away. if you need more details please dont hesitate to ask any questions.

推荐答案

从多线程服务器可以同时处理来自客户端的多个请求的角度来看,它应该比单线程服务器更好. 处理相同数量的客户".

尝试在套接字接收事件的处理程序中添加一些合理的计算.单线程服务器将陷入困境,而多线程服务器将继续提供服务.

您的朋友可能自己并没有在线程上创建和运行套接字,而是正在使用为他执行此操作的库.

更重要的是,在您的这篇文章中,甚至不清楚您需要什么帮助.
A multi-threaded server is expected to be superior to a single threaded one in the sense that it can handle multiple requests from clients simultaneously. I don''t see how can those two programs ''handle the same amount of clients''.

Try adding some reasonably heavy calculation in the handler of socket receive event. The single-threaded server will be bogged down, while the multi-threaded one will continue to serve.

Your friend is probably not creating and running sockets on threads himself, but is using a library which does that for him.

More importantly, with this post of yours, it is not even clear what you want help with.


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

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