Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TCP Server
Forum Update on Monday, May 27th 2025

TCP Server

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rennreh
    wrote on last edited by
    #1

    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??

    1 Reply Last reply
    0
    • O Offline
      O Offline
      OwonaVivienD
      wrote on last edited by
      #2

      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.

      By the way, "Vivien" is a male name in French...

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rennreh
        wrote on last edited by
        #3

        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();
            }
        }
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          asanka424
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rennreh
            wrote on last edited by
            #5

            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"

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved