Oh, that's what you meant by minimal code.
The code isn't that large, so hopefully the whole code will be minimal.
Client:
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <process.h>
struct User {
bool exists;
char name[255], ip[16];
};
short slot = 0;
int main(int argc, char **argv){
char buf[255];
bool *close;
close = false;
User *u = new User[4];
sf::SocketUDP Client;
sf::IPAddress Server;
Client.SetBlocking(false);
char name[255], ip[255];
printf("Enter username: "); scanf("%s", name);
printf("Enter IP (enter lh for localhost): "); scanf("%s", ip);
if(strcmp("lh", ip) == 0)
Server = sf::IPAddress::LocalHost;
else
Server = sf::IPAddress(ip);
if(!Server.IsValid())
return 1;
if(!Client.Bind(0))
return 1;
sf::Packet Ser;
sf::Packet Name;
sprintf(buf, "%c%s", 1, name);
Name.Append(buf, sizeof(buf));
Client.Send(Name, Server, 1357);
std::cout << "Successfully connected!" << std::endl;
sf::RenderWindow App(sf::VideoMode(640, 480), "SFML Sockets");
App.EnableKeyRepeat(false);
App.SetFramerateLimit(60.0);
sf::Event e;
while(App.IsOpened()){
Name.Clear();
App.Clear();
if(Client.Receive(Ser, Server, 1357) == sf::Socket::Done){
if(Ser.GetData()[0] == 1){
sprintf(buf, "%s", Ser.GetData());
for(int i = 1; i < sizeof(buf); i++){
buf[i - 1] = buf[i];
}
slot = atoi(buf);
std::cout << "<Given slot [" << slot << "]>" << std::endl;
}else if(Ser.GetData()[0] == 2){
sprintf(buf, "%s", Ser.GetData());
for(int i = 1; i < sizeof(buf); i++){
buf[i - 1] = buf[i];
}
int connected = atoi(strtok(buf, ":"));
for(int i = 0; i < connected; i++) {
int tId = atoi(strtok(NULL, ","));
u[tId].name[255] = atoi(strtok(NULL, ";"));
}
}
}
if(App.GetEvent(e)){}
App.Display();
}
Client.Close();
return 0;
}
Server:
#include <SFML/Network.hpp>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <process.h>
struct User {
bool exists;
char name[255], ip[16];
};
short giveSlot(User *u, int max);
int main(int argc, char **argv){
unsigned short Port = 1357;
short max = 4, connected = 0;
User *u = new User[max];
sf::SocketUDP Server;
sf::IPAddress Sender;
for(int i = 1; i < max; i++)
u[i].exists = false;
if(!Server.Bind(Port))
return 1;
char buf[255], bufs[255];
sf::Packet Name;
sf::Packet Sen;
while(true){
short slot = giveSlot(u, max); // gives the next open slot the user
Server.Receive(Name, Sender, Port);
if(Name.GetData()[0] == 1){
sprintf(buf, "%s", Name.GetData());
for(int i = 1; i < sizeof(buf); i++){
buf[i - 1] = buf[i];
}
std::cout << buf << " (" << Sender << ") has connected! <Given slot [" << slot << "]>" << std::endl;
u[slot].exists = true;
u[slot].name[255] = buf[255];
Sen.Clear();
sprintf(bufs, "%c%c", 1, u[slot]);
Sen.Append(bufs, sizeof(bufs));
Server.Send(Sen, Sender, Port);
for(int i = 1; i <= max; i++){
if(u[i].exists == true){
if(i != slot){
Sen.Clear();
sprintf(bufs, "%c%i:%i,%s;", 2, connected, i, u[i].name);
Sen.Append(bufs, sizeof(bufs));
Server.Send(Sen, Sender, Port);
}
}
}
}else if(Name.GetData()[0] == 2){
sprintf(buf, "%s", Name.GetData());
for(int i = 0; i < sizeof(buf); i++){
buf[i - 1] = buf[i];
}
std::cout << buf << " (" << Sender << ") has disconnected!" << std::endl;
//u[slot].exists = false;
//memset(u[slot].name, 0, sizeof(u[slot].name));
}
}
Server.Close();
return 0;
}
EDIT: Oh dear, now that I look at it, it does seem a bit long..