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

Author Topic: Failed to bind the socket to port 5200?  (Read 7363 times)

0 Members and 1 Guest are viewing this topic.

Zerxes

  • Newbie
  • *
  • Posts: 5
    • View Profile
Failed to bind the socket to port 5200?
« on: June 09, 2009, 09:48:36 pm »
I get this as console output:
Quote
Failed to bind the socket to port 5200
Failed to receive data ; the UDP socket first needs to be bound to a port


The Code:
Code: [Select]

        sf::IPAddress Address;
        unsigned short Port = 5200;
        m_ClientUDP.Bind( Port );
        while (1)
        {
            sf::Packet DataPacket;
            if (m_ClientUDP.Receive(DataPacket, Address, Port) != sf::Socket::Done)
            {
                return;
            }
            std::cout << "Got Data: " << DataPacket.GetDataSize() << " Bytes" << std::endl;

            sf::Sleep( m_Ping );
        }


My Firewall was off and the connecection was local (127.0.0.1).
Edit: I have the same Problems on other Ports (ex. 51000).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Failed to bind the socket to port 5200?
« Reply #1 on: June 09, 2009, 11:30:52 pm »
Please send a minimal and complete code so that I can test it directly.

Which version of SFML are you using? Which OS?
Laurent Gomila - SFML developer

Zerxes

  • Newbie
  • *
  • Posts: 5
    • View Profile
Failed to bind the socket to port 5200?
« Reply #2 on: June 09, 2009, 11:39:22 pm »
Quote from: "Laurent"
Please send a minimal and complete code so that I can test it directly.

Which version of SFML are you using? Which OS?

SFML-Version 1.5, dynamic Lib
OS: Vista 32-bit Home Premium
Compiler: MSVC08, IDE: Codeblocks

This program is for testing purposes only, here's the complete Code:
Code: [Select]
#include <iostream>
#include <SFML\Network.hpp>


struct s_InfoPlayer
{
    sf::Uint8 Address[16];
    sf::Uint8 Nick[20];
    void Clear() { memset( this, 0, sizeof(s_InfoPlayer) ); };
};

class CNetTest
{
private:
    sf::SocketTCP m_Listener;
    sf::SocketTCP m_ClientTCP;
    sf::SocketUDP m_ClientUDP;
    float m_Ping;
private:
    bool GetClientInfo()
    {
        size_t iCount = 0;
        sf::Uint32 iBuild = 0;
        s_InfoPlayer PlayerInfo;

        if (m_ClientTCP.Receive( (char*)&iBuild, sizeof(sf::Uint32), iCount) != sf::Socket::Done) { return 0; }
        if (m_ClientTCP.Receive( (char*)&PlayerInfo, sizeof(s_InfoPlayer), iCount) != sf::Socket::Done) { return 0; }

        std::cout << "Client Build " << iBuild << std::endl;
        std::cout << "Added Client " << PlayerInfo.Nick << std::endl;
        return 1;
    };

    bool SendClientInfo()
    {
        std::string sNick("NetTest");
        std::string sAddress("127.0.0.1");
        s_InfoPlayer PlayerInfo; PlayerInfo.Clear();

        sNick.copy( (char*)&PlayerInfo.Nick[0], sizeof(PlayerInfo.Nick) );
        sAddress.copy( (char*)&PlayerInfo.Address[0], sizeof(PlayerInfo.Address) );
        m_ClientTCP.Send( (char*)&PlayerInfo, sizeof(s_InfoPlayer) );

        std::cout << "Sent Client-Info" << std::endl;
        return 1;
    };
public:
     CNetTest()
    {
        m_Ping = 0.1f;

        std::cout << "Listening..." << std::endl;
        m_Listener.Listen( 5100 );
        m_Listener.Accept(m_ClientTCP, 0);
        std::cout << "Host connected!" << std::endl;
    };

    void Receive()
    {
        if ( !GetClientInfo() ) { return; };
        if ( !SendClientInfo() ) { return; };

        bool bValid = 0;
        int ErrorVal = 0;
        sf::IPAddress Address;
        unsigned short Port = 5200;

        bValid = m_ClientUDP.IsValid();
        m_ClientUDP.Unbind();
        bValid = m_ClientUDP.Bind( Port );
        ErrorVal = WSAGetLastError();
        std::cout << "WSAGetLastError: " << ErrorVal << std::endl;
        while (1)
        {
            sf::Packet DataPacket;
            if (m_ClientUDP.Receive(DataPacket, Address, Port) != sf::Socket::Done)
            {
                return;
            }
            std::cout << "Got Data: " << DataPacket.GetDataSize() << " Bytes" << std::endl;

            sf::Sleep( m_Ping );
        }
    };

    ~CNetTest() { m_Listener.Close(); m_ClientTCP.Close(); m_ClientUDP.Close(); };
};


int main()
{
    CNetTest NetTest;
    NetTest.Receive();

    std::cout << std::endl << "Host disconnected..." << std::endl;
    system("pause");
    return 0;
};


And the UDP-Part:
Code: [Select]
   void Receive()
    {
        if ( !GetClientInfo() ) { return; };
        if ( !SendClientInfo() ) { return; };

        bool bValid = 0;
        int ErrorVal = 0;
        sf::IPAddress Address;
        unsigned short Port = 5200;

        bValid = m_ClientUDP.IsValid();
        m_ClientUDP.Unbind();
        bValid = m_ClientUDP.Bind( Port );
        ErrorVal = WSAGetLastError();
        std::cout << "WSAGetLastError: " << ErrorVal << std::endl;
        while (1)
        {
            sf::Packet DataPacket;
            if (m_ClientUDP.Receive(DataPacket, Address, Port) != sf::Socket::Done)
            {
                return;
            }
            std::cout << "Got Data: " << DataPacket.GetDataSize() << " Bytes" << std::endl;

            sf::Sleep( m_Ping );
        }
    };


After the bind I get this WinSock-Error: WSAENOTSOCK 10038

Zerxes

  • Newbie
  • *
  • Posts: 5
    • View Profile
Failed to bind the socket to port 5200?
« Reply #3 on: June 10, 2009, 01:38:41 am »
Ich have the solution...

This works:
Code: [Select]
   void Receive()
    {
        if ( !GetClientInfo() ) { return; };
        if ( !SendClientInfo() ) { return; };

        sf::SocketUDP m_ClientUDP;
        bool bValid = 0;
        int ErrorVal = 0;
        sf::IPAddress Address;
        unsigned short Port = 5200;

        bValid = m_ClientUDP.IsValid();
        m_ClientUDP.Unbind();
        bValid = m_ClientUDP.Bind( Port );
        ErrorVal = WSAGetLastError();
        std::cout << "WSAGetLastError: " << ErrorVal << std::endl;
        while (1)
        {
            sf::Packet DataPacket;
            if (m_ClientUDP.Receive(DataPacket, Address, Port) != sf::Socket::Done)
            {
                return;
            }
            std::cout << "Got Data: " << DataPacket.GetDataSize() << " Bytes" << std::endl;

            sf::Sleep( m_Ping );
        }
    };


Declaring the Socket as local Variable works - I think the Stack gets corrupted before CNetTest::Receive() is called.

 

anything