Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Working of UDP  (Read 2201 times)

0 Members and 1 Guest are viewing this topic.

DarkNemesis

  • Newbie
  • *
  • Posts: 11
    • View Profile
Working of UDP
« on: August 01, 2013, 02:53:04 pm »
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
« Last Edit: August 01, 2013, 03:20:35 pm by DarkNemesis »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Working of UDP
« Reply #1 on: August 01, 2013, 04:09:03 pm »
First understand that the way SFML handles networking is nothing special, all basic networking tutorials that you can find will apply to the way SFML works. Second since you are new to networking you need to know that using UDP for your first networking project isn't that good of an idea. Reason being is because of how UDP works. That is why you should rewrite your code in TCP.

The difference between TCP and UDP is that TCP is connection based (this is what you are looking for) while UDP is connectionless. With TCP all data is guaranteed to arrive to the destination and in the order you sent it. While with UDP the data can arrive to the destination not even close to the order you sent it - or it may not arrive at all. If you use UDP you need to implement your own networking layer on top of it.

In summary, switch to TCP sockets. It will save you many headaches.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor