Hey guys, so I am attempting to work on a Server -> Multiple client based application. Right now I have it set up so I can send each client messages from the server application. The only problem is my program won't continually accept connections. Right now it is set up to accept a connection, I can say a message to that client and then I can connect another client.
I have been working off the CodingMadeEasyTutorials, but I can't seem to figure this one out. Basically I guess what I am asking is how do I continually check for new connections and be able to type from the server at the same time? Do I need to already have to set up some sort of login server?
Any help would be appreciated.
Server Code for Reference
#include<SFML/Graphics.hpp>
#include<SFML/Network.hpp>
#include<string>
#include<iostream>
#include<conio.h>
#include<vector>
using namespace std;
int main()
{
sf::IpAddress ip = sf::IpAddress::getPublicAddress();
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 << ip << std::endl;
socket.bind(2000);
//socket.setBlocking(false);
//char answer = 'b';
//do
//{
//cin >> answer;
//}while(answer != 'a');
bool done = false;
while(!done)
{
sf::IpAddress rIp;
unsigned short port;
socket.receive(buffer, sizeof(buffer), received, rIp, port);
if(received > 0)
{
computerID[port] = rIp;
cout << "someone has connected" << endl;
}
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);
}
system(" pause" );
return 0;
}
Client Code for Reference
#include<SFML/Graphics.hpp>
#include<SFML/Network.hpp>
#include<iostream>
#include<conio.h>
#include<vector>
using namespace std;
int main()
{
sf::UdpSocket socket;
std::string text = "Connected to:" ;
char buffer[2000];
std::size_t received;
unsigned short port;
std::cout << " Set port number:" ;
cin >> port;
socket.bind(port);
std::string sIP;
std::cout << " Enter server ip: " ;
cin >> sIP;
sf::IpAddress sendIP(sIP);
socket.send(text.c_str(), text.length() + 1, sendIP, 2000);
bool done = false;
while(!done)
{
sf::IpAddress tempIp;
unsigned short tempPort;
socket.receive(buffer, sizeof(buffer), received, tempIp, tempPort);
if(received > 0)
std::cout << " Received: " << buffer << std::endl;
}
return 0;
}