simple chat in console application does not send data



  • Hi.
    I make a simple chat program. When I wrote a single socket.write("bla bla "); in client and server and it sends very well. then I change that code to this code. but it does not send any thing(I mean the server does not read)
    .pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2016-08-01T10:49:07
    #
    #-------------------------------------------------
    
    QT       += core
    QT       += network
    QT       -= gui
    
    TARGET = QServer
    CONFIG   += console
    CONFIG   -= app_bundle
    
    TEMPLATE = app
    
    
    SOURCES += main.cpp \
        tcpserver.cpp
    
    HEADERS += \
        tcpserver.h
    
    

    TCPServer.h

    
    #ifndef TCPSERVER_H
    #define TCPSERVER_H
    #include <QObject>
    #include <QDebug>
    #include <QTcpSocket>
    #include <QTcpServer>
    
    class TCPServer : public QObject
    {
        Q_OBJECT
    public:
        explicit TCPServer(QObject *parent = 0);
        ~TCPServer();
    signals:
    
    public slots:
        void newConnection();
        void disconnected();
        void readyRead();
        void writeData(QString data);
    
    private:
        QTcpServer *serv;
        QTcpSocket *socket;
    };
    #endif // TCPSERVER_H
    
    

    TCPServer.cpp

    
    #include "tcpserver.h"
    
    TCPServer::TCPServer(QObject *parent) : QObject(parent)
    {
        serv=new QTcpServer(this);
        connect(serv,SIGNAL(newConnection()),this,SLOT(newConnection()));
        if(!serv->listen(QHostAddress::Any,3490))
        {
            qDebug()<<"Server Error in Connecting...."<<serv->errorString();
        }
        else
            qDebug()<<"Server Started     :)     ";
    }
    
    TCPServer::~TCPServer()
    {
        disconnected();
    }
    
    void TCPServer::newConnection()
    {
        socket=serv->nextPendingConnection();
        connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
        socket->write("Hello Client");
        socket->flush();//clean the buffer
        socket->waitForReadyRead(3000);
       // sock->close();
    }
    
    void TCPServer::disconnected()
    {
        socket->close();
        qDebug()<<"Good By     :)     ";
    }
    
    void TCPServer::readyRead()
    {
        //qDebug()<<"Server is Reading ...."<<socket->readAll();
        while(socket->canReadLine())
        {
            QByteArray ba = socket->readLine();
            if(strcmp(ba.constData(), "!exit\n") == 0)
            {
                socket->disconnectFromHost();
                break;
            }
            //printf(">> %s", ba.constData());
            qDebug() << "Server is receiving ...."<<ba.constData();
        }
    }
    
    
    void TCPServer::writeData(QString data)
    {
        socket->write(data.toStdString().c_str());
        socket->flush();//clean the buffer
        socket->waitForReadyRead(3000);
    }
    
    
    

    TCPServer/main.cpp

    
    #include <QCoreApplication>
    #include "tcpserver.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        TCPServer srv;
    //    while(1)
    //    {
            QTextStream s(stdin);
            QString value = s.readLine();
            srv.writeData(value);
    //    }
    
        return a.exec();
    }
    

    And the client code :
    .pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2016-08-01T10:48:33
    #
    #-------------------------------------------------
    
    QT       += core
    QT       += network
    QT       -= gui
    
    TARGET = QClient
    CONFIG   += console
    CONFIG   -= app_bundle
    
    TEMPLATE = app
    
    
    SOURCES += main.cpp \
        tcpclient.cpp
    
    HEADERS += \
        tcpclient.h
    

    TCPClient.h

    
    #ifndef TCPCLIENT_H
    #define TCPCLIENT_H
    
    #include <QObject>
    #include <QDebug>
    #include <QTcpSocket>
    #include <QAbstractSocket>
    
    
    class TCPclient : public QObject
    {
        Q_OBJECT
    public:
        explicit TCPclient(QObject *parent = 0);
        ~TCPclient();
    
    signals:
    
    
    public slots:
        void connected();
        void disconnected(); 
        void readyRead();
        void writeData(QString data);
    
    
    private:
        QTcpSocket *socket;
    };
    
    #endif // TCPCLIENT_H
    
    

    TCPClient.cpp

    #include "tcpclient.h"
    
    TCPclient::TCPclient(QObject *parent) : QObject(parent)
    {
        socket=new QTcpSocket(this);
        connect(socket,SIGNAL(connected()),this,SLOT(connected()));
        connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
        //connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWritten(qint64)));
        qDebug() << "Connecting......";
        socket->connectToHost("192.168.101.4",3490);
        //socket->connectToHost("voidrealm.com",80);
        if(!socket->waitForConnected(1000))
            qDebug()<<"Error occured......"<<socket->errorString();
    }
    
    TCPclient::~TCPclient()
    {
        disconnected();
    }
    
    void TCPclient::connected()
    {
        qDebug()<<"socketet is Connecting     :)      ";
        //socket->write("Hello server");
    }
    
    void TCPclient::disconnected()
    {
        qDebug()<<"socketet is closing.        :*      ";
        socket->close();
    }
    
    void TCPclient::readyRead()
    {
        //qDebug()<<"Client is Reading ...."<<socket->readAll();
        while(socket->canReadLine())
        {
            QByteArray ba = socket->readLine();
            if(strcmp(ba.constData(), "!exit\n") == 0)
            {
                socket->disconnectFromHost();
                break;
            }
            //printf(">> %s", ba.constData());
            qDebug() << "Client is receiving ...."<<ba.constData();
        }
    }
    
    void TCPclient::writeData(QString data)
    {
        socket->write(data.toStdString().c_str());
        socket->flush();//clean the buffer
        socket->waitForReadyRead(3000);
    }
    

    TCPClient/main.cpp

    #include <QCoreApplication>
    #include "tcpclient.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        TCPclient cln;
    
    //    while(1)
    //    {
            QTextStream s(stdin);
            QString value = s.readLine();
            cln.writeData(value);
    //    }
        return a.exec();
    }
    

    why server does not receive anything?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.