Page 1 of 3 123 LastLast
Results 1 to 20 of 43

Thread: incommingConnection not called by QTcpServer

  1. #1
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default incommingConnection not called by QTcpServer

    Hi all,
    i have this Server but whenever i try to connect to it from the client side
    the incommingConnection is not being executed
    can anyone help me to know the reason and how to fix it ?
    Thanks in advance.

    Server:
    Qt Code:
    1. #include "server.h"
    2. #include "ui_server.h"
    3. #include <myclient.h>
    4. #include <QtCore>
    5. #include <QtGui>
    6. #include <QTcpServer>
    7. #include <QTcpSocket>
    8.  
    9. server::server(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::server)
    12. {
    13. ui->setupUi(this);
    14.  
    15. mServer=new QTcpServer(this);
    16.  
    17. QString rPath="whateverpath";
    18. UserModel= new QFileSystemModel(this);
    19. UserModel = new QFileSystemModel(this);
    20. UserModel->setRootPath(rPath);
    21. QModelIndex index= UserModel->index(rPath);
    22. ui->UserListView->setModel(UserModel);
    23. ui->UserListView->setRootIndex(index);
    24.  
    25. UserFileModel = new QFileSystemModel(this);
    26. UserFileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::AllDirs);
    27. ui->UserFilesTableView->setModel(UserFileModel);
    28. ui->UserFilesTableView->resizeColumnsToContents();
    29. ui->UserFilesTableView->setColumnWidth(3,266);
    30. }
    31.  
    32. server::~server()
    33. {
    34. delete ui;
    35. }
    36.  
    37. void server::on_StartPushButton_2_clicked()
    38. {
    39. // starting up the server on port number 1234
    40.  
    41. if(mServer->isListening())
    42. {
    43. QMessageBox::information(this,"Start Up","The Server is already running");
    44. return;
    45. }
    46. if(mServer->listen(QHostAddress::Any,1234))
    47. {
    48.  
    49. QMessageBox::information(this,"Start Up","The Server Has Started");
    50. }
    51. else
    52. {
    53.  
    54. QMessageBox::information(this,"Start Up","The Server failed to Start up");
    55. }
    56.  
    57. }
    58.  
    59. void server::on_shutdownPushButton_8_clicked()
    60. {
    61. //shutting down the server
    62. if(!mServer->isListening())
    63. { QMessageBox::information(this,"Shutdown","The Server is Already Turned Off");
    64. return;
    65. }
    66. mServer->close();
    67. QMessageBox::information(this,"Shutdown","The Server is Turned Off");
    68.  
    69. }
    70.  
    71. void server::incomingConnection(int handle) //-----> this is never executed , i add a qDebug but it never shows
    72. {
    73. //when there is an incomming connection it is set to a client
    74. myClient *mClient = new myClient(this);
    75. mClient->SetSocket(handle);
    76. }
    To copy to clipboard, switch view to plain text mode 

    Client:
    Qt Code:
    1. #include "myclient.h"
    2. #include "mytask.h"
    3. #include <QtCore>
    4.  
    5. myClient::myClient(QObject *parent) :
    6. QObject(parent)
    7. {
    8. }
    9.  
    10. void myClient::SetSocket(int Descriptor)
    11. {
    12. socket = new QTcpSocket(this);
    13.  
    14. connect(socket,SIGNAL(connected()),this,SLOT(connected()));
    15. connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
    16. connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    17. socket->setSocketDescriptor(Descriptor);
    18. qDebug() << "client connected";
    19.  
    20. }
    21.  
    22. void myClient::connected()
    23. {
    24. qDebug() << "client connected event";
    25. }
    26.  
    27. void myClient::disconnected()
    28. {
    29. qDebug() << "client disconnected";
    30. }
    31.  
    32. void myClient::readyRead()
    33. {
    34. //do something
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 6th January 2013 at 10:37. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: incommingConnection not called by QTcpServer

    Server is listening, but where is it accepting new connection?

    You should have something like this
    Qt Code:
    1. connect(mServer, SIGNAL(newConnection()), ....);
    2. or
    3. if(mServer->hasPendingConnections())
    4. {
    5. QTcpSocket * clientSocket = mServer->nextPendingConnection ();
    6. clientSocket->readAll();
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    thanks for your reply
    but isnt the incommingConnection function should accept the incomming connections and queue them ?


    Added after 21 minutes:


    i added a newConnection() public slot to the server class
    implemented it as such
    Qt Code:
    1. void Server::newConnection()
    2. {
    3. qDebug() << "new connection ";
    4. if(mServer->hasPendingConnections())
    5. {
    6. QTcpSocket *socket = mServer->nextPendingConnection();
    7. qDebug() << "connected"l
    8. }
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    then in my constructor :
    i added
    Qt Code:
    1. connect(mServer,SIGNAL(newConnection()),this,SLOT(newConnection());
    To copy to clipboard, switch view to plain text mode 

    but again non of the qDebugs were executed...
    i rly need some help here
    Best regards.
    Last edited by wysota; 6th January 2013 at 10:38. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: incommingConnection not called by QTcpServer

    I think you better have a look here:
    http://qt-project.org/doc/qt-4.8/examples-network.html

  5. #5
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    well i fixed the problem
    but now on the server side
    when the file is being uploaded by the client to the server
    the whole GUI just freezes
    i know its from the
    Qt Code:
    1. while(Socket.waitForReadyRead())
    2. {
    3. //read from socket
    4. // append to QByteArray
    5. }
    To copy to clipboard, switch view to plain text mode 

    so does anyone have a solution to this ?
    any other way to not freeze the GUI ?
    i was actually thinking of send the file size before the data
    read it separately then doing the while loop as such :
    loop until the block size is equal to the file size sent
    if anyone have tried this , may you hint me how to send the file size alone before the data ?
    thanks is advance.
    Last edited by wysota; 6th January 2013 at 10:46. Reason: missing [code] tags

  6. #6
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Non of you guys or admins want to reply?
    i need some help here.
    thanks

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    I can only suggest to use signals and slots instead of waitFor* calls.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    well in other words how can i transfer that waitForreadyRead() loop into signals and slots ?
    im not able to get the point of what you are saying
    a sample code or pseudo code would be helpfull if not at least some logic...
    thanks.

  9. #9
    Join Date
    Sep 2011
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    well i fixed the problem
    but now on the server side
    when the file is being uploaded by the client to the server
    the whole GUI just freezes...
    Hi there!

    The solution to your problem will be adding a thread to your fuction thus when you receive data, it just simply goes in background doing its job while in the meanwhile you can still use other portions of your program. I think this is so far the best solution available to you =D

    You can find more about QThread HERE With latest Qt-5.0.0 Support =D Head over "Detailed Description" so you can see whats happening in there

  10. The following user says thank you to elmasmalo1 for this useful post:

    toufic.dbouk (6th January 2013)

  11. #10
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    threads... thanks ill give it a try
    but i have a question
    at the server side i create a QTcpSocket for ever client that connects in a class
    now how can i move this exact same socket to the thread so that i does the job right ?
    like if i created a new tcpsocket at the thread class it would be in an unconnected state ?
    thanks for your fast respons.

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    well in other words how can i transfer that waitForreadyRead() loop into signals and slots ?
    Yes. handle readyRead() and all will be fine.

    a sample code or pseudo code would be helpfull if not at least some logic...
    Sample code is in Qt docs.

    Quote Originally Posted by toufic.dbouk View Post
    threads... thanks ill give it a try
    No, please don't give threads a try. That's a wrong approach here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Sep 2011
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    For moving that specific socket into the thread will be to make your QTcpSocket in your QThread Class and make it to listen for incoming connections and then serialize that Socket in order to identify it and there you have it. That QThread Class will be the one in charge of Receiving Incomming Connections and handling them over the program as you desire.

    I should call a practical Trial & Error sample where you make few threads and each of them you assign your Incomming Connections function... and test with different settings...

    If you need more reference I can suggest this series of video tutorials that could come handy in your project:

    C++ Qt 28 - QThread Part #1: Creating a Thread
    C++ Qt 29 - QThread Part #2: The Priority
    C++ Qt 30 - QThread Part #3: The QMutex Class (This is what you will be needing in order to Serialize Objects in this case your Socket)
    C++ Qt 31 - QThread Part 4: Threads and GUI (This will teach you how to organize threads and use them normally in a GUI Form)
    C++ Qt 33 - QThread Part 5: The Waiting Thread Member
    C++ Qt 35 - QThread Part 6: Threading done correctly in Qt

    I suggest you to get a good grip over the concepts of Threading in Qt with these tutorials and later on deploying the techniques learned here into your project, only then you will have a vast knowledge on how to over control your program =D

    Hope I helped in something.

  14. #13
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    well i can figure out what u mean by handle the readyRead
    i was just reading the Qthreads documentation and trying to solve my problem
    basicly if a thread handles the readyRead() singnal then my problem would be solved
    because then the thread will wait for the data to be written and block while my main thread which is my GUI
    would be working fine
    right ?
    im confused now
    im u just give me more hints on your approach ill be greatfull
    Thanks.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    basicly if a thread handles the readyRead() singnal then my problem would be solved
    You don't need an extra thread to handle readyRead(). The main thread will do that just fine. Just connect the socket's readyRead() signal to a slot and read data from the socket from within that slot. Then when you receive the signal again, read data from the socket again, etc.

    Using threads for reading socket data is just stupid. Sure you can do it, you can even start 20 threads for each client if your operating system lets you but what's the point?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    but isnt the incommingConnection function should accept the incomming connections and queue them ?
    QTcpServer::incomingConnection will be called for each accepted connection.
    The problem in your original code is that you are expecting server::incomingConnection to be called, but server is a subclass of QMainWindow.

    Cheers,
    _

  17. #16
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by anda_skoa View Post
    QTcpServer::incomingConnection will be called for each accepted connection.
    The problem in your original code is that you are expecting server::incomingConnection to be called, but server is a subclass of QMainWindow.

    Cheers,
    _
    and thats exactly what i want , i want the QTcpServer::incommingConnection to be called when a connection is established after which ill handle it the way i want.
    well server in my class is an instance of QTcpServer class , hence i guess , for sure it will signal the QTcpServer::incommingConnection when a connection is accepted even if i subclassed from QMainWindow, i still have that QTcpServer object with its incommingConnection
    so i still cant see the problem , why isnt it being invoked ?
    if im wrong , correct my information.
    thanks.


    Added after 20 minutes:


    Quote Originally Posted by wysota View Post
    You don't need an extra thread to handle readyRead(). The main thread will do that just fine. Just connect the socket's readyRead() signal to a slot and read data from the socket from within that slot. Then when you receive the signal again, read data from the socket again, etc.
    Using threads for reading socket data is just stupid. Sure you can do it, you can even start 20 threads for each client if your operating system lets you but what's the point?
    well the whole point of using threads in my case is that i want a thread other than the main thread of my application to handle the readyRead singal so that when data is being written to the server , the servers GUI wont block or freeze. Thats all my need from a thread.
    i already linked the readyRead signal to a slot to take care of the reading and writing data to the sever but still it is managed by the main thread resulting in blocking my GUI
    because i used waitforReadyRead as a loop to write the whole data to server.
    if threads is a stupid thing to do in my case, im sorry for doing it because i dont actually know how to make it non block method , even though i tried several times.
    i have already posted my code at the first post , so if u could fix it to a non blocking way by a sample code , id be waaaay too greatfull.
    thanks for your understanding and best regards.
    Last edited by toufic.dbouk; 7th January 2013 at 14:19.

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    well the whole point of using threads in my case is that i want a thread other than the main thread of my application to handle the readyRead singal so that when data is being written to the server , the servers GUI wont block or freeze. Thats all my need from a thread.
    And how much data is it? 100GB/s? 1GB/s? 1MB/s? 10kB/s? Do you also create a new thread if you want to write 10kB to a file? You don't because it takes mili if not microseconds to do it so your GUI will not even notice that. It's exactly the same with sockets -- your application only stores data in a system buffer for the socket and returns immediately. The rest is taken care of by the OS and hardware outside the scope of your application. Thus there is no need to create threads when reading or writing data from/to a socket. Without threads you end up with much cleaner, safer and quicker code.

    i already linked the readyRead signal to a slot to take care of the reading and writing data to the sever but still it is managed by the main thread resulting in blocking my GUI
    because i used waitforReadyRead as a loop to write the whole data to server.
    If you use waitForReadyRead() then there is no point in connecting to the readyRead() signal. You only need to do the latter -- connect to readyRead() and read the available data in the socket when the slot is called.

    if threads is a stupid thing to do in my case, im sorry for doing it because i dont actually know how to make it non block method , even though i tried several times.
    No, you haven't, because all the time you end up calling waitForReadyRead() despite being told not to do it.

    i have already posted my code at the first post , so if u could fix it to a non blocking way by a sample code , id be waaaay too greatfull.
    Qt Code:
    1. void myClient::readyRead() {
    2. QByteArray ba = socket->readAll();
    3. doSomethingWithData(ba);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #18
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    now thats a well and good explained reply.
    thank you for your patience.
    but trust me i did try and more than once to not use the waitforReadyRead loop
    and when i dont, the file sent just is not fully received
    thats why i was insisting on using it just to solve my problem ( it caused another problem , blocking the GUI , so i was trying to solve my other problem)
    my GUI freezes at a 7 MB file , even a 2 MB file lead to a half a sec or so of freezing , thats why it is really annoying and cant overlook it. a 1GB file would block it forever until it is fully saved on the server.
    anyway i will try to not use the waitforRedayRead() again and see how it goes another time, maybe i had a silly hidden mistake or something.
    again Iam really thankful for your concern.

  20. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: incommingConnection not called by QTcpServer

    Quote Originally Posted by toufic.dbouk View Post
    and when i dont, the file sent just is not fully received
    How many times was readyRead() called for your file? What did you do with the data after receiving it? Show your code please. And while you're at it, fix the thing anda_skoa mentioned.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #20
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: incommingConnection not called by QTcpServer

    Qt Code:
    1. void myClient::readyRead()
    2. {
    3. QString fPath("C:\\Users\\Ali\\Desktop\\music");
    4. QByteArray block;
    5.  
    6. block=socket->readAll();
    7. socket->flush();
    8. qDebug()<< "block: " << block;
    9. socket->waitForReadyRead();
    10.  
    11. QString fName= QString(block);
    12. QFile sentFile(fPath +QDir::separator() + fName);
    13. block.clear();
    14.  
    15. if(!sentFile.exists())
    16. {
    17. sentFile.open(QFile::WriteOnly);
    18. while(socket->waitForReadyRead())
    19. {
    20. QByteArray block= socket->readAll();
    21. qDebug()<< "read: " << block.size();
    22. sentFile.write(block);
    23. block.clear();
    24. }
    25. sentFile.close();
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    thats how iam saving the data.
    this readyRead slot is connected to the sockets readyRead Signal

    now when this loop is removed while(socket->waitForReadyRead())
    the file wont be fully saved, it will be corrupted. If its a picture , the first couple of pixels will be saved.

    i replied to anda_skoa but he havnt replied back , i need him to reply to my question first to understand whats going on.
    for now i connected the newConnection to another slot that will handle the work.

Similar Threads

  1. QTcpServer
    By DmitryNik in forum Newbie
    Replies: 0
    Last Post: 25th October 2011, 12:36
  2. QTcpServer
    By DmitryNik in forum Newbie
    Replies: 2
    Last Post: 1st October 2011, 08:07
  3. QTcpServer Problem
    By ionutdanila in forum Newbie
    Replies: 2
    Last Post: 27th December 2010, 12:18
  4. QTcpServer and GDB
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2009, 08:02
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.