dev.gamez.lv Forum Index dev.gamez.lv
Latvian Game Developers Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups 

sfml networking

 
dev.gamez.lv Forum Index -> Iesācējiem
View previous topic :: View next topic  
Author Message
purkskis



Joined: 16 Sep 2010
Posts: 4

PostPosted: Thu Sep 16, 2010 9:37 pm    Post subject: sfml networking

Sveiki,
mēģinu apgūt multiplayer spēles veidošanas pamatus..
patreiz gribu izveidot vairāku klientu <-> servera programmiņu
esmu izveidojis:
serveris:
Code:
#include <SFML/Network.hpp>
#include <iostream>
#include <stdlib.h>

#define max_clients 5

struct client_s
{
    sf::IPAddress   ip;
    unsigned short  port;
    float   x;
    float   y;
} client [max_clients];

bool SenderExist(sf::IPAddress ip, unsigned short ports)
{
    for (int n=0; n<max_clients; n++)
    {
        if(ip==client[n].ip && client[n].port==ports)
            return true;
    }
    return false;
}

int main()
{
    sf::IPAddress SenderIP;

    sf::SocketUDP Server;
    if (!Server.Bind(2323))
    {
        std::cout << " errors bindojot" << std::endl;
    }

    for (int n=0; n<max_clients; n++)
    {
        client[n].ip="0";
        client[n].port=0;
        client[n].x=0;
        client[n].y=0;
    }

    int s=0;
    float x ,y ;

    unsigned short SenderPort;

    while(true)
    {
        system("cls");

        for (int n=0; n<max_clients; n++)
            std::cout << "ID[" << n << "] " << client[n].ip << ":"<< client[n].port << " " << client[n].x << " " << client[n].y  << std::endl;

        sf::Packet Packet;
        if (Server.Receive(Packet, SenderIP, SenderPort) == sf::Socket::Done)
        {
            Packet >> x >> y;

            s++;
            std::cout << s << std::endl;

            if(SenderExist(SenderIP,SenderPort)==false)
            {
                for (int n=0; n<max_clients; n++)
                {
                    if(client[n].ip=="0" && client[n].port==0)
                    {
                        client[n].ip=SenderIP;
                        client[n].port=SenderPort;
                        break;
                    }
                }
            }
            else
            {
                for (int n=0; n<max_clients; n++)
                {
                    if(client[n].ip==SenderIP && client[n].port==SenderPort)
                    {
                        client[n].x=x;
                        client[n].y=y;
                    }
                }
            }
        }

        for (int n=0; n<max_clients; n++)
        {
            for (int n2=0; n2<max_clients; n2++)
            {
                Packet.Clear();
                Packet << n2 << client[n2].x << client[n2].y;
                Server.Send(Packet,client[n].ip, client[n].port);
            }
        }
    }
    Server.Close();
    return EXIT_SUCCESS;
}


klients:
Code:
#include <SFML/Network.hpp>
#include <iostream>

#define max_clients 5

struct client_s
{
    sf::IPAddress   ip;
    unsigned short  port;
    float   x;
    float   y;
} client [max_clients];

int main()
{
    sf::IPAddress ServerAddress = "78.154.134.179";

    sf::SocketUDP Client;

    int port = sf::Randomizer::Random(1024, 3000);

    if (!Client.Bind(port))
    {
        std::cout << " errors bindojot" << std::endl;
    }
    std::cout << " bindoju " << port << " portu" << std::endl;
    Client.SetBlocking(false);

    for (int n=0; n<max_clients; n++)
    {
        client[n].ip="0";
        client[n].port=0;
        client[n].x=0;
        client[n].y=0;
    }

    float x = sf::Randomizer::Random(124.f, 200.f);
    float y = 0.0;

    sf::IPAddress ServerIP;
    unsigned short ServerPort;

    sf::Clock Clock;
    float Send_Time=0;

    while(true)
    {
        system("cls");

        for (int n=0; n<max_clients; n++)
            std::cout << "ID[" << n << "] "<< client[n].ip << ":"<< client[n].port << " " << client[n].x << " " << client[n].y  << std::endl;

        sf::Packet Packet;
        if (Client.Receive(Packet, ServerIP, ServerPort) == sf::Socket::Done)
        {
            int nn;
            float xx,yy;
            Packet >> nn >> xx >> yy;
            client[nn].x=xx;
            client[nn].y=yy;
        }

        if(Clock.GetElapsedTime()-Send_Time>0.3)
        {
            Send_Time=Clock.GetElapsedTime();
            y++;
            Packet.Clear();
            Packet << x << y;
            Client.Send(Packet, ServerAddress, 2323);
        }
    }
    Client.Close();
    return EXIT_SUCCESS;
}


