Back to index

libsfml  1.6+dfsg2
TCP.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Network.hpp>
00006 #include <iostream>
00007 
00008 
00013 void DoClientTCP(unsigned short Port)
00014 {
00015     // Ask for server address
00016     sf::IPAddress ServerAddress;
00017     do
00018     {
00019         std::cout << "Type address or name of the server to connect to : ";
00020         std::cin  >> ServerAddress;
00021     }
00022     while (!ServerAddress.IsValid());
00023 
00024     // Create a TCP socket for communicating with server
00025     sf::SocketTCP Client;
00026 
00027     // Connect to the specified server
00028     if (Client.Connect(Port, ServerAddress) != sf::Socket::Done)
00029         return;
00030     std::cout << "Connected to server " << ServerAddress << std::endl;
00031 
00032     // Receive a message from the client
00033     char Message[128];
00034     std::size_t Received;
00035     if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
00036         return;
00037 
00038     // Show it
00039     std::cout << "Message received from server : \"" << Message << "\"" << std::endl;
00040 
00041     // Define a message to send back to the server
00042     char ToSend[] = "Hi, I'm a client !";
00043 
00044     // Send the message
00045     if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
00046         return;
00047     std::cout << "Message sent to server : \"" << ToSend << "\"" << std::endl;
00048 
00049     // Close the socket when we're done
00050     Client.Close();
00051 }
00052 
00053 
00058 void DoServerTCP(unsigned short Port)
00059 {
00060     // Create a TCP socket for communicating with clients
00061     sf::SocketTCP Server;
00062 
00063     // Listen to a port for incoming connections
00064     if (!Server.Listen(Port))
00065         return;
00066     std::cout << "Server is listening to port " << Port << ", waiting for connections... " << std::endl;
00067 
00068     // Wait for a connection
00069     sf::IPAddress ClientAddress;
00070     sf::SocketTCP Client;
00071     if (Server.Accept(Client, &ClientAddress) != sf::Socket::Done)
00072         return;
00073     std::cout << "Client connected : " << ClientAddress << std::endl;
00074 
00075     // Send a message to the client
00076     char ToSend[] = "Hi, I'm the server";
00077     if (Client.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
00078         return;
00079     std::cout << "Message sent to the client : \"" << ToSend << "\"" << std::endl;
00080 
00081     // Receive a message back from the client
00082     char Message[128];
00083     std::size_t Received;
00084     if (Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
00085         return;
00086 
00087     // Show the message
00088     std::cout << "Message received from the client : \"" << Message << "\"" << std::endl;
00089 
00090     // Close the sockets when we're done
00091     Client.Close();
00092     Server.Close();
00093 }