Posting in General since it concerns both Network and System.
I'm running a blocking socket (tcp) in an sf::Thread. The program uses 1 percent CPU when I don't run the thread and 30 percent CPU when I do run it. VS Diagnostics show that the function that runs in the thread uses all that extra CPU (see the code for the function). Is this normal or am I doing this wrong?
while (1)
{
sf::Packet packet;
sf::Int32 code;
if (Socket.receive(packet) == sf::Socket::Done)
{
packet >> code;
PacketCodes.push_back(code);
switch (code)
{
// handle stuff
}
}
}
So is it truly blocking? Are you constantly receiving data?
OK, I remembered that the server I wrote sends the packets it receives to every other client.
for (unsigned int i = 0; i < Clients.size(); i++)
{
if (Selector.isReady(*Clients[i]))
{
sf::Packet packet;
if (Clients[i]->receive(packet) == sf::Socket::Done)
{
for (unsigned int j = 0; j < Clients.size(); j++)
{
if (i != j) { Clients[j]->send(packet); }
}
}
}
}
... and upon connecting another client to the server, both clients were using only 1 percent CPU as expected.
So, I guess they have to be constantly receiving data or sf::TcpSocket::receive goes nuts?
Sorry for being a noob, still very new to this network voodoo.