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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - domin568

Pages: [1]
1
Network / Re: UDP Chat doesn't work - cannot to send message.
« on: December 08, 2013, 10:58:43 pm »
I think That if i want to receive data on client i have to bind on port wchich can receive data. If i delete thread receive_data on client this app works, but only for sending data from client to server, but not from server to client. Problem is thread, but i dont know why it doesnt work, Because when i have created a TCP chat, thread worked great. // Razzorflame are you from cpp0x.pl ??

2
Network / Re: UDP Chat doesn't work - cannot to send message.
« on: December 08, 2013, 11:58:07 am »
Anybody can help me :) ??

3
Network / UDP Chat doesn't work - cannot to send message.
« on: December 01, 2013, 03:24:38 pm »
Hello i created simple chat using UDP, i have 2 applications : Client and Server. Only difference between them are another port to - send and - receive. When i run 2 applications, and I want to send message i get a message : Unsuccesfull data send like I write in code ;/ Why this code doesn't work ?
CLIENT :
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
sf::Packet packet_send;
sf::Packet packet_receive;
char data_send[100];
char data_receive[100];
sf::UdpSocket socketUDP;
sf::IpAddress local_host = "127.0.0.1";
unsigned short port_receive = 5000;
unsigned short port_send = 5001;
void receive_data()
{
        bool wyjscie = false;
        while (wyjscie != true)
        {
                if (socketUDP.receive(packet_receive, local_host, port_receive) == sf::Socket::Done)
                {
                        cout << "Receive data" << endl;
                        packet_receive >> data_receive;
                        cout << data_receive << endl;
                }
        }
}
int _tmain(int argc, _TCHAR* argv[])
{
        cout << "Client" << endl;

        if (socketUDP.bind(port_receive) == sf::Socket::Done)
        {
                cout << "Binded for port : " << port_receive << endl;
        }
        sf::Thread receive_data(&receive_data);
        receive_data.launch();
        while (true)
        {
                cin.getline(data_receive, 100);
                packet_send.clear();
                packet_send << data_send;
                if (socketUDP.send(packet_send, local_host, port_send) == sf::Socket::Done)
                {
                        cout << "Succesfull send data" << endl;
                }
                else
                {
                        cout << "Unsuccesfull data send" << endl;
                }

        }
        return 0;
}
SERVRER:
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
sf::Packet packet_send;
sf::Packet packet_receive;
char data_send[100];
char data_receive[100];
sf::UdpSocket socketUDP;
sf::IpAddress local_host = "127.0.0.1";
unsigned short port_receive = 5001;
unsigned short port_send = 5000;
void receive_data()
{
        bool wyjscie = false;
        while (wyjscie != true)
        {
                if (socketUDP.receive(packet_receive, local_host, port_receive) == sf::Socket::Done)
                {
                        cout << "Receive data" << endl;
                        packet_receive >> data_receive;
                        cout << data_receive << endl;
                }
        }
}
int _tmain(int argc, _TCHAR* argv[])
{
        cout << "Server" << endl;

        if ( socketUDP.bind(port_receive)== sf::Socket::Done )
        {
                cout << "Binded for port : " << port_receive << endl;
        }
        sf::Thread receive_data(&receive_data);
        receive_data.launch();
        while (true)
        {
                cin.getline(data_receive,100);
                packet_send.clear();
                packet_send << data_send;
                if (socketUDP.send(packet_send, local_host, port_send) == sf::Socket::Done)
                {
                        cout << "Succesfull send data" << endl;
                }
                else
                {
                        cout << "Unsuccesfull data send" << endl;
                }
       
        }
        return 0;
}
 

4
Thanks Jebbs , now my application works :), really stupid error. I will write my next codes in English

5
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;
}
 

6
Network / Re: Problems with simple chat program.
« on: November 06, 2013, 08:43:36 pm »
I solved problem, just I didn't empty data_send before save it to packet.

        while (running)
        {
        pakiet_send.clear () ; // Very Important line :)
        cin.getline(data_send,100);
        }
 
