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

Author Topic: Port problems  (Read 22924 times)

0 Members and 1 Guest are viewing this topic.

Srejv

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Port problems
« on: January 09, 2008, 11:31:52 pm »
Code: [Select]
server.cpp: In function 'int main()':
server.cpp:25: error: no matching function for call to 'sf::SocketUDP::Receive(char [128], unsigned int, size_t&, sf::IPAddress&, short unsigned int&)'
/usr/include/SFML/Network/SocketUDP.hpp:107: note: candidates are: sf::Socket::Status sf::SocketUDP::Receive(char*, size_t, size_t&, sf::IPAddress&)
/usr/include/SFML/Network/SocketUDP.hpp:131: note:                 sf::Socket::Status sf::SocketUDP::Receive(sf::Packet&, sf::IPAddress&)


And when I remove the port parameter it says "please specify port". I'm really confused.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Port problems
« Reply #1 on: January 10, 2008, 01:29:28 am »
The port is now specified with the Bind function, that you have to call before Receive.
Laurent Gomila - SFML developer

Srejv

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Port problems
« Reply #2 on: January 10, 2008, 04:03:36 pm »
Ahaa! Thanks. :D

I was also wondering, is there any way to make recieveing a packet an event? Or maybe I should put the networking in a new thread? :)

Srejv

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Port problems
« Reply #3 on: January 10, 2008, 05:08:44 pm »
Okay so I tried to create some small app to send and one to recieve small messages, but I'm not sure the client sends the message.  :?

server.cpp
Code: [Select]
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>

#include <iostream>
       
sf::Mutex GlobalMutex;
bool ThreadRunning = true;
           

void ServerPoll(void* UserData)
{      
    GlobalMutex.Lock();
    std::cout << "Starting up Server." << std::endl;
    GlobalMutex.Unlock();
    sf::SocketUDP Server;
    Server.Bind(6739);
    sf::IPAddress ip(127,0,0,1);
   
    GlobalMutex.Lock();
    std::cout << "Finished setting up for Thread." << std::endl;
    GlobalMutex.Unlock();

    while(ThreadRunning)
    {
        char Message[128];
        std::size_t Received;
        GlobalMutex.Lock();
        std::cout << "Waiting for message." << std::endl;
        GlobalMutex.Unlock();
        if(!Server.Receive(Message, sizeof(Message), Received, ip));
            return;

        GlobalMutex.Lock();
        std::cout << "Message recieved." << std::endl;
        GlobalMutex.Unlock();


        GlobalMutex.Lock();
        std::cout << Message << std::endl;
        GlobalMutex.Unlock();
    }
    GlobalMutex.Lock();
    std::cout << "Finished thread." << std::endl;
    GlobalMutex.Unlock();

    GlobalMutex.Lock();
    std::cout << "Closing server." << std::endl;
    GlobalMutex.Unlock();
    Server.Close();
    GlobalMutex.Lock();
    std::cout << "Closed server." << std::endl;
    GlobalMutex.Unlock();
}

int main()
{
    sf::RenderWindow App(sf::VideoMode(800,600), "Target lul");

    sf::Thread Thread(&ServerPoll);

    bool Running = true;
    bool ThreadRunning = true;

    Thread.Launch();
    while(Running)
    {
        sf::Event Event;
        if(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape)
            {
                Running = false;
                ThreadRunning = false;
            }
        }

        App.Display();

    }
    return 0;
}


client.cpp
Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>

int main()
{
    sf::IPAddress target(127,0,0,1);
    unsigned short port = 6739;

    sf::SocketUDP Client;
    Client.Bind(port);
    char Buffer[] = "HEYAHO :D:DDd.Dd:D";

    std::cout << "Sending message: " << Buffer << std::endl;

    if (!Client.Send(Buffer, sizeof(Buffer), target, port))
    {
        std::cout << "Msg fail" << std::endl;
        return 0;
    }

    Client.Close();
    std::cout << "Finished." << std::endl;

    return EXIT_SUCCESS;
}


Any ideas? The output I get from client.cpp is "Msg fail", so I guess I might have some parameters wrong.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Port problems
« Reply #4 on: January 11, 2008, 02:06:47 am »
The socket mustn't be bound to send, only to receive.
Laurent Gomila - SFML developer

Srejv

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Port problems
« Reply #5 on: January 11, 2008, 06:42:19 pm »
Okay, but does that matter? :|

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Port problems
« Reply #6 on: January 12, 2008, 03:02:06 am »
Yes, as you can't bind two sockets on the same port (the second will fail).
Laurent Gomila - SFML developer

Srejv

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Port problems
« Reply #7 on: January 12, 2008, 02:13:11 pm »
Okay, I removed the Bind from client.cpp, but it still doesn't work. :/

Edit:

So I changed the recieve and send ifstatements to:

Code: [Select]

if(Server.Receive(Message, sizeof(Message), Received, ip) != sf::Socket::Done)
  return;


And
Code: [Select]

if(Client.Send(Buffer, sizeof(Buffer), target, port) != sf::Socket::Done)
{
  std::cout << "Msg fail" << std::endl;
  return 0;
}


And it works. :)

CyanPrime

  • Newbie
  • *
  • Posts: 18
    • View Profile
Port problems
« Reply #8 on: May 10, 2011, 12:15:29 pm »
I'm having the same problem. Why isn't this compiling?

Code: [Select]



#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(640, 480, 32), "blah");
    sf::IPAddress addy("127.0.0.1");
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        if (addy.IsValid()){
            sf::SocketUDP Socket;

            // Create bytes to send
            char Buffer[] = "Hi guys !";

            // Send data to "192.168.0.2" on port 4444
            if (Socket.Send(Buffer, sizeof(Buffer), addy, 4444) != sf::Socket::Done)
            {
                cout << "sending failed";
            }

            //bind socket before recieveing data
            sf::SocketUDP socket2;

            // Bind it (listen) to the port 4444
            if (!socket2.Bind(4444))
            {
                cout << "binding failed";
            }

            //get data
            char buffer2[128];
            std::size_t Received;
            sf::IPAddress Sender;
            if (socket2.Receive(buffer2, sizeof(buffer2), Received, Sender) != sf::Socket::Done)
            {
                // Error...
            }

            // Show the address of the sender
            std::cout << Sender << std::endl;

            // Show the message
            std::cout << buffer2 << std::endl; // "Hi guys !"

            //close socket
            socket2.Close();

        }

        // Clear the screen (fill it with black color)
        App.Clear();

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}



Errors:
Code: [Select]

/home/ubuntu/Desktop/projects/Metaship/main.cpp|51|error: no matching function for call to ‘sf::SocketUDP::Receive(char [128], unsigned int, size_t&, sf::IPAddress&)’|

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Port problems
« Reply #9 on: May 10, 2011, 12:58:14 pm »
Because there's no sf::SocketUDP::Receive function with such a prototype. Please read the doc.
Laurent Gomila - SFML developer

CyanPrime

  • Newbie
  • *
  • Posts: 18
    • View Profile
Port problems
« Reply #10 on: May 10, 2011, 06:25:54 pm »
Yeah, my bad. I had ver 1.6 and reading the tutorial for 1.2 :\

 

anything