TCP Server



  • Hello,
    I want to Create a simple TCP-Server.

    #include <QObject>
    #include <QTcpSocket>
    #include <QTcpServer>
    #include <QAbstractSocket>
    #include <QDebug>
    
    
    class TCP_Server : public QObject {
       Q_OBJECT
    public:
       TCP_Server(QObject *parent = 0);
    
    signals:
    
    public slots:
       void newConnection();
       void startRead();
    
    
    private:
       QTcpServer *server;
       QTcpSocket *client;
    };
    
    /*
    *
    * */
    TCP_Server::TCP_Server(QObject*parent):
       QObject(parent) {
    
       server = new QTcpServer(this);
    
       connect(server,SIGNAL(newConnection()),
               this,SLOT(newConnection()));
    
       if(!server->listen(QHostAddress::Any, 9999)) {
           qDebug() << "Server could not start";
       }else{
           qDebug() << "Server started!";
       }
    }
    /*
    *
    * */
    void TCP_Server::newConnection() {
       client = server->nextPendingConnection();
    
       client->write("hello client\r\n");
       client->flush();
    
       client->waitForBytesWritten(3000);
    
       //socket->close();
    }
    /*
    *
    * */
    void TCP_Server::startRead() {
       // get the information
       QByteArray Data = client->readAll();
    
       // will write on server side window
       qDebug() <<  " Data in: " << Data;
    
       client->write(Data);
    }
    

    and main:

    #include <QCoreApplication>
    #include <tcp_server.h>
    
    int main(int argc, char *argv[]) {
        QCoreApplication a(argc, argv);
    
        TCP_Server server;
    
        return a.exec();
    }
    
    

    Why it Crash my program at line 3 in funktion void TCP_Server::newConnection()?
    I see it has a real adress??



  • Hi, rennreh,

    Before playing with the QTcpClient returned by QTcpServer::nextPendingConnection, you should perform some verifications:

    • is the pointer client not null?
    • if it isn't, is the returned QTcpClient valid and connected?
    • etc.


  • Hello,
    Client is not NULL and the connection is ok.

    I have the program improved and asked about null but I get a segmentation fault.

    void TCP_Server::newConnection() {
        client = server->nextPendingConnection();
    
        if(NULL==client) {
            qDebug() <<  "Addr is NULL: " << endl;
        }else{
            client->write("hello client\r\n");
            client->flush();
    
            client->waitForBytesWritten(3000);
    
            //socket->close();
        }
    }
    

    If I make following code it works:

    void TCP_Server::newConnection() {
        QTcpSocket *socket = server->nextPendingConnection();
    
        if(NULL==socket ) {
            qDebug() <<  "Addr is NULL: " << endl;
        }else{
            socket ->write("hello client\r\n");
            socket ->flush();
    
            socket ->waitForBytesWritten(3000);
    
            //socket ->close();
        }
    }
    


  • What is the error you get, segfault?
    Is it client->flush() gives the crash?



  • If i Debug the programm I see it crash at line client->write with message:

    "The inferiror stopped because it received a signal from the operating system. Signal name: SIGSEGV / Signal meaning: Segementation fault"


Log in to reply
 

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