Thanks TheRabbit you solved my problem which I wanted to solve 2 days :)
       

7
Network / Re: Problems with simple chat program.
« on: November 06, 2013, 08:12:25 pm »
I deleted non-blocking socket , and I change a line with receive function to :
    if ( socket.receive(pakiet_receive)==sf::Socket::Done)
        {
 

And as I can see status is always "done" because client write old message, so receive function works (but not as I want :D ). Anybody can help me ?? :)

8
Network / Problems with simple chat program.
« on: November 06, 2013, 05:21:47 pm »
Hello, I made a simple chat using tutorials on SFML-page. I created separate thread to receive data. Sending from client to server is not possible, but if I want to send from server to client this is  possible only first time , next times client receive old message (e.g from server i send "Hello", client receive this message , send next time but another text e.g "Hello123" , and client show "Hello" again .
Here is code
client :
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>
#include <cstdlib>
using namespace std;

sf::TcpSocket socket ;
sf::Mutex globalMutex;
void receive_and_show_data ()
{
        static string oldMsg;
        while (true)
        {
       
        string msg;
        sf::Packet pakiet_receive;
        socket.setBlocking (false);
    socket.receive(pakiet_receive);
       
                if(pakiet_receive >> msg)
                {
                                if(!msg.empty())
                                {
                                        std::cout << msg << std::endl;
                                        oldMsg = msg;
                                }
                }
        }
}


int main(int argc, char* argv[])
{
       
        cout << "Client" << endl ;
        char data_send [100];
        bool quit = true ;

        cout << "Write a IP adress : " << endl ;
        string adres_ip_string ;
        cin >> adres_ip_string ;
       
        sf::Packet pakiet_send ;

        sf::Socket::Status status = socket.connect (adres_ip_string,5000);
       
        if (status != sf::Socket::Done )
        {
                cout << "Nie mozna polaczyc " << endl ;
        }
        else if (status == sf::Socket::Done)
        {
                cout << "Connected" << endl ;
                quit = false ;
                sf::Thread receiveThread (&receive_and_show_data);
                receiveThread.launch () ;
                while (quit == false)
                {
                cin.getline(data_send,100);

                pakiet_send << data_send ;
                globalMutex.lock();
                if (socket.send (pakiet_send) != sf::Socket::Done )
                {
                        cout << "Nie udalo sie wyslac danych " << endl ;
                }      
                globalMutex.unlock();



                }
                }
        return 0;
}
Server :
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <string>
#include <iostream>

using namespace std ;
const short PORT = 5000 ;
sf::TcpSocket socket;
sf::Mutex globalMutex;
void receive_show_data ()
{
        static string oldMsg;
        while (true)
        {
       
        string msg;
        sf::Packet pakiet_receive;
    socket.receive(pakiet_receive) ;

                if(pakiet_receive >> msg)
                {
                        if(oldMsg != msg)
                                if(!msg.empty())
                                {
                                        std::cout << msg << std::endl;
                                        oldMsg = msg;
                                }
                }
       
        }
}

int main(int argc, char* argv[])
{
       
        char data_send[100];
        bool running = true ;

        sf::TcpListener listener ;
        listener.listen (PORT);
        sf::Packet pakiet_send ;
        if ( listener.accept (socket) == sf::Socket::Done )
        {
                cout << "Connected with : " << socket.getRemoteAddress() << endl ;
        }
       
        sf::Thread receiveThread (&receive_show_data);
        receiveThread.launch () ;
        while (running)
        {

        cin.getline(data_send,100);
        pakiet_send << data_send ;
       
        if (socket.send (pakiet_send) != sf::Socket::Done )
        {
                cout << "Nie udalo sie wyslac danych " << endl ;
        }



        }
        return 0;
}
 
Thanks for possible reply  :)

Pages: [1]
anything