QTcpServer, QAbstractSocket
-
Documentation has this:
"bool QTcpServer::listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 )Tells the server to listen for incoming connections on address address and port port. If port is 0, a port is chosen automatically. If address is QHostAddress::Any, the server will listen on all network interfaces."
Does "address" here means the external IP address on website somewhere (fictitious e.g. WeLikeDogs.com) or IP address on my development computer?
Am I correct to assume the "port" is port on my computer, right?
Likewise, documentation has this:
"void QAbstractSocket::connectToHost ( const QString & hostName, quint16 port, OpenMode openMode = ReadWrite )
Attempts to make a connection to hostName on the given port."
Host name would be external host address like fictitious example, WeLoveYou.com, and
"port" is the local port on my computer, right? -
[quote]
Does “address” here means the external IP address on website somewhere (fictitious e.g. WeLikeDogs.com) or IP address on my development computer?
[/quote]
QTcpServer starts a sever on your computer, so you have to give it the address you want it to listen on (e.g. 127.0.0.1 or 192.168.1.123).
[quote]
Am I correct to assume the “port” is port on my computer, right?
[/quote]
As your computer is the server, yes.
[quote]
Host name would be external host address like fictitious example, WeLoveYou.com, and
“port” is the local port on my computer, right?
[/quote]
Yes. -
This depends on your application.
If you set up a server you allow other applications to connect to. Fictious names are more convenient to read, but otherwise it should not a difference in general. I guess ip address may change more often.For the client applications you should look at the documentation for "QTcpSocket ":http://doc.qt.nokia.com/4.7/qtcpsocket.html it will use the method you are refering. On the client you can use both the domain names like www.google.com and also Ip addresses.
If you are not interested in connecting to somewhere in the internet, but locally in an internally network, you probably have to use ip addresses. Either something like 192.168.1.1 or even on the same machine for both applications 127.0.0.1.
-
@ void QAbstractSocket::connectToHost ( const QString & hostName, quint16 port, OpenMode openMode = ReadWrite ) @
bq. Host name would be external host address like fictitious example, WeLoveYou.com, and
“port” is the local port on my computer, right?Er. I think loladiro has misread your second question. The answer is No. When you're doing a connectToHost(...), the port is that of the host and refers to a particular service. Like 80 for HTTP or 22 for SSH. Web-servers listen on port 80. When you connect to a website you would be connecting to that port, locally any port greater than 1024 is chosen because all ports less than 1024 are reserved for specific applications.
-
Jim with respect to this:
“void QAbstractSocket::connectToHost ( const QString & hostName, quint16 port, OpenMode openMode = ReadWrite )
Attempts to make a connection to hostName on the given port.”hostName would be external hostname, like WeLikeJim.org for example.
And Port would be the port used by WeLikeJim.org for connection on WeLikeJim.org's (remote) computer, and it is not port on my computer, right? -
bq. And Port would be the port used by WeLikeJim.org for connection on WeLikeJim.org’s (remote) computer, and it is not port on my computer, right?
Right!
@QTcpServer::listen (serverAddress, serverPort); //SERVER
QAbstractSocket::connectToHost (serverAddress, serverPort); //CLIENT @Your Client's port will chosen automatically. You may know it as follows:
@QAbstractSocket::localPort();@ -
bq. hostName would be external hostname, like WeLikeJim.org for example.
And Port would be the port used by WeLikeJim.org for connection on WeLikeJim.org’s (remote) computer, and it is not port on my computer, right?Right!
Of course, you could also connect to a port on your own machine too. if you're running two applications which need to communicate on the same machine, one could be running a tcp server or "someport".. and the other can then connect to "someport".
Basically its the port on the host you're connecting to.
-
Thanks Jim.
Here is what I have not done before.
How do I go about testing two applications talking with each other using QTcpServer on the same computer, e.g. laptop, via local host address?
I have not done this before.Someone suggest using separate physical servers, but I need to do preliminary testing on my laptop first.
I wish there is an example code of doing this.
I saw the example in the sample, but I do not think it is of any use. I am writing an app using QTcpServer, and QAbstractSocket.
I am just surprise there is no good example.Basically, I like to know what addresses and ports do I need to use to do such simulation via localhost and local ports.
How many such local address and ports can I use to simulate multiple connections between multiple apps.
Hope all this makes sense..
-
At work, my project is a reporting tool, which communicates with our flagship software running on the same machine, to make requests both ways and report the results of a simulation and more advanced communication. Will post an example, when I get the time.
bq. Someone suggest using separate physical servers, but I need to do preliminary testing on my laptop first.
It depends on what you need to test. Since, running both apps on same machine is just connections at localhost (127.0.0.1), you cannot test certain network problems or features, which might need a real scenario, in which case VM's or a cheap server as suggested elsewhere sounds like a good option.
Ofcourse, you cannot use ports less than 1024 for your applications. You can choose any random high port which is not being used already.
-
[quote author="hpng" date="1308897923"]
Basically, I like to know what addresses and ports do I need to use to do such simulation via localhost and local ports.
How many such local address and ports can I use to simulate multiple connections between multiple apps.
Hope all this makes sense.
.[/quote]You choose the address and port :-)
If the server listens on all addresses, then connecting to 127.0.0.1 should be sufficient. Otherwise connect to the public address of your machine (public in the sense of a non-localhost address).
Your client chooses the appropriate ip address depending on the destination's address and some free port for its communication, nothing to handle there.
-
Thanks Volker.
bq.
You choose the address and port :-)If the server listens on all addresses, then connecting to 127.0.0.1 should be sufficient. Otherwise connect to the public address of your machine (public in the sense of a non-localhost address).
Your client chooses the appropriate ip address depending on the destination’s address and some free port for its communication, nothing to handle there.
bq.I thing the following presentation may clear up my understanding:
OK, so let say I pick localhost address (127.0.0.1 ), and ports like these:
Port address Unofficial use by:
9999 Urchin Web Analytics Unofficial
10000 Webmin—Web-based Linux admin toolSo if I have
We want to do basic communication testing between App#1 and App#2 on same computer.
Let say App# is using QHostAddress(127.0.0.1) and port # 10000
App#2 is using QHostAddress(127.0.0.1) and port # 9999;So to talk to App#2, here is App#1 Psuedo code:
@
QTcpServer TcpServer1;
QAbstractSocket* pTcpSocket1;
QHostAddress(127..0.0.1);
//Listening for App#2
TcpServer1.listen( QHostAddress(127.0.0.1), 9999 );
....
.....
//Connecting to App#2
pTcpSocket1 = TcpServer1.nextPendingConnection();
......
...App#2
App#2 to talk to App#1, here is App#2 Psuedo code:
QTcpServer TcpServer2;
QAbstractSocket* pTcpSocket2;
QHostAddress(127..0.0.1);
//Listening for App#1
TcpServer2.listen( QHostAddress(127.0.0.1), 10000 );
....
.....
//Connecting to App#1
pTcpSocket2 = TcpServer2.nextPendingConnection();
......
...
@Would this work to allow for App#1 and App#2 to send data between each other on SAME computer ?
-
hpng,
you could probably create two applications based on your pseudo code. Establishing two servers is possible, but not necessary. Typically you have one server application and one client application. The server application would use QTcpServer as you have done. The client application would use QTcpSocket and open the connection. In the server application you would establish a socket similar to your mimick above. This allows communication in both ways. There is no need to setup the same on both sides.
However, I wonder if you have seen the examples provided by Qt. There is the "fortune server application":http://doc.qt.nokia.com/4.7/network-fortuneserver.html . If you use this as a start and also the Fortune client application, you should be able to walk through the requirements and make modifications as you need.
This might be more effective for you to understand and move forward. Especially, since there is already running code available.
Certainly you can come back and ask questions, if they arise then.