Yep.
This class are writen from someone shared on sfml forum:
cpp:
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SFML/Network.hpp>
#include "network.h"
CNetwork::CNetwork()
{
port = 5432;
this->timeout = 5;
state = ready;
}
CNetwork::CNetwork( int port, float timeout )
{
this->port = port;
this->timeout = timeout;
state = ready;
}
CNetwork::~CNetwork()
{
Client.Close();
}
bool CNetwork::Connect( sf::IPAddress ClientAddress )
{
if ( state == connected ) return false;
this->ClientAddress = ClientAddress;
if (!this->ClientAddress.IsValid())
return false;
Client.SetBlocking(true);
if ( Client.Connect(5432, this->ClientAddress, timeout) == sf::Socket::Done )
state = connected;
else
state = timed_out;
Client.SetBlocking(false);
return (state == connected);
}
bool CNetwork::WaitConnection()
{
switch(state)
{
case connected: return false;
case ready:
Server.SetBlocking(false);
Server.Listen(port);
state = waiting;
case waiting:
if (Server.Accept(Client, &ClientAddress) == sf::Socket::Done)
{
state = connected;
Client.SetBlocking(false);
Server.Close();
return true;
}
default:
return false;
}
}
bool CNetwork::WaitData( sf::Packet* Packet )
{
if ( state != connected ) return false;
switch(Client.Receive(*Packet))
{
case sf::Socket::Done: return true;
case sf::Socket::Disconnected:
state = lost;
default:
return false;
}
return false;
}
bool CNetwork::SendData( sf::Packet* Packet )
{
if ( state != connected ) return false;
switch(Client.Send(*Packet))
{
case sf::Socket::Done:
Packet->Clear();
return true;
case sf::Socket::Disconnected:
state = lost;
default:
return false;
}
return false;
}
.h:
#ifndef NETWORK_H_INCLUDED
#define NETWORK_H_INCLUDED
#include <SFML/Network.hpp>
enum EState
{
ready = 0,
waiting,
connected,
timed_out,
lost
};
class CNetwork
{
public:
CNetwork();
CNetwork( int port, float timeout );
~CNetwork();
bool WaitConnection();
bool Connect( sf::IPAddress ClientAddress );
bool WaitData( sf::Packet* Packet );
bool SendData( sf::Packet* Packet );
float timeout;
int port;
EState state;
sf::IPAddress ClientAddress;
sf::SocketTCP Server;
sf::SocketTCP Client;
};
template <class T>
class CMessage
{
uint32_t id;
uint32_t len;
T data;
};
template <class T>
sf::Packet& operator <<(sf::Packet& Packet, const CMessage<T>& C)
{
return Packet << C.id << C.len << C.data;
}
template <class T>
sf::Packet& operator >>(sf::Packet& Packet, CMessage<T>& C)
{
return Packet >> C.id >> C.len >> C.data;
}
#endif
main function on server:
#include "network.h"
#include <iostream>
#include <string>
int main()
{
CNetwork Network;
sf::Packet ToSend;
sf::Packet Recieved;
bool runn = true;
while(runn)
{
Network.WaitConnection();
if ( Network.WaitData(&Recieved) )
{
std::string data;
Recieved >> data;
std::cout<<data<<std::endl;
Recieved.Clear();
}
}
return 0;
}
main function on client:
#include "network.h"
#include <iostream>
#include <string>
std::string doMsg(std::string &var)
{
var = "";
std::cout<<"\n[Message]: ";
std::cin>>var;
return var;
}
int main()
{
CNetwork Network;
sf::Packet ToSend;
sf::Packet Recieved;
std::string serverIp;
unsigned short serverPort;
std::cout<<"Connect to: ";
std::cin>>serverIp;
std::cout<<"Server port: ";
std::cin>>serverPort;
Network.port = serverPort;
Network.Connect(sf::IPAddress(serverIp));
bool runn = true;
std::cout<<"\n\nPreparing.... Please be patient.\n\n";
std::string msg;
while(doMsg(msg) != "\0")
{
if(msg == "!quit")
{
runn = false;
}
if (msg.length() > 0)
{
ToSend.Clear();
ToSend << msg;
// fill ToSend packet...
if (!Network.SendData(&ToSend))
{
std::cout<<"Oupss.. Something going wrong!";
}
} else std::cout<<"Invalid data (\0 ;))";
}
return 0;
}