Results 1 to 5 of 5

Thread: Client Server connection through specific port

  1. #1
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Client Server connection through specific port

    I am conncetion through a QSslSocket to a QTcpServer. I can specify the listening port on the Server side, but the client chooses a random port for his connection.
    Can I also specify a specific local port on which the connection is started from (e.g. 2000)?

  2. #2
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Re: Client Server connection through specific port

    I found the QAbstractSocket::bind method but it doesn't seem to work

    Qt Code:
    1. void ConnectionHandler::connectToServer() {
    2. this->socket->bind(QHostAddress::LocalHost, 2000);
    3. this->socket->connectToHost(this->ip, this->port);
    4.  
    5. if (!this->socket->waitForConnected()) {
    6. this->socket->close();
    7. this->errorMsg = this->socket->errorString();
    8. }
    9.  
    10. qDebug() << this->socket->localPort();
    11. }
    To copy to clipboard, switch view to plain text mode 

    The output I get is again a random port and not 2000!?

  3. #3
    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: Client Server connection through specific port

    There is no high level way to do this: you are describing normal behaviour for a TCP client. The source port is what differentiates two separate client connections from the same client to the same server. If your client forced the source port the machine could only support a single connection to a server, i.e. Only one connection to qtcentre.org system-wide.

    The bind() call on the client socket will only set the interface that a multi-interface client uses to set the sending address.

    Might i ask why you want to do this?
    Last edited by ChrisW67; 18th October 2015 at 21:14. Reason: updated contents

  4. #4
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Re: Client Server connection through specific port

    It is a IM Client, and therefore not necessary to run two Clients on the same machine. Is there another possible way to make sure only one Client is running on the system?

  5. #5
    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: Client Server connection through specific port

    I think there is a Qt Solution called QtSingleApplication that does this.

    Cheers,
    _
    Last edited by anda_skoa; 25th October 2015 at 15:05.

Similar Threads

  1. Replies: 3
    Last Post: 16th August 2015, 11:32
  2. Replies: 7
    Last Post: 20th May 2015, 09:28
  3. client & server connection -Beginer
    By sathees in forum Newbie
    Replies: 1
    Last Post: 11th May 2014, 12:00
  4. Replies: 1
    Last Post: 21st March 2010, 21:14
  5. client/server without port
    By evgenM in forum Qt Programming
    Replies: 3
    Last Post: 14th April 2006, 12: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.