SFML community forums

Help => Network => Topic started by: lohr on June 30, 2008, 11:38:57 am

Title: Packet Send and Receive
Post by: lohr on June 30, 2008, 11:38:57 am
Hello,

I want to send and receive a packet with the following structure:

Code: [Select]
struct Position
{
    int     x;
    int y;
int rot;
};


now thats my server Init:

Code: [Select]
sf::SocketTCP Server;

    if (!Server.Listen(1000))
        return EXIT_FAILURE;

    sf::IPAddress ClientAddress;
    sf::SocketTCP Client;
    Server.Accept(Client, &ClientAddress);

sf::Packet RegularPacket;
Position P;


and thats my client Init:

Code: [Select]
sf::IPAddress ServerAddress = sf::IPAddress::GetLocalAddress();

    sf::SocketTCP Client;

    if (!Client.Connect(1000, ServerAddress))
        return EXIT_FAILURE;

sf::Packet RegularPacket;
Position P;



Here is the Receive Part of the Server:

Code: [Select]
if (Client.Receive(RegularPacket) != sf::Socket::Done)
return EXIT_FAILURE;
if (RegularPacket >> P)
{
s_auto.SetPosition(P.x,P.y);
s_auto.SetRotation(P.rot);
}


Here is the Send Part of the Client:

Code: [Select]
P.x = pos.x;
P.y = pos.x;
P.rot = rot;

if (RegularPacket << P)
{
if (Client.Send(RegularPacket) != sf::Socket::Done)
return EXIT_FAILURE;
}

if (Client.Send(RegularPacket) != sf::Socket::Done)
return EXIT_FAILURE;


Its a litte Car Racing game where i am trying to send the position of the client car to the server, but it isnt working...
The Connection is established betweeen them.

Do you find anything wrong?

Best regards,
lohr
Title: Packet Send and Receive
Post by: Laurent on June 30, 2008, 11:43:19 am
Quote
but it isnt working...

Can you give more details please ? ;)
Title: Packet Send and Receive
Post by: workmad3 on June 30, 2008, 12:05:07 pm
Well, you are sending the packet twice... that may be a problem.
Title: Packet Send and Receive
Post by: lohr on June 30, 2008, 12:24:19 pm
Quote from: "Laurent"
Quote
but it isnt working...

Can you give more details please ? ;)


Okay sorry ^^
The first pos and rot info that i am sending always is received by the server, all others after that get lost.

Quote
Well, you are sending the packet twice... that may be a problem.


Hm i tried it with one sending operation as well and it didnĀ“t work.
Title: Packet Send and Receive
Post by: lohr on June 30, 2008, 02:08:45 pm
So it is working...
I forgot to run the Clear() Method for the RegularPacket ^^

Do you have any tips on the fixing of latency problems?