Back to index

libsfml  1.6+dfsg2
Functions
Sockets.cpp File Reference
#include <iostream>
#include <cstdlib>

Go to the source code of this file.

Functions

void DoClientTCP (unsigned short Port)
 Create a client and connect it to a running server.
void DoClientUDP (unsigned short Port)
 Create a client and send a message to a running server.
void DoServerTCP (unsigned short Port)
 Launch a server and wait for incoming connections.
void DoServerUDP (unsigned short Port)
 Launch a server and wait for incoming messages.
int main ()
 Entry point of application.

Function Documentation

void DoClientTCP ( unsigned short  Port)

Create a client and connect it to a running server.

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

Definition at line 13 of file TCP.cpp.

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

    // Create a TCP socket for communicating with server
    sf::SocketTCP Client;

    // Connect to the specified server
    if (Client.Connect(Port, ServerAddress) != sf::Socket::Done)
        return;
    std::cout << "Connected to server " << ServerAddress << std::endl;

    // Receive a message from the client
    char Message[128];
    std::size_t Received;
    if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
        return;

    // Show it
    std::cout << "Message received from server : \"" << Message << "\"" << std::endl;

    // Define a message to send back to the server
    char ToSend[] = "Hi, I'm a client !";

    // Send the message
    if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
        return;
    std::cout << "Message sent to server : \"" << ToSend << "\"" << std::endl;

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

Here is the caller graph for this function:

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 DoServerTCP ( unsigned short  Port)

Launch a server and wait for incoming connections.

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

Definition at line 58 of file TCP.cpp.

{
    // Create a TCP socket for communicating with clients
    sf::SocketTCP Server;

    // Listen to a port for incoming connections
    if (!Server.Listen(Port))
        return;
    std::cout << "Server is listening to port " << Port << ", waiting for connections... " << std::endl;

    // Wait for a connection
    sf::IPAddress ClientAddress;
    sf::SocketTCP Client;
    if (Server.Accept(Client, &ClientAddress) != sf::Socket::Done)
        return;
    std::cout << "Client connected : " << ClientAddress << std::endl;

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

    // Receive a message back from the client
    char Message[128];
    std::size_t Received;
    if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
        return;

    // Show the message
    std::cout << "Message received from the client : \"" << Message << "\"" << std::endl;

    // Close the sockets when we're done
    Client.Close();
    Server.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:

int main ( )

Entry point of application.

Returns:
Application exit code

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

Definition at line 25 of file Sockets.cpp.

{
    // Choose a random port for opening sockets (ports < 1024 are reserved)
    const unsigned short Port = 2435;

    // TCP or UDP ?
    char Protocol;
    std::cout << "Do you want to use TCP ('t') or UDP ('u') ? ";
    std::cin  >> Protocol;

    // Client or server ?
    char Who;
    std::cout << "Do you want to be a server ('s') or a client ('c') ? ";
    std::cin  >> Who;

    if (Who == 's')
    {
        // Run as a server
        if (Protocol == 't')
            DoServerTCP(Port);
        else
            DoServerUDP(Port);
    }
    else
    {
        // Run as a client
        if (Protocol == 't')
            DoClientTCP(Port);
        else
            DoClientUDP(Port);
    }

    // Wait until the user presses 'enter' key
    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');
    std::cin.ignore(10000, '\n');

    return EXIT_SUCCESS;
}

Here is the call graph for this function: