PDA

View Full Version : Client Server connection through specific port



Cerberus
17th October 2015, 15:53
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)?

Cerberus
18th October 2015, 20:45
I found the QAbstractSocket::bind method but it doesn't seem to work



void ConnectionHandler::connectToServer() {
this->socket->bind(QHostAddress::LocalHost, 2000);
this->socket->connectToHost(this->ip, this->port);

if (!this->socket->waitForConnected()) {
this->socket->close();
this->errorMsg = this->socket->errorString();
}

qDebug() << this->socket->localPort();
}



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

ChrisW67
18th October 2015, 22:12
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?

Cerberus
18th October 2015, 22:29
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?

anda_skoa
22nd October 2015, 18:10
I think there is a Qt Solution called QtSingleApplication that does this.

Cheers,
_