1
Network / Nonblocking Tcp Socket Status is Never sf::Socket::Done
« on: August 19, 2021, 09:56:30 am »
I'm running into problems with nonblocking Tcp sockets where the client is unable to receive the sf::Socket::Done status despite that the server accepting the connection. After the server accepts the connection, the client's socket status becomes NotReady.
Server minimal example:
Server output:
Client minimal example:
Client output:
When I set the client's socket to blocking (server remains unblocking), then it proceeds as expected.
Server output
Client output
This is tested on Windows 10.
I'm using SFML version 2.5.1.
I will continue looking into this, and I'll post anything I identified in my investigation.
Server minimal example:
#include <chrono>
#include <iostream>
#include <SFML/Network.hpp>
#include <thread>
int main()
{
std::cout << "Starting server\n";
unsigned long updateRate = 30;
sf::TcpListener listen;
listen.setBlocking(false);
if (listen.listen(2011) != sf::Socket::Done)
{
std::cout << "Failed to listen at port 2011\n";
return -1;
}
std::cout << "Listening on port 2011\n";
sf::TcpSocket client;
client.setBlocking(false);
while (true)
{
if (listen.accept(client) == sf::Socket::Done)
{
break;
}
//simulate work
std::this_thread::sleep_for(std::chrono::milliseconds(updateRate));
}
std::cout << "Connected... waiting for packet\n";
sf::Packet packet;
while (true)
{
sf::Socket::Status status = client.receive(packet);
if (status == sf::Socket::Done)
{
int num;
packet >> num;
std::cout << "Received data. " << num << " \n";
break; //TODO: loop a couple times to check if receiving multiple data.
}
//Simulate work
std::this_thread::sleep_for(std::chrono::milliseconds(updateRate));
}
std::cout << "Disconnecting server.\n";
client.disconnect();
return 0;
}
#include <iostream>
#include <SFML/Network.hpp>
#include <thread>
int main()
{
std::cout << "Starting server\n";
unsigned long updateRate = 30;
sf::TcpListener listen;
listen.setBlocking(false);
if (listen.listen(2011) != sf::Socket::Done)
{
std::cout << "Failed to listen at port 2011\n";
return -1;
}
std::cout << "Listening on port 2011\n";
sf::TcpSocket client;
client.setBlocking(false);
while (true)
{
if (listen.accept(client) == sf::Socket::Done)
{
break;
}
//simulate work
std::this_thread::sleep_for(std::chrono::milliseconds(updateRate));
}
std::cout << "Connected... waiting for packet\n";
sf::Packet packet;
while (true)
{
sf::Socket::Status status = client.receive(packet);
if (status == sf::Socket::Done)
{
int num;
packet >> num;
std::cout << "Received data. " << num << " \n";
break; //TODO: loop a couple times to check if receiving multiple data.
}
//Simulate work
std::this_thread::sleep_for(std::chrono::milliseconds(updateRate));
}
std::cout << "Disconnecting server.\n";
client.disconnect();
return 0;
}
Server output:
Starting server
Listening on port 2011
Connected... waiting for packet
Listening on port 2011
Connected... waiting for packet
Client minimal example:
#include <chrono>
#include <iostream>
#include <SFML/Network.hpp>
#include <thread>
int main()
{
std::cout << "Starting client\n";
unsigned long updateRate = 30;
sf::TcpSocket client;
client.setBlocking(false);
while (true)
{
sf::Socket::Status status = client.connect(sf::IpAddress("127.0.0.1"), 2011);
if (status == sf::Socket::Done)
{
break;
}
//Simulate work
std::this_thread::sleep_for(std::chrono::milliseconds(updateRate));
}
std::cout << "Connected... sending packet\n";
sf::Packet packet;
while (true)
{
int num = 456;
packet << num;
sf::Socket::Status status = client.send(packet);
if (status == sf::Socket::Done)
{
std::cout << "Sent data. " << num << " \n";
break;
}
//Simulate work
std::this_thread::sleep_for(std::chrono::milliseconds(updateRate));
}
std::cout << "Disconnecting client.\n";
client.disconnect();
return 0;
}
#include <iostream>
#include <SFML/Network.hpp>
#include <thread>
int main()
{
std::cout << "Starting client\n";
unsigned long updateRate = 30;
sf::TcpSocket client;
client.setBlocking(false);
while (true)
{
sf::Socket::Status status = client.connect(sf::IpAddress("127.0.0.1"), 2011);
if (status == sf::Socket::Done)
{
break;
}
//Simulate work
std::this_thread::sleep_for(std::chrono::milliseconds(updateRate));
}
std::cout << "Connected... sending packet\n";
sf::Packet packet;
while (true)
{
int num = 456;
packet << num;
sf::Socket::Status status = client.send(packet);
if (status == sf::Socket::Done)
{
std::cout << "Sent data. " << num << " \n";
break;
}
//Simulate work
std::this_thread::sleep_for(std::chrono::milliseconds(updateRate));
}
std::cout << "Disconnecting client.\n";
client.disconnect();
return 0;
}
Client output:
Starting client
When I set the client's socket to blocking (server remains unblocking), then it proceeds as expected.
Server output
Starting server
Listening on port 2011
Connected... waiting for packet
Received data. 456
Disconnecting server.
Listening on port 2011
Connected... waiting for packet
Received data. 456
Disconnecting server.
Client output
Starting client
Connected... sending packet
Sent data. 456
Disconnecting client.
Connected... sending packet
Sent data. 456
Disconnecting client.
This is tested on Windows 10.
I'm using SFML version 2.5.1.
I will continue looking into this, and I'll post anything I identified in my investigation.