Results 1 to 13 of 13

Thread: Creating a TcpServer listener class for MainWindow to use

  1. #1
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Creating a TcpServer listener class for MainWindow to use

    I'm trying to get my MainWindow application launch a few TcpServer threads to wait for connections and go from there.

    I am able to connect to the open port, but nothing gets triggered inside of Qt for me to go any further.

    MainWindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "serverlistener.h"
    4. #include <QtNetwork/QTcpServer>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. if (!serverListener.listen(QHostAddress::LocalHost, 5600)) {
    13. qCritical("Error opening port 5600.");
    14. serverListener.close();
    15. return;
    16. }
    17. }
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. delete ui;
    22. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include "serverlistener.h"
    6.  
    7. class ServerListener;
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. private:
    21. Ui::MainWindow *ui;
    22. ServerListener serverListener;
    23.  
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    ServerListener.cpp:
    Qt Code:
    1. #include "serverlistener.h"
    2. #include "mainwindow.h"
    3. #include <iostream>
    4. #include <QDebug>
    5.  
    6. using namespace std;
    7.  
    8. ServerListener::ServerListener(QObject *parent) :
    9. QTcpServer(parent)
    10. {
    11. // serverSocket = new QTcpServer(this);
    12.  
    13. cout << "Listening" << endl;
    14. }
    15.  
    16. ServerListener::~ServerListener() {
    17. cout << "Destruction" << endl;
    18. serverSocket->close();
    19. }
    20.  
    21. void ServerListener::incomingConnection(int descriptor) {
    22.  
    23. cout << "Incoming connection" << endl;
    24. //ServerThread *thread = new ServerThread(descriptor, this);
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

    ServerListener.h:
    Qt Code:
    1. #ifndef SERVERLISTENER_H
    2. #define SERVERLISTENER_H
    3.  
    4. #include <QtNetwork/QTcpServer>
    5. #include <QObject>
    6.  
    7. class ServerListener : public QTcpServer
    8. {
    9. Q_OBJECT;
    10. public:
    11. explicit ServerListener(QObject *parent = 0);
    12. ~ServerListener();
    13.  
    14. protected:
    15. void incomingConnection(int descriptor);
    16.  
    17. private:
    18. QTcpServer *serverSocket;
    19. };
    20.  
    21. #endif // SERVERLISTENER_H
    To copy to clipboard, switch view to plain text mode 

    Here's my goal:

    Qt Code:
    1. [listen1]<-------|MainWindow|------->[listen2]
    2.  
    3. [client1->listen1]
    4. [client2->listen2]
    5. [client3->listen1]
    6. ...
    7. [client10->listen1]
    To copy to clipboard, switch view to plain text mode 

    Basically, I want to make sure the listening functions don't block the rest of the application. The program I'm making is quite advanced and this is my first time using Qt. I absolutely love the language and editor, which is why I chose it over VS2010 or VC#. I just hope I can accomplish what I have in mind (arrays/structs of sockets and structuring communication between them as needed).

    Thanks.

  2. #2
    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: Creating a TcpServer listener class for MainWindow to use

    You need to connect some signals to your socket. Have a look at networking examples that come with Qt.
    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.


  3. #3
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a TcpServer listener class for MainWindow to use

    Quote Originally Posted by wysota View Post
    You need to connect some signals to your socket. Have a look at networking examples that come with Qt.
    I did look at it and I didn't see any connect() for the incomingConnection(), I thought that was inherited from the parent TcpServer class. I simply have a cout statement to see if that function is ever called and it is not, which is why I'm confused.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Creating a TcpServer listener class for MainWindow to use

    Quote Originally Posted by TheVirus View Post
    I did look at it and I didn't see any connect() for the incomingConnection(), I thought that was inherited from the parent TcpServer class. I simply have a cout statement to see if that function is ever called and it is not, which is why I'm confused.
    Without using the client socket, you'll not get far.
    Take a look at this example: http://doc.qt.nokia.com/4.7/network-fortuneserver.html

    Question:
    Why do you inherit QTcpServer and create a QTcpServer in that same class?

    Notes:
    Your program will crash because you commented out important code. If you do that, always check that your code is valid.
    The use of the word "thread" in your code without you understanding the basics of network programming and signals and slots doesn't bode well. You need to simplify your approach.
    Last edited by tbscope; 16th April 2011 at 20:11.

  5. #5
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a TcpServer listener class for MainWindow to use

    Quote Originally Posted by tbscope View Post
    Without using the client socket, you'll not get far.
    Take a look at this example: http://doc.qt.nokia.com/4.7/network-fortuneserver.html

    Question:
    Why do you inherit QTcpServer and create a QTcpServer in that same class?

    Notes:
    Your program will crash because you commented out important code. If you do that, always check that your code is valid.
    The use of the word "thread" in your code without you understanding the basics of network programming and signals and slots doesn't bode well. You need to simplify your approach.
    Right now, I'm using 'nc' to test my connection before going any further. I've looked at the fortune-server example several times and thought I was doing precisely what it was doing, just in a MainWindow instead of a QDialog.

    The commented out 'new ServerListener' was from when I was not inheriting QTcpServer and was going about the server in a different way. I just left the commented code in there for future reference to myself that I've tried that method. I know the program will crash if I uncomment that code, that's why it's commented out I understand network programming just fine, just not the way Qt does it. I'm just not sure what I'm doing wrong and staring at the same examples for several hours isn't help either.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating a TcpServer listener class for MainWindow to use

    Apart from the main window destructor derefrencing an uninitialised pointer the code as presented compiles and runs fine here.
    Qt Code:
    1. chrisw@newton /tmp/simple_example $ ./simple_example
    2. Listening
    3. Incoming connection
    4. Incoming connection
    5. Incoming connection
    6. Incoming connection
    7. Destruction
    8. Segmentation fault
    To copy to clipboard, switch view to plain text mode 
    I was using telnet to connect to port 5600 from two separate places.

    Start by correcting the destructor, clean the build, run qmake, and rebuild.

  7. #7
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a TcpServer listener class for MainWindow to use

    Quote Originally Posted by ChrisW67 View Post
    Apart from the main window destructor derefrencing an uninitialised pointer the code as presented compiles and runs fine here.
    Qt Code:
    1. chrisw@newton /tmp/simple_example $ ./simple_example
    2. Listening
    3. Incoming connection
    4. Incoming connection
    5. Incoming connection
    6. Incoming connection
    7. Destruction
    8. Segmentation fault
    To copy to clipboard, switch view to plain text mode 
    I was using telnet to connect to port 5600 from two separate places.

    Start by correcting the destructor, clean the build, run qmake, and rebuild.
    I got it working somehow, but I'm redoing my approach so I can share a QList between classes (I need to keep track of connections). So, I'm going to make a new thread in a bit if I can't figure it out. In my other program (C), I used 'extern' to share the struct between files, let's hope Qt acts the same.

  8. #8
    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: Creating a TcpServer listener class for MainWindow to use

    By saying "thread" do you mean "thread" or "class"? Because so far we have seen no threads in your code.
    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.


  9. #9
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a TcpServer listener class for MainWindow to use

    At the time, the goal was to detect incoming connections and then pass them off to a thread to be processed. Talking with someone on the IRC channel, he told me that using threads is pointless as they don't block like Java does. I do have the connections working and I can parse input, but I'm trying to add the connection's IP, port and text to a class so I can keep track of the connections and process them later.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating a TcpServer listener class for MainWindow to use

    Quote Originally Posted by TheVirus View Post
    I got it working somehow,
    Guessing and just pushing buttons until it "works somehow" is not a recipe for a reliable result.

    In my other program (C), I used 'extern' to share the struct between files, let's hope Qt acts the same.
    Qt is not a programming language. Why do you believe that using a library of routines, like Qt, is going to change the fundamental behaviour of the programming language (C/C++)?

  11. #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: Creating a TcpServer listener class for MainWindow to use

    Not that using a public global variable for sharing data is a good concept...
    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.


  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Creating a TcpServer listener class for MainWindow to use

    Quote Originally Posted by wysota View Post
    Not that using a public global variable for sharing data is a good concept...
    Indeed not, and even worse if you are talking about multiple threads accessing said global structure.

  13. #13
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a TcpServer listener class for MainWindow to use

    Quote Originally Posted by ChrisW67 View Post
    Guessing and just pushing buttons until it "works somehow" is not a recipe for a reliable result.


    Qt is not a programming language. Why do you believe that using a library of routines, like Qt, is going to change the fundamental behaviour of the programming language (C/C++)?
    I was reorganizing the .h files and removed some #includes and it worked after that. I think it was due to either a conflict or ordering of the includes.

    I'm not hoping that Qt will change the way C++ works, I'm just unfamiliar with it. It's been years since I've done C++ programming and even when I was, I hardly used anything this advanced (never got into sockets, never got into data sharing). I don't claim to be a great programmer, in fact, I think I'm pretty basic, but I do have motivation and a willingness to learn. The way I learn is by doing. I can't read a book and then go work on some simple programs, because I'll get bored and not bother with it. I learn by giving myself a task and doing what it takes to complete it. Once it's completed, I then work on making it better and more efficient and try to use the more advanced features of the language than hacking something together as a band-aid.

    Quote Originally Posted by wysota View Post
    Not that using a public global variable for sharing data is a good concept...
    I'm just trying to follow what already works in a different method. I know I have a lot to learn of C++/Qt and I know that doing things just to get them to work is a poor excuse for bad coding practices and also leads to bad habits and a lack of learning, but it's what I'm used to and I'm trying to overcome it. Old habits die hard, and all that jazz. I thank you guys for your help and wouldn't have been able to get this far without this forum and IRC channel having me look at things in a different light.

    Quote Originally Posted by ChrisW67 View Post
    Indeed not, and even worse if you are talking about multiple threads accessing said global structure.
    Yeah, that's one challenge that I was putting off and was going to be "I'll cross that bridge when I get to it." but I realize that it would probably involve a rewrite of everything done up to that point. I know I need to do things right from the beginning and I know that the project I am undertaking is probably not one for a beginner, especially one with rusty C++ skills and no Qt knowledge. I've rarely used libraries in the past and did things in a really, really poor manner, but I always managed to get things done (college classes). I'm purely self-taught and if something worked then I'd never question it. I've never developed anything for use outside of my own personal needs or for anything major.

    I realize my bad habits and poor style is causing more work for the people I'm asking for help and also realize that it will, potentially, lead to unanswered questions or the standard 'RTFM'. I may be new here but I can already tell this is a community that is about sharing knowledge and attention to detail. I plan on reading the documentation some more and start an outline of what I want to do and any roadblocks I'll run into. I'm not a coding perfectionist, as you can probably tell, but I do have an unquenchable thirst for knowledge. I must know why things work and how they work. I also hate it when people think poorly of me, even if it is over an anonymous forum.

    I hope I can fix my issues and make the community proud.

    <end of Mea Culpa>

Similar Threads

  1. Replies: 3
    Last Post: 12th April 2011, 11:58
  2. Replies: 4
    Last Post: 17th January 2011, 15:05
  3. Replies: 2
    Last Post: 21st April 2010, 09:07
  4. Problem with Listener and QPixmap
    By thiagotrss in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2009, 19:24
  5. Phone call listener
    By raulftang in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 25th April 2007, 20:09

Tags for this Thread

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.