SFML community forums

Help => Network => Topic started by: reethok on January 23, 2013, 10:01:05 am

Title: Failed to bind socket to port 2000
Post by: reethok on January 23, 2013, 10:01:05 am
Well, i have this problem. Hope somebody can help me.

Sender:

#include <SFML/Network.hpp>
#include <iostream>

int main()
{
    sf::IPAddress Address("XXX.XXX.XXX.XX"); // My public IP
    std::string ip = Address.ToString();
    sf::SocketTCP *sock = new sf::SocketTCP;
    sf::SocketTCP Listener;

    Listener.Accept(*sock, &Address);
    char toSend[] = "Hola, quĂ© tal";
    Listener.Listen(2000);
    sock->Send(toSend, sizeof(toSend));

    return 0;
}
 

Receiver:

#include <iostream>
#include <SFML/Network.hpp>
int main()
{
    sf::IPAddress ip(sf::IPAddress::GetPublicAddress());
    std::cout << "Mi IP es " << ip.ToString() << "." << std::endl;
    sf::SocketTCP socket;
    sf::SocketTCP socketEntrada;
   std::size_t asdf;

    socketEntrada.Connect(2000,"XXX.XXX.XXX.XX"); // My public IP


    char recibidor[] = "asdasdasdasdfafgufbrfsifhbvrufbwufbwuvwufebwfnwjfvwuebfwhlbfvewf";

    if (!socket.Listen(2000)) {
        std::cout << "No se puede escuchar el puerto 2000." << std::endl;
        return 1;
    }

    while (!socket.Accept(socketEntrada, NULL));
    std::cout << "Se a conectado al puerto 2000." << std::endl;
    socketEntrada.Receive(recibidor, sizeof(recibidor), asdf);

    std::cout << recibidor << std::endl;
r    eturn 0;
}
 

I run first the reciever, then in another terminal the sender, and the sender oututs this:

"Failed to bind socket to port 2000".

Why is this happening?

Thank you, greetings.

EDIT:
I changed the ort to 1984, and now the sender runs, but nothing happens, it just ends in 1 milisecond (?), and the reciever just stay like "nikolov@rettask:~/Escritorio$ ./B
Mi IP es 201.141.136.35."
Title: Re: Failed to bind socket to port 2000
Post by: zsbzsb on January 23, 2013, 01:35:49 pm
For starters, I think you really should go research some more on sockets and networking. Trying to connect to your own public IP from inside usually doesn't work too well. Try using a local IP address like 127.1.1.1 (or 'localhost') or another computer on your network (aka 192.168.xxx.xxx).

By the way, I'm not a C++ guru (I prefer .NET) but
 char recibidor[] = "asdasdasdasdfafgufbrfsifhbvrufbwufbwuvwufebwfnwjfvwuebfwhlbfvewf";
doesn't look like the way you should declare a char array.
Title: Re: Failed to bind socket to port 2000
Post by: reethok on January 24, 2013, 11:41:09 am
I've try it whit a friend and it doesnt work either.

That I'm doing wrong? :/

Greetings.
Title: Re: Failed to bind socket to port 2000
Post by: zsbzsb on January 24, 2013, 02:50:23 pm
Read this (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) and then please post again. I have no idea what you changed after my last post (or even if you attempted it).
Title: Re: Failed to bind socket to port 2000
Post by: binary1248 on January 24, 2013, 08:35:50 pm
You need to read more about how TCP connections work. If you had read the documentation page on sf::TcpListener (http://sfml-dev.org/documentation/2.0/classsf_1_1TcpListener.php#details) you would notice that you listen on a given port and accept and incoming connection request after that. In your code you somehow accept on the socket and then listen? That can't work. You also don't need to specify an address to listen on since SFML doesn't support this feature anyway and using your public address is probably not what you want either since that is not the address of one of the interfaces on your computer but instead the address of an interface of your router/gateway.

On your receiver you don't need to listen on a port. TCP connections are bidirectional, you can use them to send and receive data. Once the sender accepts the connection request, the TCP connection will be in the established state in which you can send and receive data on the same connection at will. Calling .Listen() and .Accept() on the client is never correct.

Just read the documentation on TcpSocket (http://sfml-dev.org/documentation/2.0/classsf_1_1TcpSocket.php#details). It has enough information for you to figure it out on your own.

By the way, I'm not a C++ guru (I prefer .NET) but
 char recibidor[] = "asdasdasdasdfafgufbrfsifhbvrufbwufbwuvwufebwfnwjfvwuebfwhlbfvewf";
doesn't look like the way you should declare a char array.
This is perfectly valid C++. See this (http://www.cplusplus.com/doc/tutorial/ntcs/). Although maybe one could argue that using std::string might be better, reethok has other things to take care of for now.