PDA

View Full Version : forcing local port number from TcpSocket?



nbkhwjm
3rd January 2008, 15:48
is there are method to force the local port number when making a tcp connection?

for example, i am connecting to an RSH service to port 514, but i need the source port to be 1023...



void Process::connectToRsh()
{

blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost(ui.IpAddressLineEdit->text(),514);
ui.ResultsTextEdit->append(tr("connecting"));
}

So its easy to see how to connect to port 514, but what about the local one?

Thanks!
chris

marcel
3rd January 2008, 16:13
See QAbstractSocket::setLocalPort()

nbkhwjm
3rd January 2008, 16:30
I saw that one, but the description didnt match what i needed, it seemed it was for something else.


You can call this function in a subclass of QAbstractSocket to change the return value of the localPort() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.

Note that this function does not bind the local port of the socket prior to a connection (e.g., QUdpSocket::bind()).

How can this be useful if it does not bind the port until after the connection is established?

nbkhwjm
3rd January 2008, 16:45
also on compile:

/usr/local/Trolltech/Qt-4.3.1/include/QtNetwork/qabstractsocket.h:158: error: `void QAbstractSocket::setLocalPort(quint16)' is protected
process.cpp:26: error: within this context
make: *** [process.o] Error 1

nbkhwjm
4th January 2008, 13:30
Response from TrollTech...


This is currently not possible using the Qt API, no.



What you'd have to do if you really need to do this is to do the local

binding of a socket descriptor yourself and then set the socket

descriptor on the QTcpSocket afterwards using the setSocketDescriptor()

function.



We do plan to introduce this functionality at a later point though.

Thomas
4th January 2008, 22:33
Just curious: why would you want to do it? The receiving end, i.e. the receiving TCP stack, does always know from which port the incoming connection originated. Otherwise it could not talk back. ;)

nbkhwjm
8th January 2008, 18:28
Some protocols define which port the sending system must connect from. Although this behavior has been deprecated in most protocols, it is still needed by some "antique" ones.

A good example is the original LPD spec.

The tool I am writing is an emulator that allows this setting to me configured... (i guess Ill have to not allow it for now)