Added the trunk/branches/tags directories at repository root, and moved previous root into trunk/
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1002 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
commit
2f524481c1
974 changed files with 295448 additions and 0 deletions
18
samples/sockets/Makefile
Normal file
18
samples/sockets/Makefile
Normal file
|
@ -0,0 +1,18 @@
|
|||
EXEC = sockets
|
||||
OBJ = Sockets.o TCP.o UDP.o
|
||||
|
||||
all: $(EXEC)
|
||||
|
||||
sockets: $(OBJ)
|
||||
$(CC) $(LDFLAGS) -o $(EXECPATH)/$@ $(OBJ) -lsfml-network -lsfml-system
|
||||
|
||||
%.o: %.cpp
|
||||
$(CC) -o $@ -c $< $(CFLAGS)
|
||||
|
||||
.PHONY: clean mrproper
|
||||
|
||||
clean:
|
||||
@rm -rf *.o
|
||||
|
||||
mrproper: clean
|
||||
@rm -rf $(EXECPATH)/$(EXEC)
|
62
samples/sockets/Sockets.cpp
Normal file
62
samples/sockets/Sockets.cpp
Normal file
|
@ -0,0 +1,62 @@
|
|||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <iostream>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Function prototypes
|
||||
// (I'm too lazy to put them into separate headers...)
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoClientTCP(unsigned short Port);
|
||||
void DoClientUDP(unsigned short Port);
|
||||
void DoServerTCP(unsigned short Port);
|
||||
void DoServerUDP(unsigned short Port);
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Entry point of application
|
||||
///
|
||||
/// \return Application exit code
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
// 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;
|
||||
}
|
93
samples/sockets/TCP.cpp
Normal file
93
samples/sockets/TCP.cpp
Normal file
|
@ -0,0 +1,93 @@
|
|||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Network.hpp>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Create a client and connect it to a running server
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoClientTCP(unsigned short Port)
|
||||
{
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Launch a server and wait for incoming connections
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoServerTCP(unsigned short Port)
|
||||
{
|
||||
// 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();
|
||||
}
|
65
samples/sockets/UDP.cpp
Normal file
65
samples/sockets/UDP.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Network.hpp>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Create a client and send a message to a running server
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoClientUDP(unsigned short Port)
|
||||
{
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Launch a server and wait for incoming messages
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
void DoServerUDP(unsigned short Port)
|
||||
{
|
||||
// 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();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue