Hello I have such strange problem , I made application with network connection, by TCP sockets, I created another thread to networking. When I begin 2 applications (server,client), in Task Manager I can see "Application don't responding", but through windows aero I can see my crashed applications, and client and server works ! I press W and my shape move up, and even this is sending to opposite application and there I can see that shape was moved on another application. This is strange problem :/ Sorry for my English. Code (some names is from Polish language because it's better for me in recognise a application and I think this is not problem for you because names are not very important)
Server :
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Network.hpp>
#include "Klocek.h"
sf::Packet pakiet_wyslany;
sf::Packet pakiet_odebrany;
sf::Vector2f start_position_odebrane ;
sf::Vector2f klocek_size_odebrane ;
sf::Color kolor_klocka_odebrane ;
float thickness_klocek_odebrane ;
sf::Color kolor_thickness_odebrane ;
sf::Vector2f pozycja_klocka_net;
sf::TcpSocket socket ;
using namespace std;
void poruszajKlockiem (sf::Keyboard::Key klawisz [],klocek & klocek)
{
if (sf::Keyboard::isKeyPressed (klawisz[0])== true )
{
klocek.setPosition (sf::Vector2f (klocek.getPosition().x , klocek.getPosition().y -4) ) ;
}
if (sf::Keyboard::isKeyPressed (klawisz[1])== true )
{
klocek.setPosition (sf::Vector2f (klocek.getPosition().x , klocek.getPosition().y +4) ) ;
}
if (sf::Keyboard::isKeyPressed (klawisz[2])== true )
{
klocek.setPosition (sf::Vector2f (klocek.getPosition().x -4 , klocek.getPosition().y ) ) ;
}
if (sf::Keyboard::isKeyPressed (klawisz[3])== true )
{
klocek.setPosition (sf::Vector2f (klocek.getPosition().x +4 , klocek.getPosition().y ) ) ;
}
}
void komunikacja_sieciowa (klocek & klocek)
{
while (true)
{
pakiet_wyslany.clear () ;
pakiet_odebrany.clear () ;
pakiet_wyslany << klocek.getPosition().x << klocek.getPosition().y ;
if (socket.send (pakiet_wyslany) != sf::Socket::Done )
{
cout << "Nie udalo sie wyslac " << endl ;
}
if (socket.receive (pakiet_odebrany) == sf::Socket::Done )
{
pakiet_odebrany >> pozycja_klocka_net.x >> pozycja_klocka_net.y ;
}
}
}
void aktualizuj_pozycje_kloceknet (klocek & klocek_net)
{
klocek_net.setPosition (pozycja_klocka_net);
}
int main(int argc, char* argv[])
{
cout << "SERVER" << endl ;
sf::TcpListener listener;
listener.listen (5000);
sf::RenderWindow okno(sf::VideoMode (800,600,32),"Klocki");
okno.setFramerateLimit (60);
sf::Vector2f start_position (300,300);
sf::Vector2f klocek_size (50,50);
sf::Color kolor_klocka (255,0,0);
float thickness_klocek = 10 ;
sf::Color kolor_thickness (0,255,255);
pakiet_wyslany << start_position.x << start_position.y << klocek_size.x << klocek_size.y << kolor_klocka.r << kolor_klocka.g << kolor_klocka.b
<< thickness_klocek << kolor_thickness.r << kolor_thickness.g << kolor_thickness.b ;
if (listener.accept (socket)==true)
{
cout << "Polaczono" << endl ;
}
if ( socket.send (pakiet_wyslany) != sf::Socket::Done )
{
cout << "Unable to send data " << endl ;
}
if (socket.receive(pakiet_odebrany) == sf::Socket::Done )
{
pakiet_odebrany >> start_position_odebrane.x >> start_position_odebrane.y >> klocek_size_odebrane.x >> klocek_size_odebrane.y >> kolor_klocka_odebrane.r >> kolor_klocka_odebrane.g >> kolor_klocka_odebrane.b
>> thickness_klocek_odebrane >> kolor_thickness_odebrane.r >> kolor_thickness_odebrane.g >> kolor_thickness_odebrane.b ;
}
klocek klocek_net (start_position_odebrane,klocek_size_odebrane.x,klocek_size_odebrane.y,kolor_klocka_odebrane,thickness_klocek_odebrane,kolor_thickness_odebrane );
klocek klocek (start_position,klocek_size.x,klocek_size.y,kolor_klocka,thickness_klocek,kolor_thickness);
sf::Thread watek_net (&komunikacja_sieciowa,klocek) ;
watek_net.launch ();
sf::Keyboard::Key klawisze [] = {sf::Keyboard::W,sf::Keyboard::S,sf::Keyboard::A,sf::Keyboard::D};
while (okno.isOpen ())
{
aktualizuj_pozycje_kloceknet (klocek_net);
poruszajKlockiem (klawisze,klocek);
okno.display();
okno.clear();
klocek_net.drawKlocek(okno);
klocek.drawKlocek(okno);
}
return 0;
}
client
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Network.hpp>
#include "Klocek.h"
sf::Packet pakiet_wyslany;
sf::Packet pakiet_odebrany;
sf::Vector2f start_position_odebrane ;
sf::Vector2f klocek_size_odebrane ;
sf::Color kolor_klocka_odebrane ;
float thickness_klocek_odebrane ;
sf::Color kolor_thickness_odebrane ;
sf::Vector2f pozycja_klocka_net;
sf::TcpSocket socket ;
using namespace std;
void poruszajKlockiem (sf::Keyboard::Key klawisz [],klocek & klocek)
{
if (sf::Keyboard::isKeyPressed (klawisz[0])== true )
{
klocek.setPosition (sf::Vector2f (klocek.getPosition().x , klocek.getPosition().y -4) ) ;
}
if (sf::Keyboard::isKeyPressed (klawisz[1])== true )
{
klocek.setPosition (sf::Vector2f (klocek.getPosition().x , klocek.getPosition().y +4) ) ;
}
if (sf::Keyboard::isKeyPressed (klawisz[2])== true )
{
klocek.setPosition (sf::Vector2f (klocek.getPosition().x -4 , klocek.getPosition().y ) ) ;
}
if (sf::Keyboard::isKeyPressed (klawisz[3])== true )
{
klocek.setPosition (sf::Vector2f (klocek.getPosition().x +4 , klocek.getPosition().y ) ) ;
}
}
void komunikacja_sieciowa (klocek & klocek)
{
while (true)
{
pakiet_wyslany.clear () ;
pakiet_odebrany.clear () ;
pakiet_wyslany << klocek.getPosition().x << klocek.getPosition().y ;
if (socket.send (pakiet_wyslany) != sf::Socket::Done )
{
cout << "Nie udalo sie wyslac " << endl ;
}
if (socket.receive (pakiet_odebrany) == sf::Socket::Done )
{
pakiet_odebrany >> pozycja_klocka_net.x >> pozycja_klocka_net.y ;
}
}
}
void aktualizuj_pozycje_kloceknet (klocek & klocek_net)
{
klocek_net.setPosition (pozycja_klocka_net);
}
int main(int argc, char* argv[])
{
sf::Vector2f start_position (300,300);
sf::Vector2f klocek_size (50,50);
sf::Color kolor_klocka (255,0,0);
float thickness_klocek = 10 ;
sf::Color kolor_thickness (0,0,255);
if (socket.connect ("127.0.1",5000)==sf::Socket::Done)
{
cout << "Polaczono " << endl ;
}
if (socket.receive(pakiet_odebrany) == sf::Socket::Done )
{
pakiet_odebrany >> start_position_odebrane.x >> start_position_odebrane.y >> klocek_size_odebrane.x >> klocek_size_odebrane.y >> kolor_klocka_odebrane.r >> kolor_klocka_odebrane.g >> kolor_klocka_odebrane.b
>> thickness_klocek_odebrane >> kolor_thickness_odebrane.r >> kolor_thickness_odebrane.g >> kolor_thickness_odebrane.b ;
}
klocek klocek_net (start_position_odebrane,klocek_size_odebrane.x,klocek_size_odebrane.y,kolor_klocka_odebrane,thickness_klocek_odebrane,kolor_thickness_odebrane );
pakiet_wyslany << start_position.x << start_position.y << klocek_size.x << klocek_size.y << kolor_klocka.r << kolor_klocka.g << kolor_klocka.b
<< thickness_klocek << kolor_thickness.r << kolor_thickness.g << kolor_thickness.b ;
if ( socket.send (pakiet_wyslany) != sf::Socket::Done )
{
cout << "Unable to send data " << endl ;
}
klocek klocek (start_position,klocek_size.x,klocek_size.y,kolor_klocka,thickness_klocek,kolor_thickness);
sf::Thread watek_net (&komunikacja_sieciowa,klocek) ;
watek_net.launch ();
sf::RenderWindow okno(sf::VideoMode (800,600,32),"Zderzejace sie klocki");
okno.setFramerateLimit (60);
sf::Keyboard::Key klawisze [] = {sf::Keyboard::W,sf::Keyboard::S,sf::Keyboard::A,sf::Keyboard::D};
while (okno.isOpen ())
{
aktualizuj_pozycje_kloceknet (klocek_net);
poruszajKlockiem (klawisze,klocek);
okno.display();
okno.clear();
klocek_net.drawKlocek(okno);
klocek.drawKlocek(okno);
}
return 0;
}