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

Author Topic: Packet problem  (Read 3580 times)

0 Members and 1 Guest are viewing this topic.

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Packet problem
« on: September 16, 2009, 07:02:43 am »
Is this code correct? Don't know whats wrong... :(
Code: [Select]

//Server
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;

int main()
{
    while(1)//Holding the server open for new connection
    {
        sf::SocketTCP Listener;
        if(!Listener.Listen(2304))
        {
        cout << "Unable to listen to the given Port 2304!" << endl;
        }

        sf::SocketTCP Client;
        sf::IPAddress ClientAddress;
        if(Listener.Accept(Client, &ClientAddress) != sf::Socket::Done)
        {
        cout << "Some error occured.Clients are unable to connect to the Server" << endl;
        }

        sf::Packet Packet;
        std::string Order;
        while(1)
        {
            if(Client.Receive(Packet) == sf::Socket::Disconnected)
        {
            cout << "The client disconnected" << endl;
            break;
            }
        Client.Receive(Packet);
        Packet >> Order;

        cout << ClientAddress << ": " << Order << endl;
        if(Order == "Server.Close"){break;}
        }
        if(Order == "Server.Close"){Listener.Close();Client.Close();}
        //Else: Restart Server
    }
   
}
//Client
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;

int main()
{
    sf::SocketTCP Client;
    if(Client.Connect(2304,"192.168.178.22") != sf::Socket::Done)
    {
    cout << "Unable to connect to the server!" << endl;
    }
   
    std::string Order;
    sf::Packet Packet;
    while(1)
    {
    cout << ">> ";
    cin >> Order;

    Packet << Order;
    Client.Send(Packet);
    if(Order == "Client.Close"){break;}
    }
    Client.Close();
}


The first Packet send isn' displayed at the server...if I send a second Package, the first is displayed at the server and after any packet I send isn't displayed :(
Hope you can help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Packet problem
« Reply #1 on: September 16, 2009, 09:08:14 am »
Hi

1- The client never clears its packet, messages are accumulated in it and you always extract the first one on the server. You must either call Packet.Clear(), or declaring it inside your loop so that you use a new clean instance at every iteration.

2- The server calls Receive twice, so it always receives two packets but only processes the last one.
Laurent Gomila - SFML developer

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Packet problem
« Reply #2 on: September 16, 2009, 04:54:44 pm »
Thank you!Works perfect!  :roll: