Solved [solved] tpc socket binding problem
-
I have a simple console app which i wrote to test qt's tcp sockets, as follows:
When the app starts is asks me (console input) if i want it to be a client or a server; then:
- if the app runs in client mode then a QTcpSocket is created and it is bound to 127.0.0.1:8081 - this is done with bind(QHostAddress(QString(clientIP.c_str())), clientPort)
- if the app runs in server mode then a QTcpServer is created and it is bound to listen on 127.0.0.1:8080 - this is done with listen(QHostAddress(QString(listenIP.c_str())), listenPort)
Now the problem: i first start an app instance in client mode, and then i start another instance in server mode. The following error occurs in the server-mode application when trying to bind the QTcpServer : 'listen' returns with 'false', the error string is "The bound address is already in use". Why is this? Is this normal behavior although i am using different ports for the listener and the client? Can't i bind a listener and a client on the same machine on the same IP on different ports?
-
Hi,
for a client yo do not need to bind to a port.
Just calltcpSocket->connectToHost(hostname, port);
see the example: link text
If you explicitly want to use a specic port on client side it must differ from the server port if both processes run on the same machine.
If you still get this error check for example if you have another service running on port 8080 (maybe a proxy?)
Greetings
Nando -
@gyll
You would need to listen with QTcpServer on a specific port (e.g. 8080).
The same port would be used with QTcpSocket to connect.There are examples available Fortune server and Fortune client. This will help you in understanding.
-
@Nando i don't get it, maybe you didn't read my post carefully? the client and server sockets are on DIFFERENT ports (server on 8080, client on 8081, i even marked that in bold in my post)
and no, there's no other service on port 8080, if i start the server first than it binds properly.
-
@gyll Please show us your code.
I read it. And see that you use different ports, but somehow either your have a service already running on this port or maybe you have a code issue. (same port variable from config for both modes client / server?)Did you remove the bind from the client code?
You do not need this.Greetings
Nando -
- i need to specify the client-side port, please don't tell me what i need and what i don't
- i tried to remove the binding of the client (i.e. allow it to chose its own port) but the same happens: the server's 'listen()' returns "The bound address is already in use" error
- if i start only the server (i.e. the client is not stated at all) then the server binding (i.e. 'listen()') apparently works (or, at the very least, it does not return any error)
- i cannot paste the code here, it's way too big. i'm writing my own wrapper classes over Qt's sockets and they are way to big to list here
-
You are right that I did not read care full enough. Sorry about that. My understanding was that your problem is more basic.
@gyll said:
I have a simple console app which i wrote to test qt's tcp sockets, as follows:
When the app starts is asks me (console input) if i want it to be a client or a server; then:
- if the app runs in client mode then a QTcpSocket is created and it is bound to 127.0.0.1:8081 - this is done with bind(QHostAddress(QString(clientIP.c_str())), clientPort)
- if the app runs in server mode then a QTcpServer is created and it is bound to listen on 127.0.0.1:8080 - this is done with listen(QHostAddress(QString(listenIP.c_str())), listenPort)
Now the problem: i first start an app instance in client mode, and then i start another instance in server mode. The following error occurs in the server-mode application when trying to bind the QTcpServer : 'listen' returns with 'false', the error string is "The bound address is already in use". Why is this? Is this normal behavior although i am using different ports for the listener and the client? Can't i bind a listener and a client on the same machine on the same IP on different ports?
Basically there should be no problem in what you are trying to do. You can use a couple of QTcpClients and a couple of QTcpServers in parallel on the same machine as well as in the same application at the same time. I'm doing this all the time.
You may want to check with this or similar statement:
Server->listen ( QHostAddress :: Any, 8080 );
Possibly your port is already occupied by a completely different application or by a copy of your application (left over from trials).
Therefore, you need to modify your code for checking if it is an assigned port problem.Next step would be using another port number just to see, what the error message is there.
-
okay, my mistake, there's no problem with the sockets. instead of instantiating only the server in the server app and the client in the client app, i was indeed instantiating both sockets twice (once in the client app and then again in the server app)