简体   繁体   English

SFML TCP套接字发送导致滞后尖峰

[英]SFML TCP Socket Sending causes lag spikes

This gets called every frame at 30 FPS (as it should be), but every 10 seconds or so a massive lagspike occurs. 以30 FPS(应为每秒)的速度调用每一帧,但是每10秒钟左右就会发生一次巨大的延迟。 The spikes tend to last 5 seconds then it runs smooth again. 尖峰往往持续5秒钟,然后再次平稳运行。 What am I doing wrong? 我究竟做错了什么?

socketsSent++;
sf::TcpSocket socket;
socket.connect(ip, atoi(serverInfo[5].c_str()));
std::string data;
data = "X:" + std::to_string((int)objects["player"].getPosition().x) + ":Y:" + std::to_string((int)objects["player"].getPosition().y);
socket.send(data.c_str(), data.length() + 1);

Keep the connection open across these calls. 在这些呼叫之间保持连接打开。 A TCP connect requires three packets to be exchanged, and a disconnect requires four, so you have a packet overhead of up to 7 times if you connect and disconnect each time. TCP连接需要交换三个数据包,而断开连接则需要四个数据包,因此,如果每次连接和断开连接,您的数据包开销最多为7倍。

However, it's not a real-time protocol. 但是,它不是实时协议。 You must expect some lags here and there. 您必须在这里和那里都有些滞后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM