Results 1 to 3 of 3

Thread: bind QTcpSocket to port

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

    Default bind QTcpSocket to port

    On the client I create a QTcpSocket *ClientSocket = new QTcpSocket(this);
    ClientSocket ->bind(this computer's ip address, 33336)
    ClientSocket ->connectToHost(server ip address, 33333)


    The Server then kicks in by connecting newConnection
    QTcpSocket *ServerSocket = TcpServer->nextConnection();

    The client and server are on the same network.

    if I look at the data
    qdebug << ServerSocket->peerPort();

    the port number is random.

    How do I make the client bind to a specific port?

  2. #2
    Join Date
    Jul 2018
    Posts
    3
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: bind QTcpSocket to port

    ClientSocket -> connectToHost(server ip address, 33333);

    if(!ClientSocket ->waitForConnected(5000))
    {
    qDebug() << "Error: " << ClientSocket ->errorString();
    }

    if(ClientSocket ->state() == QAbstractSocket::ConnectedState)
    {
    QByteArray data;
    ClientSocket ->write(data);
    ClientSocket ->flush();
    return ClientSocket ->waitForBytesWritten(1);
    }

  3. #3
    Join Date
    Mar 2014
    Location
    USA
    Posts
    82
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: bind QTcpSocket to port

    Yeah, I got that, but it doesn't tell me how to use a specific port on the client side. I can already connect and send data. The server is looking on a specific port (33333). The client is using a random port. I need to use a specific port on the client that is assigned to me by sys admin. I can connect to the server on the assigned socket port (server ip, 33333). The client socket ( the one calling connectToHost(server ip, 33333) has to send the signal out from a specific port (port 33336) that is also assigned to me because because that's what sys admin wants even though I can't see why it's necessary, but we're dealing with a secure system so I have to do what they tell me.
    Last edited by TonyInSoMD; 19th July 2018 at 08:40.

Similar Threads

  1. How to bind Qml with .H and .CPP?
    By Mathan in forum Qt Quick
    Replies: 1
    Last Post: 3rd August 2016, 14:47
  2. Replies: 0
    Last Post: 19th May 2015, 15:11
  3. Replies: 4
    Last Post: 12th November 2014, 20:35
  4. Replies: 1
    Last Post: 13th March 2013, 08:44
  5. QUdpSocket connectToHost and bind an the same port
    By aLiEnHeAd in forum Qt Programming
    Replies: 2
    Last Post: 9th September 2010, 07:48

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.