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

Author Topic: Failed to bind socket to port 2000  (Read 5468 times)

0 Members and 2 Guests are viewing this topic.

reethok

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Failed to bind socket to port 2000
« 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."
« Last Edit: January 23, 2013, 10:06:39 am by reethok »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Failed to bind socket to port 2000
« Reply #1 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.
« Last Edit: January 23, 2013, 01:38:44 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

reethok

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Failed to bind socket to port 2000
« Reply #2 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Failed to bind socket to port 2000
« Reply #3 on: January 24, 2013, 02:50:23 pm »
Read this and then please post again. I have no idea what you changed after my last post (or even if you attempted it).
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Failed to bind socket to port 2000
« Reply #4 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 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. 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. Although maybe one could argue that using std::string might be better, reethok has other things to take care of for now.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).