Excuse me, but you are doing very strange things here...
- When lauching the server, the server is never finishing, so client(1) will never be launched.
- Why stopping the clien to start a server (with z), when launching client ?
What is important is that UDP socket can send and receive, no need to be a server in order to receive packets.
What is meant by "connected UDP" is that your packets must manage a "virtual connexion" between server and client :
- ask to connect to the server
- if authorized, connect effectively
- when deconnecting, send it to the server in order the server deconnect the client
- etc...
So, I would modify your code as this (directly, no verification, there can be some errors
) :
int main()
{
cout << "Type: \'s\' to run server or \'c\' to run client." <<endl;
char input;
cin >> input;
if(input == 's')
{
server();
}
else if(input == 'c')
{
client();
}
}
void server(int param)
{
SocketUDP Server;
if (!Server.Bind(10023))
{
cout << "Error" << endl;
return;
}
cout << "Server initialized." << endl;
std::vector<IPAddress> MyClients; // Important : the list of clients connected
IPAddress SenderIP;
unsigned short SenderPort;
sf::Packet Packet;
while (true)
{
if(Server.Receive(Packet, SenderIP, SenderPort) == sf::Socket::Done)
{
// Extract the message to know what to do
string message;
Packet >> message;
// Is a new client trying to connect ?
if (message=="CONN")
{
//... Verify that the client is not connected yet (in our client vector)
//... If not, verify we are not going over the MAX CLIENT count
//... If not, send to the client that the connection is ok (message="OK")
//... Add the IPAddress of the client in our vector
}
//A connected client is sending a message
{
//... Verify that the client is a connected one (in our client vector)
//... If not, do nothing or send it something to explain
//... If yes, ok, you can use the message
//... If the message is "DECO", then remove the client from the vector, because it just deconnect !
}
}
}
Server.Close();
}
By the way, you'll need something to close the server properly.