SFML community forums

Help => Network => Topic started by: Nexy on September 16, 2010, 05:47:13 pm

Title: [SOLVED] Server and client
Post by: Nexy on September 16, 2010, 05:47:13 pm
Hello,
I'm having problem with using  packets. Can someone makes easy server and client using UDP?

Code: [Select]

// With UDP sockets

Socket.Send(Packet, Address, Port);
Socket.Receive(Packet, Address);


Example:

       
client sends "123" to server and server receives it,
server sends "456" to client and client receives it.
Title: [SOLVED] Server and client
Post by: Nexus on September 16, 2010, 05:56:00 pm
The use of sockets and packets is explained in the tutorials (http://www.sfml-dev.org/tutorials/1.6/).
Title: [SOLVED] Server and client
Post by: Nexy on September 16, 2010, 05:59:09 pm
Quote from: "Nexus"
The use of sockets and packets is explained in the tutorials (http://www.sfml-dev.org/tutorials/1.6/).


I know. But I can't make "server <-> client" using UDP packets.
Title: [SOLVED] Server and client
Post by: purkskis on September 16, 2010, 06:21:24 pm
server:
Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>


int main()
{
    const unsigned short Port = 2435;

    sf::SocketUDP Server;
    if (!Server.Bind(Port))
    {
        std::cout << " error" << std::endl;
    }

    sf::Packet Packet;
    sf::IPAddress ClientAddress;
    unsigned short ClientPort;
    Server.Receive(Packet, ClientAddress, ClientPort);

    int a;
    Packet >> a;
    std::cout << "packet : " << a << " from "<< ClientAddress <<" port: "<< ClientPort << std::endl;

    a=12345;
    Packet.Clear();
    Packet << a;
    Server.Send(Packet, ClientAddress, ClientPort);

    std::cin.ignore(10000, '\n');
    Server.Close();
    return EXIT_SUCCESS;
}


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


int main()
{
    const unsigned short Port = 2400;

    sf::SocketUDP Client;
    if (!Client.Bind(Port))
    {
        std::cout << " error" << std::endl;
    }

    sf::Packet Packet;
    sf::IPAddress ServerAddress="127.0.0.1";
    unsigned short ServerPort=2435;

    int a;
    a=54321;
    Packet << a;
    Client.Send(Packet, ServerAddress, ServerPort);

    Packet.Clear();
    Client.Receive(Packet, ServerAddress, ServerPort);

    Packet >> a;
    std::cout << "packet : " << a << " from "<< ServerAddress <<" port: "<< ServerPort << std::endl;

    std::cin.ignore(10000, '\n');
    Client.Close();
    return EXIT_SUCCESS;
}


 this will do?
Title: [SOLVED] Server and client
Post by: Laurent on September 16, 2010, 07:09:41 pm
Look at the "sockets" sample in the SFML SDK.
Title: [SOLVED] Server and client
Post by: Nexy on September 16, 2010, 07:23:45 pm
@purkskis

It works! Thanks :)

//Topic to close