itkā +/- strādā, bet ir problēmas klientiem ar paku saņemšanu(baigais lags)
savā programmā es gribu uztaisīt kad klients sūta savas kordinātes serverim un tad serveris visiem pārējiem klientiem
patreiz serveris pacietīgi gaida kad atnāks kāda paka tad klients izsūta paku ar 2 mainīgajiem un katreiz izsūtot, otro mainīgo palielina par viens
serveris saņem, ieliek massīvā kordinātes un izsūta visiem klientiem
teorētiski visiem klientiem un serverim vaidzētu būt massīvam ar vienādiem elementiem tajā bet nekaa
serveris itkā saņem un izsūta pakas bet klientiem viņas krājas rinā
reku:

ko es daru nepareiz?
Back to top
View user's profile
snake5
Indago dalībnieks
Indago dalībnieks


Joined: 27 Jun 2007
Posts: 2590

PostPosted: Thu Sep 16, 2010 11:38 pm    Post subject:

Pamēģini izdrukāt klienta informāciju par tā pozīciju (t.i. "x" un "y", ne to, kas nāk no servera).
Varētu būt skaidrāka situācija.
Pagaidām izskatās, ka vienkārši pārāk lēni/reti atnāk ziņas un tāpēc klientiem un serverim ir veca informācija.
_________________
"There are two choices here: "looks good" and "realism"." -- Paul Nettle
Back to top
View user's profile Visit poster's website
purkskis



Joined: 16 Sep 2010
Posts: 4

PostPosted: Thu Sep 16, 2010 11:58 pm    Post subject:

Nēnu situācija jau +/- ir skaidra - klientiem lēnu atnāk pakas(ienākošas pakas lēnu tiek apstrādātas)
ar serveri itkā problēmu nav, viņš pakas saņem un uzreiz izsūta, bet klientiem saņēmtās pakās stāv rindā
piemēram tai screenaa ko ieliku izslēdzot serveri klienti turpina saņemt pakas līdz tie pēdējie cipari ir vienādi kā bij serverim
Back to top
View user's profile
elvman
Indago Uzvarētājs
Indago Uzvarētājs


Joined: 09 Apr 2003
Posts: 1278
Location: Kuldiga

PostPosted: Fri Sep 17, 2010 1:06 am    Post subject:

Neesmu īsti iedziļinājies kodā, bet pēc problēmas izklāsta varu pateikt vienu: tev vienmēr jārēķinās ar to, ka pakete neatnāks momentāni, cilvēks var sēdēt uz lēna neta, vai kādā citā valstī utt. tev vienmēr jāparedz šādi gadījumi.
_________________
long time; /* know C */
Back to top
View user's profile Visit poster's website
spicausis



Joined: 23 Jan 2006
Posts: 85
Location: Teika

PostPosted: Fri Sep 17, 2010 4:37 am    Post subject:

Serveris uz katru receive() ir spiests veikt max_clients² send();
Katrs klients uz vienu receive() ir spiests (vismaz vienreiz) notīrīt ekrānu — šobrīd, viņam tas jāpagūst paveikt 5× sekundē, citādi viņš nepagūs apstrādāt viņam nākošos pieprasījumus.

Nomaini client.c:
Code:
-       if (Client.Receive(Packet, ServerIP, ServerPort) == sf::Socket::Done)
+       while (Client.Receive(Packet, ServerIP, ServerPort) == sf::Socket::Done)


un klientam jau kļūs vieglāk, viņš varēs sev adresētās pakas pagūt apstrādāt;

bet tas nekādi nav uzskatāms par risinājumu, meklē iespējas samazināt sarunu skaitu, tur ir plašs lauks pārdomām un uzlabojumiem.
Back to top
View user's profile Visit poster's website
Display posts from previous:   
dev.gamez.lv Forum Index -> Iesācējiem All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group