实施多客户端服务器的最佳方法是什么?
从基于TCP的环境中,我实现了下面的软件,
客户端连接时,服务器会创建两个线程。 (例如发送,recv)
发送线程在数据进入队列时发送数据,并等待是否为空。
RECV线程连续接收和处理客户发送的消息(例如,设置)。
目前,我针对的客户总数为4〜5。
这是我的示例代码,
////////////////////////
// Send Thread
while(m_thread_stop_send == false) { // thread stop flag (when it terminated)
if(m_sendQueue.empty()) // Queue is memeber variable
continue;
// Send data to client
// until the size of MSG is completely transmitted.
while(send size < message size) {
int ret = send(... );
send size += ret;
...
if(send size == message size) break;
}
// (depending on the MSG) if necessary,
// Heres recv routine only treat send's ack
while(recv size < message size) { // according to packet header
int ret = recv(... );
recv size += ret;
...
if(recv size == message size) break;
}
m_sendQueue.pop();
}
////////////////////////
// Recv Thread
while(m_thread_stop_recv == false) { // thread stop flag (when it terminated)
if(alive_flag > 5) // if no alive Msg while 25 sec,
close socket;
terminate recv thread
notify to termination to send thread
// recv client's message and parsing OP-Code
switch(OP-Code)
{
case A:
processing...
and send response to client
// response was sent directly, without send queue & thread
send(... response data ...);
case B:
processing...
and send response to client
case Alive: // 5 sec term
alive_flag = 0
}
}
所以我尝试使用这些代码,但是我不确定这是正确的方法...
您是否有任何好的示例或开源来处理发送和RECV的范围,对于每个客户端?
From TCP based environment, I implement my software like below,
Actually, it's my first time implementing a network, so I don't know if this is the right way.
When the client connects, the server creates two threads. (Such as send, recv)
Send thread sends data when data enters the queue, and waits if it is empty.
Recv thread continuously receives and processes messages (ex. settings) sent by the client.
Currently, the total number of clients I am targeting is 4~5.
This is my sample code,
////////////////////////
// Send Thread
while(m_thread_stop_send == false) { // thread stop flag (when it terminated)
if(m_sendQueue.empty()) // Queue is memeber variable
continue;
// Send data to client
// until the size of MSG is completely transmitted.
while(send size < message size) {
int ret = send(... );
send size += ret;
...
if(send size == message size) break;
}
// (depending on the MSG) if necessary,
// Heres recv routine only treat send's ack
while(recv size < message size) { // according to packet header
int ret = recv(... );
recv size += ret;
...
if(recv size == message size) break;
}
m_sendQueue.pop();
}
////////////////////////
// Recv Thread
while(m_thread_stop_recv == false) { // thread stop flag (when it terminated)
if(alive_flag > 5) // if no alive Msg while 25 sec,
close socket;
terminate recv thread
notify to termination to send thread
// recv client's message and parsing OP-Code
switch(OP-Code)
{
case A:
processing...
and send response to client
// response was sent directly, without send queue & thread
send(... response data ...);
case B:
processing...
and send response to client
case Alive: // 5 sec term
alive_flag = 0
}
}
So I try to these code, but I'm not sure this is correct way ...
Do you have any good examples or open sources of handling Send and Recv continuously, and for each client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论