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

Author Topic: my first network program  (Read 5675 times)

0 Members and 1 Guest are viewing this topic.

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
my first network program
« on: September 13, 2010, 09:02:16 pm »
hey,
i want to make simple program that receives coordinates from client and sends them to other clients.
so far i've made this: http://www.failiem.lv/down.php?i=aofrvh&n=multi_graf.rar
can someone take a look at it?  :roll:

everything seams to be working fine if only one client is "connected"
that sprite who looks like shadow is player coords which are sent to server and received back..

BUT when 2 clients are active and moving, its starts to lag :?
Server log shows those received coordinates and the same are sent back, but client displays them later, because there are already dozens of coordinates waiting in line to be displayed.

What am I doing wrong?

and another question - how to statically link  libgcc_s_dw2-1.dll
i trayed  -static-libgcc but it doesn't work :/

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
my first network program
« Reply #1 on: September 14, 2010, 06:33:40 pm »
Anyone?   :cry:

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
my first network program
« Reply #2 on: September 15, 2010, 10:31:26 pm »
ok.. i rewrote my masterpiece ;D
server:
Code: [Select]
#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;
}


client:
Code: [Select]
#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;
}


every time clients sends a packet, Y is increased by 1
When server receives a packet from client, it stores it into array and sends whole array to all clients, so array at server and client should be +/- equal
but i get this:
Is it becouse I run 4 clients at 1 pc or I do something stupid at program?

PeterWelzien

  • Newbie
  • *
  • Posts: 38
    • View Profile
my first network program
« Reply #3 on: September 16, 2010, 08:34:42 am »
It could be that you're missing packets. Can you try switching to TCP and see if it works?

(I'm only guessing though)
/Peter Welzien

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
my first network program
« Reply #4 on: September 16, 2010, 09:23:14 am »
Quote from: "purkskis"
but i get this: ...
Please, can you explain what you got ?
I'm at the office right now, and I can't use sites like imageshack or photobucket, etc...

It will be easier to help if you can explain in plain text your problem ;)
Mindiell
----

andbal

  • Newbie
  • *
  • Posts: 3
    • View Profile
my first network program
« Reply #5 on: September 16, 2010, 01:16:44 pm »
Im having kinda same problem. Client(s) sends packets (every 100ms), server receives them normally and sends back to the client(s) normally but client(s) receives them with delay (there are no timers on server send / client receive).

Packets are not being missed, they arrive but with a great delay.. Even if some packets were lost, it wouldnt be a big deal. Main goal is to receive packets from the server as fast as possible.

In that pic he posted u can see some clients and a server exchanging packets. Problem is the one I described previously - server is already receiving/sending ~134th packet to clients but the last packet clients received is ~80-90th.

speedtest.net results are ping 2ms, dl 65.51Mb, ul 75.86Mb :

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
my first network program
« Reply #6 on: September 17, 2010, 12:10:27 am »
Quote from: "andbal"

In that pic he posted u can see some clients and a server exchanging packets. Problem is the one I described previously - server is already receiving/sending ~134th packet to clients but the last packet clients received is ~80-90th.


yup thats the problem..
someone help please?! ;(

andbal

  • Newbie
  • *
  • Posts: 3
    • View Profile
my first network program
« Reply #7 on: September 19, 2010, 11:36:15 am »
Hello ladies and gentlemen!

Could you please give us some ideas how to solve this little delay problem (or where could hide the problem that causes it)?

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
my first network program
« Reply #8 on: September 20, 2010, 06:13:12 pm »
feel free to say anything, people   :?

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
my first network program
« Reply #9 on: September 21, 2010, 09:02:36 am »
Hmmm
Downloading SFML 1.7 to test your code...
Mindiell
----

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
my first network program
« Reply #10 on: September 23, 2010, 09:07:09 am »
Tested...
Nothing seems really annoying here. Numbers are increasing correctly.

What I could suggest is some modification in the Server code :
Code: [Select]
           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;

                        //Sending only information changed
                        Packet.Clear();
                        Packet << n << x << y;
                        break;
                    }
                }

                //Sending to connected clients
                for (int n=0; n<max_clients; n++)
                {
                    if (client[n].ip!="0")
                    {
                        Server.Send(Packet,client[n].ip, client[n].port);
                    }
                }
            }
This will send only modified information only when the information changed and not resending all the information every time something may have changed. You could even add a test verifying that x or y has been effectively modified.

In the client code :
Code: [Select]
if(Clock.GetElapsedTime()>0.3)
        {
            Clock.Reset();
            y ++;
            Packet.Clear();
            Packet << x << y;
            Client.Send(Packet, ServerAddress, 2323);
        }
Nothing really important, but you can use the clock as this. No need to keep a Timer object ;)
Mindiell
----