Jak tworzę serwer, i podłączam się do niego z np. 2 aplikacji client, to serwer wykrywa tylko pierwszego podłączonego, reszty już nie. Co jest przyczyną?
#include <SFML\Graphics.hpp>
#include <SFML\Network.hpp>
#include <string>
#include <iostream>
#include <map>
#include <conio.h>
using namespace std;
int main()
{
sf::UdpSocket socket;
char connectionType, mode;
char buffer[2000];
std::size_t received;
std::map<unsigned short, sf::IpAddress> computerID;
std::string text = "Connected to: ";
std::cout << "Enter (s) for Server, Enter (c) for Client" << std::endl;
cin >> connectionType;
unsigned short port;
std::cout << "Set port number: ";
cin >> port;
socket.bind(port);
if(connectionType == 's')
{
char answer = 'b';
do
{
sf::IpAddress rIp;
unsigned short port;
socket.receive(buffer, sizeof(buffer), received, rIp, port);
if(received > 0)
{
std::cout << "Connected to port: " << port << std::endl;
computerID[port] = rIp;
}
cin >> answer;
}while(answer != 'a');
}
else
{
std::string sIP;
std::cout << "Enter server ip: ";
char c;
while(true)
{
c = (char)getch();
if(c == 13)
break;
sIP += c;
std::cout << "*";
}
sf::IpAddress sendIP(sIP);
socket.send(text.c_str(), text.length() + 1, sendIP, 2000);
}
bool done = false;
while(!done)
{
if(connectionType == 's')
{
std::getline(cin, text);
std::map<unsigned short, sf::IpAddress>::iterator tempIterator;
for(tempIterator = computerID.begin(); tempIterator != computerID.end(); tempIterator++)
socket.send(text.c_str(), text.length() + 1, tempIterator -> second, tempIterator -> first);
}
else
{
sf::IpAddress tempIp;
unsigned short tempPort;
socket.receive(buffer, sizeof(buffer), received, tempIp, tempPort);
if(received > 0)
std::cout << "Received: " << buffer << std::endl;
else
std::cout << "Nothing" << buffer << std::endl;
}
}
system("pause");
return 0;
}