Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Error if port isn't open - Sockets  (Read 3866 times)

0 Members and 1 Guest are viewing this topic.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Error if port isn't open - Sockets
« on: January 10, 2015, 12:10:42 am »
Hello

I was wondering, I'm working with sockets now and connecting etc just works fine.
But the problem is, if I'm connecting to an IP and a closing port, the socket just keeps connecting and trying and doing nothing.

When I use an open port it works without problems.

Is it possible to make it like that
if the port isn't open => Throw an error back instantly.

Thanks for reading,
Niely

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Error if port isn't open - Sockets
« Reply #1 on: January 10, 2015, 12:31:54 am »
The only way to tell if "the port it open" is, if the connection fails, but since you most likely just keep trying to connect if the connection fails...

If you want further help, then as usual provide all the necessary information, including a complete and minimal example code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Error if port isn't open - Sockets
« Reply #2 on: January 10, 2015, 12:41:52 am »
#include <iostream>

#include <SFML/Network.hpp>

using namespace std;
int main() {
sf::TcpSocket socket;
socket.connect("142536789.cf", 21);
char buffer[1024];
size_t received = 0;
socket.receive(buffer, sizeof(buffer), received);
cout << "Info: " << buffer <<endl;

if (socket.isOk()) {
 cout<<"Ok"<<endl;
} else {
 cout<<"Error"<<endl;
}
}
   

Just a really basic code.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Error if port isn't open - Sockets
« Reply #3 on: January 10, 2015, 12:54:49 am »
This is just the way TCP works, and SFML doesn't have any magic way around it.

When you want to connect to another system using TCP, it starts off with a SYN message, kind of like a "Hello, is anyone there?". Just like in real life, when you knock at someone's door, you don't know how long it takes them to answer you, or even if they are at home. Any sane person would give up waiting after several minutes if they have absolutely no clue whether the person is home or not. Conversely, knocking and assuming nobody is home after waiting for 2 seconds is also not the right thing to do.

For this reason, sf::TcpSocket::connect() takes a third parameter - the timeout value. This basically tells SFML how long you are willing to wait for the guy at the other end to answer your "Hello" message. Obviously, because we are talking about computer systems, you specify the value in milliseconds or microseconds typically. If you don't want to unnecessarily wait, you can set the amount of time you are willing to wait for, and when that time is exceeded, SFML will simply return from the connect() call unsuccessfully.

Now... if you are thinking of setting the timeout value to something really low, like... 0 or 1 millisecond, keep in mind that 0 is reserved to indicate "wait forever", and if you choose a value that is too small, you won't give the message enough time to travel through the network and the reply to travel back. If the average round-trip-time between you and the server is 50ms, then only waiting for 40ms before giving up will never allow you to connect. As such, you should choose this value carefully.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Error if port isn't open - Sockets
« Reply #4 on: January 10, 2015, 01:08:08 pm »
^Thanks a lot!
Parameter 3 is actually exactly what I want, but how exactly do I check this?

#include <iostream>

#include <SFML/Network.hpp>

using namespace std;
int main() {
sf::TcpSocket socket;
socket.connect("142536789.cf", 456, sf::seconds(5));
if (socket != sf::Socket::Done) {
cout<<"error"<<endl;
} else {
char buffer[1024];
size_t received = 0;
socket.receive(buffer, sizeof(buffer), received);
cout << "- " << buffer <<endl;
}
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error if port isn't open - Sockets
« Reply #5 on: January 10, 2015, 02:26:00 pm »
Quote
how exactly do I check this?
Please read the documentation and tutorials.
Laurent Gomila - SFML developer

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Error if port isn't open - Sockets
« Reply #6 on: January 10, 2015, 02:33:40 pm »
But that's the way showed in the documentation (Socket::Done).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error if port isn't open - Sockets
« Reply #7 on: January 10, 2015, 06:40:07 pm »
No. Read it more carefully ;)
Laurent Gomila - SFML developer

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Error if port isn't open - Sockets
« Reply #8 on: January 10, 2015, 11:38:28 pm »
Indeed, you're right!  ;D

Thanks a lot all! You all were a great help!

Working code:

#include <iostream>

#include <SFML/Network.hpp>

using namespace std;
int main() {
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("142536789.cf", 456, sf::seconds(5));
if (status != sf::Socket::Done) {
cout<<"error"<<endl;
} else {
char buffer[1024];
size_t received = 0;
socket.receive(buffer, sizeof(buffer), received);
cout << "- " << buffer <<endl;
}
}
 

Application is almost ready now, only needed to have the basic understatement.