TCP chat



  • To juz kolejny problem z TCP w QT.
    Napisalem sobie prymitywna aplikacje chatu, jednak na serwerze kiedy chce wyslac wiadomosc pokazuje sie error i zamyka aplikacje. Niewiem jak to naprawic, na kliencie bo wcisnieciu buttona tekst znika i nie ma zadnego bledu.

    To moje kody zrodlowe:
    server.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2016-01-17T12:45:39
    #
    #-------------------------------------------------
    
    QT       += core gui
    QT       += network
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = server
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            server.cpp
    
    HEADERS  += server.h
    
    FORMS    += server.ui
    
    

    server.h

    #ifndef SERVER_H
    #define SERVER_H
    
    #include <QMainWindow>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QHostAddress>
    #include <QList>
    
    namespace Ui {
    class server;
    }
    
    class server : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit server(QWidget *parent = 0);
        ~server();
    
    public slots:
        void connect_new();
        void leer_socketclient();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::server *ui;
        QTcpServer *tcpserver;
        QTcpSocket *tcpclient;
    };
    
    #endif // SERVER_H
    
    

    server.cpp

    #include "server.h"
    #include "ui_server.h"
    
    server::server(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::server)
    {
        ui->setupUi(this);
    
        tcpserver = new QTcpServer (this);
        //tcpserver->setMaxPendingConnections();
    
        tcpserver->listen(QHostAddress::LocalHost, 1234);
        connect(tcpserver, SIGNAL(newConnection()), this, SLOT(connect_new()));
    }
    
    server::~server()
    {
        delete ui;
    }
    
    void server::connect_new()
    {
        //tcpclient = tcpserver->nextPendingConnection();
        connect(tcpclient, SIGNAL(readyRead()), this, SLOT(leer_socketclient()));
    }
    
    void server::leer_socketclient()
    {
        QByteArray buffer;
        buffer.resize( tcpclient->bytesAvailable() );
        tcpclient->read( buffer.data(), buffer.size() );
        ui->plainTextEdit->setReadOnly( true );
        ui->plainTextEdit->appendPlainText( QString (buffer));
    }
    
    void server::on_pushButton_clicked()
    {
        tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
        ui->lineEdit->clear();
    }
    
    

    main.cpp

    #include "server.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        server w;
        w.show();
    
        return a.exec();
    }
    
    

    ============================================================================
    client.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2016-01-17T12:22:05
    #
    #-------------------------------------------------
    
    QT       += core gui
    QT       += network
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = client
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            client.cpp
    
    HEADERS  += client.h
    
    FORMS    += client.ui
    
    

    client.h

    #ifndef CLIENT_H
    #define CLIENT_H
    
    #include <QMainWindow>
    #include <QTcpSocket>
    #include <QHostAddress>
    #include <QList>
    
    namespace Ui {
    class client;
    }
    
    class client : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit client(QWidget *parent = 0);
        ~client();
    
    
    private slots:
        void on_pushButton_clicked();
    
    public slots:
        void leer_socketserver();
    
    private:
        Ui::client *ui;
    
        QTcpSocket *tcpclient;
    };
    
    #endif // CLIENT_H
    
    

    client.cpp

    #include "client.h"
    #include "ui_client.h"
    
    client::client(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::client)
    {
        ui->setupUi(this);
    
        tcpclient = new QTcpSocket (this);
    
        tcpclient->connectToHost( QHostAddress::LocalHost , 1234);
    
        connect (tcpclient, SIGNAL (readyRead()), this, SLOT (leer_socketserver()));
    }
    
    client::~client()
    {
        delete ui;
    }
    
    void client::on_pushButton_clicked()
    {
        tcpclient->write( ui->lineEdit->text().toLatin1().data(), ui->lineEdit->text().size());
        ui->lineEdit->clear();
    }
    
    void client::leer_socketserver()
    {
        QByteArray buffer;
        buffer.resize( tcpclient->bytesAvailable() );
        tcpclient->read( buffer.data(), buffer.size() );
        ui->plainTextEdit->setReadOnly( true );
        ui->plainTextEdit->appendPlainText( QString (buffer));
    }
    
    

    main.cpp

    #include "client.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        client w;
        w.show();
    
        return a.exec();
    }
    
    


  • Udalo mi sie naprawic usuwajac w server.cpp

    tcpserver->newConnection();
    //i dodajac
    tcpclient = new QTcpSocket (this);
    

    Jednak pojawil sie kolejny problem. Wiadomosci dochodza kiedy odpale na 1 pc. Kiedy chce to zrobic na 2 podlaczonych do 1 routera, wiadomosci nie dochodza, a po stronie servera w 3 zakladce (komunikaty aplikacji) po probie wyslania wiadomosci pojawia sie :

    QIODevice::write (QTcpSocket): device not open
    

Log in to reply
 

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