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

Author Topic: Server and client UDP packet communication  (Read 3434 times)

0 Members and 1 Guest are viewing this topic.

dalslandan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Server and client UDP packet communication
« on: July 25, 2017, 11:43:04 pm »
Hello.

I got a little stuck when trying to setup a monitoring system sending all information to the server.

The idea is to use UDP and packets to simplify things, communication is only one-way.

This is what I've come up with so far, the idea is to be able to have a loop at the server side which spits out whatever comes in (cout), and if there is an error display that too.
#include <SFML/Network.hpp>
#include <iostream>

using namespace std;

int main() {

        sf::UdpSocket socket;

        // bind the socket to a port
        if (socket.bind(54000) != sf::Socket::Done) {
                cout << "There was an error binding to port 54000." << endl;
        }
        else {
                cout << "UDP socket is listening." << endl;
        }

        system("pause"); //Please ignore this, windows specific non-recommended stuff.
        return 0;
}

And on the client side I just want to send some information.
#include <SFML/Network.hpp>
#include <iostream>

using namespace std;

int main() {

        sf::IpAddress recipient = "127.0.0.1";
        unsigned short port = 54000;

        string message = "Hello";
        double speed = 10.3345;

        sf::Packet packet;

        packet << message << speed;
        sf::UdpSocket.send(packet, recipient, port); // This is the line VS 2015 don't like*

        system("pause"); //Please ignore this, windows specific non-recommended stuff.
        return 0;
}

My coding skills are rather basic, but I'm missing something here.

Thank you for anyone taking their time to help me out. :)

For the record, I used these pages when trying to set it up.
https://www.sfml-dev.org/tutorials/2.4/network-socket.php
https://www.sfml-dev.org/tutorials/2.4/network-packet.php
« Last Edit: July 26, 2017, 01:52:51 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: Server and client UDP packet communication
« Reply #1 on: July 26, 2017, 12:35:57 am »
So what's the problem you're stuck with?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dalslandan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Server and client UDP packet communication
« Reply #2 on: July 26, 2017, 01:45:14 am »
This line of code isn't accepted by the compiler. (In the client code.)

sf::UdpSocket.send(packet, recipient, port);

So to simply put it, something is wrong with the code to send packets to the server.*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: Server and client UDP packet communication
« Reply #3 on: July 26, 2017, 01:52:37 am »
This line of code isn't accepted by the compiler. (In the client code.)

sf::UdpSocket.send(packet, recipient, port);
So what's the exact error message?
Error messages aren't just fun decoration or "hey something went wrong" notifications, but they tell you exactly what the issue is. The challenge is just to understand them. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dalslandan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Server and client UDP packet communication
« Reply #4 on: July 26, 2017, 02:02:01 am »
Sorry.

There are 6 errors, all corresponding to the same line.

1. expected an identifier
2. variable "packet" is not a type name
3. variable "recipient" is not a type name
4. variable "port" is not a type name
5. C2143 syntax error: missing ';' before .                      (.send is it referring too*)
6. C2059 syntax error: '.'
« Last Edit: July 26, 2017, 02:05:18 am by dalslandan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
Re: Server and client UDP packet communication
« Reply #5 on: July 26, 2017, 02:05:43 am »
Ah, I only saw the mistake now.
You can't call a function on a class name, but you need to create an instance!

sf::UdpSocket udpSocket;
//...
udpSocket.send(packet, recipient, port);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dalslandan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Server and client UDP packet communication
« Reply #6 on: July 26, 2017, 02:08:39 am »
Makes sense, just tested it out and everything works.

Awesome, thank you.  :)