Hi,
I came across something very puzzling while working with sockets. Check out the following example:
#include <iostream>
#include <SFML/Network.hpp>
int main()
{
sf::TcpListener l;
l.listen(2017);
sf::TcpSocket sock;
if (sock.connect(sf::IpAddress::LocalHost, 2017) == sf::Socket::Done)
std::cout << "Connected!\n";
if (sock.connect({1,2,3,4}, 2017) == sf::Socket::Done)
std::cout << "???\n";
char ch;
std::cin >> ch;
}
This gives me
Connected!
???
I would very much expect the second call to connect to fail. Can anyone explain what's going on? Is this a bug? I understand that further calls to connect disconnect the socket first, but the second connection should obviously fail.
Edit:Equally confusing behaviour, I would expect the second call to connect to succeed, yet I get no output:
sf::TcpListener l;
l.listen(2017);
sf::TcpSocket s;
if (s.connect({1,2,3,4}, 2017, sf::milliseconds(200)) == sf::Socket::Done)
std::cout << "???\n";
if (s.connect(sf::IpAddress::LocalHost, 2017) == sf::Socket::Done)
std::cout << "Connected\n";
char ch;
std::cin >> ch;
I'm compiling with mingw-w64 GCC 7.2.0 on Windows 10 with SFML from github master
I'm trying to loop through a range of IpAddresses e.g. 192.168.178.2 - 192.168.178.99 and attempt to connect with the same socket for each address. At first I thought I was making some mistake using multiple threads, but then I tried a small example in a single main().
Edit2:Calling disconnect seems to work for me to "handle" a failed connection, kind of like this:
sf::Uint8 b1{192}, b2{168}, b3{178};
socks.push_back(std::make_unique<sf::TcpSocket>());
for (sf::Uint8 b4 = 2; b4 < 100; ++b4) {
if (socks.back()->connect({b1,b2,b3,b4}, port, sf::milliseconds(100)) == sf::Socket::Done) {
chat.queue("Connected");
socks.push_back(std::make_unique<sf::TcpSocket>());
} else
socks.back()->disconnect();
}