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

Author Topic: SFML Window and Packet frozen  (Read 3515 times)

0 Members and 1 Guest are viewing this topic.

BoingBoing

  • Newbie
  • *
  • Posts: 17
    • View Profile
SFML Window and Packet frozen
« on: January 11, 2010, 04:16:57 am »
I could receive packets and send them but the SFML window just freezes.
Code: [Select]
#ifdef _WIN32
//#include <windows.h>
#endif

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Network.hpp>
#include <sys/timeb.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

sf::Clock Clock;
sf::RenderWindow App;

float CurTime( void )
{
return Clock.GetElapsedTime();
}

void Initialize();
void Draw();
void Think();
void KeyPress(sf::Key::Code Key);
void KeyReleased(sf::Key::Code Key);
void OnRecieve(const char* Message);
float NextThink = 0;
sf::SocketTCP Client;

int main()
{
App.Create(sf::VideoMode(640, 480), "MMOClient v0.1");

ifstream ipfile("ip.txt",ios::in);
    sf::IPAddress ServerAddress;
ipfile >> ServerAddress;
ipfile.close();
const unsigned short Port = 2435;
if (Client.Connect(Port,ServerAddress) == sf::Socket::Done)
{
std::cout << "Connected to server " << ServerAddress << std::endl;
} else {
App.Close();
}

//Initialize();
bool isrunning = true;
while (isrunning)
{
sf::Packet p;
std::string s;
if (Client.Receive(p) == sf::Socket::Done)
{
p >> s;
std::cout << s << std::endl;
}
char ToSend[] = "w-off";
Client.Send(ToSend, sizeof(ToSend));
App.Clear();
Draw();
App.Display();
}
    /*while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
if (Event.Type == sf::Event::KeyPressed)
KeyPress(Event.Key.Code);
if (Event.Type == sf::Event::KeyReleased)
KeyReleased(Event.Key.Code);
        }

//if (Client.Receive(Message, sizeof(Message), Received))
//{
//OnRecieve(Message);
//}
if (NextThink < CurTime())
{
NextThink = CurTime() + 0.02;
Think();
}
App.Clear();
Draw();
App.Display();
    }*/
Client.Close();
    return EXIT_SUCCESS;
}
I provide a robust framework.
In my a-

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML Window and Packet frozen
« Reply #1 on: January 11, 2010, 08:25:21 am »
Socket functions are blocking by default, which means that if there's nothing to receive your application will freeze until data arrives. You should either use a separate thread to handle your sockets, or use non-blocking sockets.
Laurent Gomila - SFML developer

BoingBoing

  • Newbie
  • *
  • Posts: 17
    • View Profile
SFML Window and Packet frozen
« Reply #2 on: January 11, 2010, 09:22:41 am »
I'm a newbie, do you got any tutorials or examples on how to do multithreading?
I provide a robust framework.
In my a-

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SFML Window and Packet frozen
« Reply #3 on: January 11, 2010, 09:25:57 am »
There are tutorials on this site :)
http://www.sfml-dev.org/tutorials/1.5/
Laurent Gomila - SFML developer