Results 1 to 5 of 5

Thread: Use specific port on a client QTcpSocket

  1. #1
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Use specific port on a client QTcpSocket

    I'm trying to set it up so that when a client connects to a server, the client uses a specified port. Right now the client uses a random port. It looked like binding would work, but it didn't change anything it still used a random port. Is there a way to force the client to use a specific port? I'm using Windows 10, Visual Studio 2013, and Qt 5.4.1 64-bit. If you need something like a sample of the code, let me know. It's kind of a pain. My work computer doesn't have internet, so I have to burn it to a CD, carry it over, and transfer to the computer that has internet. But if it will help, I'm willing to do it.

  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: Use specific port on a client QTcpSocket

    Why do you need the client to use a specified port? This is not usually desired as the port might be in use by some other software. You should be able to use bind() to do this unless your OS prevents you from binding to a specific port (e.g. ports lower than 1024 are usually reserved).
    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
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Use specific port on a client QTcpSocket

    For whatever reason, IT department asked for it if possible. I used the following code but it doesn't seem to work.


    Qt Code:
    1. .h
    2. QTcpSocket *m_pSocketClient;
    3.  
    4. .cpp
    5. m_pSocketClient = new QTcpSocket(this);
    6. m_pSocketClient->bind(Specified Address, Specified Port);
    7. m_pSocketClient->connectToHost(Server Address, Server Port);
    To copy to clipboard, switch view to plain text mode 


    It still uses a random port. What am I doing wrong?

  4. #4
    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: Use specific port on a client QTcpSocket

    What is the port you're trying to bind to? Does bind() return true? Note, you might need to force a flag for reusing the port, otherwise you won't be able to bind the socket shortly (e.g. minutes) after the previous connection using that port is closed.
    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.


  5. #5
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Use specific port on a client QTcpSocket

    It's returning true. For testing purposes I'm using port 33336. The chances of that one being used are slim. I've tried all the possible flags, and none of them made a difference. It still uses a random port

Similar Threads

  1. Client Server connection through specific port
    By Cerberus in forum Qt Programming
    Replies: 4
    Last Post: 22nd October 2015, 18:10
  2. Replies: 0
    Last Post: 22nd June 2015, 03:46
  3. Binding QTcpSocket to a specific network interface
    By vkincaid in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2010, 22:28
  4. Replies: 1
    Last Post: 21st March 2010, 22:14
  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.