Results 1 to 5 of 5

Thread: QTcpServer and port problem

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QTcpServer and port problem

    Hello.
    I have a problem with setting up a port for QTcpServer.

    Qt Code:
    1. void MainWindow::initServ()
    2. {
    3. adr.setAddress( QString("127.0.0.1") );
    4. adr.toIPv4Address();
    5. quint16 port = 5050;
    6.  
    7. QTcpServer *CRserv = new QTcpServer();
    8. CRserv->setMaxPendingConnections( 10 );
    9. CRserv->setProxy(QNetworkProxy::NoProxy);
    10.  
    11. if (!CRserv->listen()) {
    12. QMessageBox::critical(this, tr("CR_bypasser"),
    13. tr("Unable to start the server: %1.")
    14. .arg(CRserv->errorString()));
    15. close();
    16. return;
    17. }else{
    18. CRserv->listen( adr, port );
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    When i setup server this way i got in appliaction output window:
    "QTcpServer::listen() called when already listening"

    Thanx in advance for any reply.
    Best regards

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer and port problem

    Some othere application might already be using that port. I guess Yahoo messenger uses 5050 frequently.
    Try changing the port number and see if it works

  3. #3
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTcpServer and port problem

    It's not port at fault (i tried many combination and don't have any 'net app' running atm.)
    And looking at the code that i provide its no suprise that it says that QTcpServer is runing.
    To me it seams that server starts runing when i create new one
    Qt Code:
    1. QTcpServer *CRserv = new QTcpServer();
    To copy to clipboard, switch view to plain text mode 
    on it's default settings, ie. any adapter, random port.
    When i call listen() before "if" statment i got 'qmessagebox' error with no desciption.

    Any other sugestions?

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer and port problem

    Qt Code:
    1. if (!CRserv->listen()) {
    2. QMessageBox::critical(this, tr("CR_bypasser"),
    3. tr("Unable to start the server: %1.")
    4. .arg(CRserv->errorString()));
    5. close();
    6. return;
    7. }else{
    8. CRserv->listen( adr, port );
    9. }
    To copy to clipboard, switch view to plain text mode 
    your problem is here, because if/else combination is wrong. When you use listen() and it returns true, then your server is listening on every address and any port. And if it's true then else block would be processed when you call listen() on alredy listening server with different address and port...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTcpServer and port problem

    Thank you. I knew it was wrong. Without if works like a charm.
    I thought that I will first check conn state, but it looks like it connect same time when i check it.
    Big thanks.

Similar Threads

  1. Replies: 4
    Last Post: 23rd May 2008, 09:42
  2. QTcpServer & QTcpSocket questions...
    By jxmot in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2008, 21:38
  3. Same port when you pass socketDescriptor?
    By Dumbledore in forum Qt Programming
    Replies: 3
    Last Post: 11th December 2007, 22:03
  4. QTcpSoket and QTcpServer problem.
    By v3n0w in forum Qt Programming
    Replies: 9
    Last Post: 22nd October 2007, 18:20
  5. Replies: 6
    Last Post: 8th January 2007, 10:24

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.