Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    QTcpServer - program finishes after listen

    General and Desktop
    3
    4
    1155
    Loading More Posts
    • 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.
    • A
      awfun last edited by

      Hello all.

      I'm new with Qt so please excuse me for a simple question.
      I need to create a simple tcp client-server application. I found a "youtube video":http://www.youtube.com/watch?v=BSdKkZNEKlQ explaining how to use QTcpServer. Problem is that my program finishes before client doesnt manage to connect.
      What is the problem? I'm not well with streams and threads, does listen() method start a new thread?
      Thank you for your answers

      server.pro
      [code]QT       += core network
      QT       -= gui

      TARGET = server
      CONFIG   += console
      CONFIG   -= app_bundle

      TEMPLATE = app

      SOURCES += main.cpp
         myserver.cpp

      HEADERS +=
         myserver.h[/code]

      myserver.h
      [code]#ifndef MYSERVER_H
      #define MYSERVER_H

      #include <QObject>
      #include <QDebug>
      #include <QTcpServer>
      #include <QTcpSocket>

      class MyServer : public QObject
      {
         Q_OBJECT
      public:
         explicit MyServer(QObject *parent = 0);
         
      signals:
         
      public slots:
         void acceptConnection();

      private:
         QTcpServer *server;
         
      };

      #endif // MYSERVER_H
      [/code]

      myserver.cpp
      [code]#include "myserver.h"

      MyServer::MyServer(QObject *parent) :
         QObject(parent)
      {
         server = new QTcpServer(this);

      connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));

      if( !server->listen(QHostAddress::Any,1234))
             printf("Server could not start\r\n");
         else
             printf("Server started\r\n");
      }

      void MyServer::acceptConnection()
      {
         QTcpSocket *socket = server->nextPendingConnection();

      socket->write("hello client\r\n");
         socket->flush();

      socket->waitForBytesWritten(3000);

      socket->close();

      }
      [/code]

      main.cpp
      [code]#include <QCoreApplication>
      #include <stdio.h>
      #include "myserver.h"

      int main(int argc, char * argv[])
      {
         QCoreApplication a(argc, argv);
         MyServer mServer;
         printf( "finished" );
         return a.exec();
      }
      [/code]

      telnet.exe
      [code]open 127.0.0.1 1234[/code]

      1 Reply Last reply Reply Quote 0
      • A
        awfun last edited by

        Problem suddenly was solved. I did nothing ^_^

        1 Reply Last reply Reply Quote 0
        • C
          code_fodder last edited by

          I would just note that I have found sometimes when developing I can get some build errors, then undo my changes and still have errors. Then when I delete all my objects / output files and do a full re-build the problem goes away.

          Sometimes I think the build process gets the files it needs to re-compile or not confused and this can cause strange issues...

          That's just a note, its not a concrete issue, just something I have observed.

          1 Reply Last reply Reply Quote 0
          • Jeroentjehome
            Jeroentjehome last edited by

            When in doubt it is always a good thing to remove all object files and recompile. That's standard compilation mixup fix.
            Greetz

            Greetz, Jeroen

            1 Reply Last reply Reply Quote 0
            • First post
              Last post