Results 1 to 2 of 2

Thread: how to make tcp server listen to a specific port using QTcpServer

  1. #1
    Join Date
    Mar 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to make tcp server listen to a specific port using QTcpServer

    i tried using QHostAddress to initialse my QTcpServer object but whenever i start the server i get an error unable to start the server.
    this is the code for initialising the server
    Qt Code:
    1. QTcpServer *tcpserver;
    2. tcpserver = new QTcpServer(this);
    3.  
    4. QHostAddress hostadd("192.168.3.89");
    5.  
    6. tcpserver->listen(hostadd,23456);
    7. if (!tcpserver->listen()) {
    8.  
    9. QMessageBox::critical(this, tr("Fortune Server"), tr("Unable to start the server: %1.").arg(tcpserver->errorString()));
    10. close();
    11. return;
    12. }
    To copy to clipboard, switch view to plain text mode 

    when i compile i get no error.
    is this correct??? please help me
    thanx in advance

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make tcp server listen to a specific port using QTcpServer

    Shoudn't it be:

    Qt Code:
    1. QTcpServer *tcpserver;
    2. tcpserver = new QTcpServer(this);
    3.  
    4. QHostAddress hostadd("192.168.3.89");
    5.  
    6. if (!tcpserver->listen(hostadd,23456)) {
    7. QMessageBox::critical(this, tr("Fortune Server"), tr("Unable to start the server: %1.").arg(tcpserver->errorString()));
    8. close();
    9. return;
    10. }
    To copy to clipboard, switch view to plain text mode 
    You were checking the return value of the wrong call to listen..

    HIH

    Johannes

Similar Threads

  1. How to make a proxy server?
    By Passerby in forum Newbie
    Replies: 8
    Last Post: 17th September 2011, 15:54
  2. QTcpServer and port problem
    By Talei in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2009, 19:01
  3. Replies: 1
    Last Post: 24th November 2008, 10:56
  4. Replies: 1
    Last Post: 18th June 2006, 11:12
  5. client/server without port
    By evgenM in forum Qt Programming
    Replies: 3
    Last Post: 14th April 2006, 13:10

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.