Results 1 to 20 of 43

Thread: incommingConnection not called by QTcpServer

Hybrid View

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

    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
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    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.

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
  •  
Qt is a trademark of The Qt Company.