-
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
-
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.
-
#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.
-
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.
-
^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;
}
}
-
how exactly do I check this?
Please read the documentation and tutorials.
-
But that's the way showed in the documentation (Socket::Done).
-
No. Read it more carefully ;)
-
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.