Hi everyone ! I've got some weird problem with my code...
The first time, I've make an array and if a client disconnected, I push the last elements to overwrite the client disconnected
for(int i=0;i<coNumber /*Number of connected clients*/;i++){
if(socket[i].receive(packet)!=sf::Socket::Done){
coNumber--; //Remove one client
for(int j=i;j<coNumber;j++){
socket[i]=socket[i+1];
}
}
}
But got the awesome
NonCopyable error
So I've ask to Lukas/eXpl0it3r (Thanks to him !)
And suggest me to use
std::vector<std::unique_ptr<sf::TcpSocket>> socket;
instead of
sf::TcpSocketsocket[MAXCONNECTIONS];
play with somes pointers, and then get stuck...
An error occured with this line:
if(listener.accept(*socket[coNumber])==sf::Socket::Done && coNumber<MAXCONNEXIONS){
No error during compiling, but during the execution (Application not responding)
Error 255
So there is my actual code:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include <string>
#include <memory>
#include <iostream>
#define PORT 2903
#define MAXCONNEXIONS 10
struct Entity{
std::string name;
int lvl;
int x;
int y;
int direction;
};
sf::Packet& operator <<(sf::Packet& packet, const Entity& entity) {
return packet << entity.name << entity.lvl << entity.x << entity.y << entity.direction;
}
sf::Packet& operator >>(sf::Packet& packet, Entity& entity) {
return packet >> entity.name >> entity.lvl >> entity.x >> entity.y >> entity. direction;
}
int main(int argc, char* argv[]){
std::cout<<"Started !"<<std::endl;
std::vector<std::unique_ptr<sf::TcpSocket>> socket;
int coNumber = 0;
sf::Packet packet;
Entity tmpEntity;
sf::TcpListener listener;
listener.setBlocking(false);
listener.listen(PORT);
while(1){
if(listener.accept(*socket[coNumber])==sf::Socket::Done && coNumber<MAXCONNEXIONS){
coNumber++;
}
if(coNumber>0){
for(int i=0;i<coNumber;i++){
if(socket[i]->receive(packet)!=sf::Socket::Done){
coNumber--;
}
if(packet>>tmpEntity){
std::cout<<tmpEntity.name<<","<<tmpEntity.lvl<<","<<tmpEntity.x<<","<<tmpEntity.y<<","<<tmpEntity.direction<<std::endl;
}
}
}
}
return 0;
}
Thanks for your help !