从C scoket客户机/服务器滞后 [英] C sockets client/server lag

查看:129
本文介绍了从C scoket客户机/服务器滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编程的C / C ++客户机/服务器应用程序的插座。在这一点上,客户端itselfs连接到每50ms服务器和发送消息。

I'm programming a C/C++ client/server sockets application. At this point, the client connects itselfs to the server every 50ms and sends a message.

似乎一切的作品,但数据流量是不连续的:突然之间,服务器不接收任何东西,然后5的消息在一次......有时候一切正常......

Everything seems to works, but the data flow is not continuous: Suddenly, the server doesn't receives anything more, and then 5 messages at once... And sometimes everything works...

有人曾经这样奇怪的行为起源的想法?

Has someone an idea of the origin of this strange behaviour ?

在code的某些部分:

Some part of the code:

客户:

while (true)
{
if (SDL_GetTicks()-time>=50)
{
socket = new socket();
socket->write("blah");
message.clear();
message = socket->read();
socket->close();
delete socket;
time=SDL_GetTicks();
}
}

服务器:

while (true) {
fd_set readfs;
struct timeval timeout={0,0};
FD_ZERO(&readfs);
FD_SET(sock, &readfs);
select(sock + 1, &readfs, NULL, NULL, &timeout)
if(FD_ISSET(sock, &readfs))
{
SOCKADDR_IN csin;
socklen_t crecsize = sizeof csin;
SOCKET csock = accept(sock, (SOCKADDR *) &csin, &crecsize);
sock_err = send(csock, buffer, 32, 0);
closesocket(csock);
}
}

编辑:
1。我试着做

Edits: 1. I tried to do

int flag = 1;
setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof flag);

在客户端和服务器,但问题依然存在。

In both client and server, but the problem is still there.

2.Yes这些连接/ deconnections非常inneficient,但是当我尝试写

2.Yes those connections/deconnections are very inneficient, but when I try to write

socket = new socket();
while (true)
{
if (SDL_GetTicks()-time>=50)
{
socket->write("blah");
message.clear();
message = socket->read();
time=SDL_GetTicks();
}
}

则消息将只发送一次(或接收)...

最后:

我忘了适用于TCP_NODELAY在服务器端的客户端套接字。现在,它完美的作品!
我把线程的流程,使插座保持开放。
谢谢大家:)

I had forgotten to apply TCP_NODELAY to the client socket on the server side. Now it works perfectly ! I put the processes in threads so that the sockets keep open. Thank you all :)

推荐答案

这就是所谓的纳格延迟 。该算法在等待TCP堆栈更多的数据实际发送任何网络,直到某些超时到期之前到达。所以,你应该修改的Nagle超时( http://fourier.su/index.php?topic=249.0 )或禁用所有的Nagle延迟(<一个href=\"http://www.unixguide.net/network/socketfaq/2.16.shtml\">http://www.unixguide.net/network/socketfaq/2.16.shtml),这样的数据将每发送发送通话。

This is what called "Nagle delay". This algorithm is waiting on TCP stack for more data to arrive before actually sending anything to network untill some timeout expires. So you should modify the Nagle timeout (http://fourier.su/index.php?topic=249.0) or disable Nagle delay at all (http://www.unixguide.net/network/socketfaq/2.16.shtml), so data will be sent per send call.

这篇关于从C scoket客户机/服务器滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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