PDA

View Full Version : bind QTcpSocket to port



TonyInSoMD
18th July 2018, 18:55
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?

Narasimha Reddy
19th July 2018, 08:07
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);
}

TonyInSoMD
19th July 2018, 08:29
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.