Here's my current server code:
/************************************************************
SERVER
************************************************************/
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
#include <SFML/Graphics.hpp>
bool running = true;
void netThread(){
char buffer[128];
std::size_t received;
sf::IpAddress sender;
unsigned short port;
// Create the UDP socket
sf::UdpSocket socket;
socket.setBlocking(false);
// Bind it (listen) to the port 4567
if (!socket.bind(4567)){
// Error...
cout << "Unable to bind UDP socket: 4567" << endl;
}
while(running){
int rec = socket.receive(buffer, sizeof(buffer), received, sender, port);
if(rec != sf::Socket::Done){
//Error
}
if(rec == sf::Socket::Disconnected)
cout << "D/C" << endl;
// Show the address / port of the sender
std::cout << sender << ":" << port << std::endl;
// Show the message
std::cout << buffer << std::endl; // "Hi guys !"
}
}
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::Thread thread(&netThread);
thread.launch();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed){
running = false;
window.close();
}
}
window.clear();
window.display();
}
running = false;
return 0;
}
Can anyone make any suggestions as to what I should do for a UDP connection?
I'm trying to have multiple clients for my server, so...would I use just one socket? Or multiple sockets?
I'm assuming I just use one as there's no actual connection and it's just packets coming into
my server, however how would I differentiate different players on my game? By IP?
Or would I have them, for each message they send, send a special ID that uniquely identifies
who they are on the server...?
Not sure how to set this up.