SFML community forums

Help => Network => Topic started by: VenomOFSilence on September 09, 2022, 11:26:42 pm

Title: why when i use this code its runs very slow if i make it check set of ports?
Post by: VenomOFSilence on September 09, 2022, 11:26:42 pm
This is the code , if i make it check set ports by using for loop it runs very slow.
#include <iostream>
#include <SFML/Network.hpp>
#include <string>

static bool port_is_open(const std::string& address, int port)
{
    return (sf::TcpSocket().connect(address, port) == sf::Socket::Done);
}

int main1()
{
    if (port_is_open("127.0.0.1", 445))
        std::cout << "OPEN" << std::endl;
    else
        std::cout << "CLOSED" << std::endl;
    return 0;
}
Title: Re: why when i use this code its runs very slow if i make it check set of ports?
Post by: eXpl0it3r on September 20, 2022, 08:08:49 pm
Because it needs to establish a TCP connection with each endpoint and I guess has a default timeout time. So every failing connection waits for the timeout to pass.

If you want to write a port scanner, you're probably better off using existing solutions or using sockets directly, so you can adjust the behavior to your needs.