user3806521
user3806521

Reputation: 53

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. The spikes tend to last 5 seconds then it runs smooth again. 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);

Upvotes: 0

Views: 208

Answers (1)

user207421
user207421

Reputation: 311018

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.

However, it's not a real-time protocol. You must expect some lags here and there.

Upvotes: 1

Related Questions