So... This is exactly what is happening with me:
It runs perfectly fine, no problems or issues.
I invite my friend to join to see if it works properly.
He joins, and it instantly ignores me, and focuses on him.
He disconnects.
Server focuses on me again.
How do I fix this? Also, if anyone has any suggestions on how to make my code more efficient, that would also be appreciated.
By the way, this is only the loop part of my server. Everything else works fine.
while(running){
IPAddress Address;
unsigned short port;
if(Listener.Receive(Received, Address, port) != Socket::Done){
cout << "Error receiving information from " << Address << " ( Message ) " << endl;
}
string message;
message.clear();
Received >> message;
if(message == "New character"){
float x,y;
bool exists = false;
int character;
string name;
Received >> name >> x >> y >> character;
cout << name << endl;
for(int a = 0; a < connected_players; a++){
if(playersc[a].getIP() == Address){
exists = true;
}
}
if(exists == false){
players[connected_players].SetX(x);
players[connected_players].SetY(y);
playersc[connected_players].setName(name);
playersc[connected_players].setCharacter(character);
playersc[connected_players].setIP(Address);
connected_players++;
cout << "There are now " << connected_players << " playing. " << Address << " has connected!" << endl;
}
}
if(message == "Remove character"){
for(int a = 0; a < connected_players; a++){
if(playersc[a].getIP() == Address){
for(int b = a; b < connected_players; b++){
playersc[b] = playersc[b + 1];
players[b] = players[b + 1];
}
cout << "now there are " << connected_players - 1 << " players playing. " << Address << " has disconnected!" << endl;
connected_players--;
}
}
}
if(message == "Update"){
float x, y;
string name;
int character;
Received >> name >> x >> y >> character;
for(int a = 0; a < connected_players; a++){
if(playersc[a].getIP() == Address){
playersc[a].setCharacter(character);
players[a].SetX(x);
players[a].SetY(y);
}
}
ToSend << connected_players - 1;
if(Listener.Send(ToSend, Address, port) != Socket::Done){
cout <<"Error sending information to "<< Address << " ( Update ) " << endl;
}
ToSend.Clear();
for(int a = 0; a < connected_players - 1; a++){
if(playersc[a].getIP() != Address){
ToSend << playersc[a].getName() << players[a].GetPosition().x <<players[a].GetPosition().y << playersc[a].getCharacter();
if(Listener.Send(ToSend, Address, port) != Socket::Done){
cout << "Error sending information to "<< Address << " ( Update ) " << endl;
}
ToSend.Clear();
}
}
}
}
[/code]