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

Author Topic: [SOLVED] Server and client  (Read 4165 times)

0 Members and 1 Guest are viewing this topic.

Nexy

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Server and client
« 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED] Server and client
« Reply #1 on: September 16, 2010, 05:56:00 pm »
The use of sockets and packets is explained in the tutorials.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Nexy

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Server and client
« Reply #2 on: September 16, 2010, 05:59:09 pm »
Quote from: "Nexus"
The use of sockets and packets is explained in the tutorials.


I know. But I can't make "server <-> client" using UDP packets.

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
[SOLVED] Server and client
« Reply #3 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SOLVED] Server and client
« Reply #4 on: September 16, 2010, 07:09:41 pm »
Look at the "sockets" sample in the SFML SDK.
Laurent Gomila - SFML developer

Nexy

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Server and client
« Reply #5 on: September 16, 2010, 07:23:45 pm »
@purkskis

It works! Thanks :)

//Topic to close