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

Author Topic: Probleme with TcpSocket::Receive  (Read 2976 times)

0 Members and 1 Guest are viewing this topic.

Mourtaza

  • Newbie
  • *
  • Posts: 8
    • View Profile
Probleme with TcpSocket::Receive
« on: March 01, 2012, 11:11:03 pm »
Hello everybody, I am coding one simple application and I have a problem with this code :

Code: [Select]
#include<SFML/Graphics.hpp>
#include<SFML/Network.hpp>
#include<iostream>
#include<string>
#include"Naruto.h"
    using namespace sf;
    using namespace std;
int main(void)
{
    RenderWindow app(VideoMode(800,600,32),"Naruto");
    TcpSocket mySocket;
        bool connexion(true);
        while(connexion)
        {
            if(mySocket.Connect("192.168.1.176",5000) == Socket::Done)
                connexion = false;
        }
        Naruto naruto("Image/NarutoNormal.bmp");
            naruto.changerPosition(400,100);

        Naruto sakura("Image/SakuraNormal.bmp");
            sakura.changerPosition(400,400);

        Texture terrTxt;
            terrTxt.LoadFromFile("Image/terrain.bmp");
                Sprite terrain;
                    terrain.SetTexture(terrTxt);

    while(app.IsOpen())
    {
        Event event;
            while(app.PollEvent(event))
            {
                if(event.Type == Event::Closed)
                    app.Close();
                Packet paquet;

                if(Keyboard::IsKeyPressed(Keyboard::Up))
                {
                    naruto.avancer();
                    paquet << "haut";
                    mySocket.Send(paquet);
                    paquet.Clear();

                }
                if(Keyboard::IsKeyPressed(Keyboard::Down))
                {
                    naruto.reculer();
                    paquet << "bas";
                    mySocket.Send(paquet);
                    paquet.Clear();

                }
                if(Keyboard::IsKeyPressed(Keyboard::Left))
                {
                    naruto.allerAGauche();
                    paquet << "gauche";
                    mySocket.Send(paquet);
                    paquet.Clear();
                }
                if(Keyboard::IsKeyPressed(Keyboard::Right))
                {
                    naruto.allerADroite();
                    paquet << "droite";
                    mySocket.Send(paquet);
                    paquet.Clear();
                }
                string myText("");
                if(mySocket.Receive(paquet))
                {
                    paquet >> myText;
                }

            }
        app.Clear();
        app.Draw(terrain);
        app.Draw(naruto.getPersonnage());
        app.Draw(sakura.getPersonnage());
        app.Display();
    }
    return EXIT_SUCCESS;
}


The probleme is at the reception of a packet just before clearing the window etc...
My window is always "transparent" , it did not display what he does.
I think, and I know that there is a problem in the reception of a packet, but what ? Even if i don't write anything in this condition.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Probleme with TcpSocket::Receive
« Reply #1 on: March 02, 2012, 08:10:05 am »
Quote
if(mySocket.Receive(paquet))

Check the doc, Receive doesn't return a boolean.
Laurent Gomila - SFML developer

Mourtaza

  • Newbie
  • *
  • Posts: 8
    • View Profile
Probleme with TcpSocket::Receive
« Reply #2 on: March 02, 2012, 12:18:44 pm »
I tried with

Code: [Select]
if(mySocket.Receive(paquet) == Socket::Done)

but it's there's the same problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Probleme with TcpSocket::Receive
« Reply #3 on: March 02, 2012, 12:34:08 pm »
Quote
but it's there's the same problem

Yeah, you're not doing anything with myText so whether this code is correct or not doesn't make any difference.

But do you actually receive something, or is the program blocked on the Receive call? What happens exactly?
Laurent Gomila - SFML developer

Mourtaza

  • Newbie
  • *
  • Posts: 8
    • View Profile
Probleme with TcpSocket::Receive
« Reply #4 on: March 02, 2012, 02:16:46 pm »
I think

Code: [Select]
app.Clear();
.....
app.Display();
is never call. If I don't write this condition :

Code: [Select]
if(mySocket.Receive(paquet) == Socket::Done)
{
......
}


There is no problem to move my character and display the window etc..
But if I don't write it, I can"t move the other character by receiving packet from my server.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Probleme with TcpSocket::Receive
« Reply #5 on: March 02, 2012, 03:00:56 pm »
Receive is blocking, it won't return until it receives something. So it means that your program never receives anything from the network, or receives something but at a very low rate.

Calling a blocking Receive function in the main loop is not recommended because it will prevent your program from running at its normal rate. You should either use a thread or non-blocking sockets.
Laurent Gomila - SFML developer

Mourtaza

  • Newbie
  • *
  • Posts: 8
    • View Profile
Probleme with TcpSocket::Receive
« Reply #6 on: March 02, 2012, 05:33:49 pm »
non blocking-socket = UDP Socket ?

I was just trying to do it in a Thread, but my socket is not declared in this thread, so I try to write in the parameters of my thread, as a reference or not. But I have an error which says that the constructor of NonCopyable is private.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Probleme with TcpSocket::Receive
« Reply #7 on: March 02, 2012, 06:31:26 pm »
Quote
non blocking-socket = UDP Socket ?

Read the doc / tutorials ;)
Code: [Select]
socket.SetBlocking(false);

Quote
I was just trying to do it in a Thread, but my socket is not declared in this thread, so I try to write in the parameters of my thread, as a reference or not. But I have an error which says that the constructor of NonCopyable is private.

You must pass it as a pointer to avoid problems.
Laurent Gomila - SFML developer

Mourtaza

  • Newbie
  • *
  • Posts: 8
    • View Profile
Probleme with TcpSocket::Receive
« Reply #8 on: March 02, 2012, 07:07:19 pm »
Thanks, I find it by the way, finally I suceed, my windows are displayed etc.. all it's ok but it don't work good if I press 2 keys at the same time, just one key is "seen" by the server, is it normal ? Should I post the code ?