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

Author Topic: Problems with simple chat program.  (Read 3018 times)

0 Members and 1 Guest are viewing this topic.

domin568

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
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  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problems with simple chat program.
« Reply #1 on: November 06, 2013, 06:42:29 pm »
Don't use non-blocking sockets when not necessary.
Check the status returned by the receive function.
Laurent Gomila - SFML developer

domin568

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Problems with simple chat program.
« Reply #2 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 ?? :)

TheRabbit

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Problems with simple chat program.
« Reply #3 on: November 06, 2013, 08:26:59 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 ?? :)

Try emptying the packet before putting the new string back into it, both on  client and server.

domin568

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Problems with simple chat program.
« Reply #4 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 :)
       
« Last Edit: November 06, 2013, 08:58:24 pm by domin568 »

TheRabbit

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Problems with simple chat program.
« Reply #5 on: November 06, 2013, 09:07:26 pm »
Do you get each message exactly twice, or does it just keep repeating the very first message forever?

Try changing [code =cpp] socket.receive(pakiet_receive);[/code] to
 if (socket.receive(pakiet_receive) == sf::Socket::Done)
on the client code.

 

anything