Hi people,
i've read the networking and packet tutorial and tried to put everything in a working example.
So, the connection is accepted from the server showing the client ip...but is not showing the packet client have sended.
Here is the client code:
#include <iostream>
#include <SFML/Network.hpp>
int main(int argc, char* argv[])
{
// ----- The client -----
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("192.168.1.71", 5001);
if (status != sf::Socket::Done){
// error...
}
sf::Uint16 x = 10;
std::string s = "hello";
double d = 0.6;
sf::Packet packet;
packet << x << s << d;
socket.send(packet);
return 0;
}
An then, the server code:
#include <iostream>
#include <SFML/Network.hpp>
int main(int argc, char* argv[])
{
// ----- The server -----
// Create a listener to wait for incoming connections on port 55001
sf::TcpListener listener;
listener.listen(5001);
if (listener.listen(5001) != sf::Socket::Done){
// error...
}
// Wait for a connection
sf::TcpSocket client;
if (listener.accept(client) != sf::Socket::Done)
{
// error...
}
std::cout << "New client connected: " << client.getRemoteAddress() << std::endl;
// Receive a message from the client
while(true){
sf::Packet packet;
client.receive(packet);
sf::Uint16 x;
std::string s;
double d;
packet >> x >> s >> d;
if (packet >> x) {
std::cout << x << std::endl;
}
}
return 0;
}