Hello Guys.. Some of you might be knowing me already on forums, I work on visual studio and am learning game programming.
This time I wanted to learn sfml networking, so I decided to use it, but it seems my concepts regarding the same are not clear..
I created a client and server, and i wanted to build something like this :
Client tries to connect to server, sends a connection packet (@ UDP), server receives it, push backs a player in the dynamic list, storing that IP address and sends continuous data to that client. When the client is closed, it again sends a disconnection packet to server, server erases that player and stops sending data to that client. If Client tries to connect to server and don't receive a confirmation, it will keep on sending connection request to server till it finally connects with it.
This is what I intent to create, and the following is the code which I have written. But somehow its not working as accepted, probably because I am doing a mistake which I am not able to make out. So I would please like someone to help me where am I wrong..
Client :
#include<SFML/Graphics.hpp>
#include<SFML/Network.hpp>
#include<fstream>
#define PORT 54000
#define IP "172.22.31.173"
#define REQ(a) connect_request = a; connect_packet.clear(); connect_packet << connect_request;
using namespace std;
using namespace sf;
int main()
{
RenderWindow client_window(sf::VideoMode(800, 600), "PFR Client");
fstream fout("log.txt");
//Creating Incoming and Outgoing sockets
UdpSocket out_socket, in_socket;
out_socket.bind(PORT); in_socket.bind(PORT);
in_socket.setBlocking(false);
//Receipent IP Address and Port
unsigned short port;
IpAddress sender;
/* Packets:-
connect_packet : for connecting/disconnecting from the servers. Sends 100/101 respectively.
game_packet : for receiving continous game data, failure ensures disconnection from server.
confirm_packet : for ensuring stable connection. */
Packet connect_packet, game_packet, confirm_packet;
//8 Byte Integers for requesting connection, and saving the status of former.
sf::Int8 connect_request, connect_status = 0;
while (client_window.isOpen())
{
sf::Event event;
while (client_window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
REQ(101);
out_socket.send(connect_packet, IP, PORT);
client_window.close();
}
}
if(connect_status == 0)
{
client_window.clear(Color::Blue);
REQ(100);
out_socket.send(connect_packet, IP, PORT);
in_socket.receive(confirm_packet, sender, port);
confirm_packet >> connect_status;
confirm_packet.clear();
}
else
client_window.clear(Color::Red);
client_window.display();
}
}
Server:
#include<SFML\Network.hpp>
#include<SFML\Graphics.hpp>
#include<list>
#include<fstream>
#include"Player.h"
#define PORT 54000
#define PLAYERLOOP for(auto it1 = players.begin(); it1 != players.end(); it1++)
#define PLAYERCHECK PLAYERLOOP if(it1->IP_address == temp_sender)
using namespace std;
using namespace sf;
std::list<Player> players;
int main()
{
fstream fout("log.txt");
sf::RenderWindow server_window(sf::VideoMode(800, 600), "PFR Server");
sf::UdpSocket incoming_socket, send_socket;
incoming_socket.bind(PORT); send_socket.bind(PORT);
incoming_socket.setBlocking(false);
Packet send_packet, connect_packet;
unsigned short port; IpAddress temp_sender;
while (server_window.isOpen())
{
sf::Event event;
while (server_window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
server_window.close();
}
if (incoming_socket.receive(connect_packet, temp_sender, port) == Socket::Done)
{
server_window.clear(Color::Red);
sf::Int8 request;
connect_packet >> request;
// Requesting code 100 for connecting
if (request == 100)
{
bool player_exist = false;
PLAYERCHECK {player_exist = true; break;}
if (player_exist == false)
{
server_window.clear(Color::Green);
players.push_back(Player(temp_sender));
sf::Int8 connect_status = 87;
Packet confirm_packet; confirm_packet << connect_status;
send_socket.send(confirm_packet, temp_sender, PORT);
connect_packet.clear();
}
}
// Requesting Code 101 for disconnecting
else if (request == 101)
PLAYERCHECK {players.erase(it1); connect_packet.clear(); break;}
}
// Sending data to connected players
PLAYERLOOP send_socket.send(send_packet, it1->IP_address, PORT);
server_window.display();
}
}
I do hope I have clearly stated my problem, and that someone would be able to make out the mistake for me and help me learn sfml networking.
Thanks in advance