Back to index

libsfml  1.6+dfsg2
Functions
UDP.cpp File Reference
#include <SFML/Network.hpp>
#include <iostream>

Go to the source code of this file.

Functions

void DoClientUDP (unsigned short Port)
 Create a client and send a message to a running server.
void DoServerUDP (unsigned short Port)
 Launch a server and wait for incoming messages.

Function Documentation

void DoClientUDP ( unsigned short  Port)

Create a client and send a message to a running server.

/////////////////////////////////////////////////////////

Definition at line 13 of file UDP.cpp.

{
    // Ask for server address
    sf::IPAddress ServerAddress;
    do
    {
        std::cout << "Type address or name of the server to send the message to : ";
        std::cin  >> ServerAddress;
    }
    while (!ServerAddress.IsValid());

    // Create a UDP socket for communicating with server
    sf::SocketUDP Client;

    // Send a message to the server
    char Message[] = "Hi, I'm a client !";
    if (Client.Send(Message, sizeof(Message), ServerAddress, Port) != sf::Socket::Done)
        return;
    std::cout << "Message sent to server : \"" << Message << "\"" << std::endl;

    // Close the socket when we're done
    Client.Close();
}

Here is the caller graph for this function:

void DoServerUDP ( unsigned short  Port)

Launch a server and wait for incoming messages.

/////////////////////////////////////////////////////////

Definition at line 42 of file UDP.cpp.

{
    // Create a UDP socket for communicating with clients
    sf::SocketUDP Server;

    // Bind it to the specified port
    if (!Server.Bind(Port))
        return;

    // Receive a message from anyone
    sf::IPAddress ClientAddress;
    unsigned short ClientPort;
    char Message[128];
    std::size_t Received;
    if (Server.Receive(Message, sizeof(Message), Received, ClientAddress, ClientPort) != sf::Socket::Done)
        return;

    // Display it
    std::cout << "Message received from " << ClientAddress << " on port " << ClientPort
              << ": \"" << Message << "\"" << std::endl;

    // Close the socket when we're done
    Server.Close();
}

Here is the caller graph for this function: