Get remote host Ip address QTcpServer - c++

I'm using Qt to create TCP server using QTcpServer. Every time a client connects to server, I would like to know the remote host's IP address and port number. I tried searching documentation but couldn't find any information on this topic.
I know there should be a way, I'm just not able to figure it out. Help please.

QTcpServer won't tell you the address/port directly on an incoming connection but you can get it by getting the connected QTcpSocket from QTcpServer::nextPendingConnection() and then using QTcpSocket's peerAddress() and peerPort() methods.

Related

Connect to DBus service via TCP

I'm new in work with interprocess communication. I need you help and clear explanation. I have 2 applications. One of them is a service and one is a client. I've used QT/C++ to write them. When they are working on the one local PC - all is good. But I need to separate them.
So, I have a service on the PC with IP 192.30.82.101.
I connect it to bus using next code:
QDBusConnection connection = QDBusConnection::sessionBus();
connection.registerObject("/my/service/MyService", mySvc);
connection.registerService("my.service.MyService");
Also I have a client on the another PC with IP 192.30.82.40.
I connect my client using the next code:
QDBusServiceWatcher serviceWatcher = new QDBusServiceWatcher();
serviceWatcher->setConnection(QDBusConnection::sessionBus());
serviceWatcher->addWatchedService("my.service.MyService");
.....
myServiceProxy = new local::MyService("my.service.MyService",
"/my/service/MyService", QDBusConnection::sessionBus(), this);
I need to connect my client on the PC with IP 192.30.82.40 to the service on the PC 192.30.82.101. I don't know how to do this. I've found a few topics on the forum but I still don't understand what to do.
So, what I have found:
On the PC where is my service I have to add additional lines to /etc/dbus-1/session.conf:
<listen>tcp:host=<host>,port=<port></listen>
<listen>unix:tmpdir=/tmp</listen>
<auth>ANONYMOUS</auth>
<allow_anonymous/>
Here I have a question: Which IP address I have to use here (server or client)? Which port is used for DBUS (is it default port or how I can check it)? I have tried to set an IP address of my client PC, my service PC and different ports, and port 0, but I've got an error: Failed to bind socket "172.30.82.40:0": Cannot assign requested address.
On the client side I have to set DBUS_SESSION_BUS_ADDRESS with the same address: export DBUS_SESSION_BUS_ADDRESS=tcp:host=<host>,port=<port>.
I've tried to do this with different ports, with 0 port, because this means - use any free port. But I cannot start bus daemon with any port+IP configuration.
Also I've tried to connect client QDBusServiceWatcher to bus using the next code:
serviceWatcher->setConnection(QDBusConnection::connectToBus("tcp:host=<host>,port=<port>", "session"));
It was not successful.
I have no idea how to connect them to each other.
Can anybody, please, explain me how it should be, how does it work?
I want to clearly understanding how does it work?
I will be very grateful for any help. I hope for your kindness.
UPDATE
I have found how to connect my client to DBus bus via TCP:
On the PC where is my service I have to add additional lines to /etc/dbus-1/session.conf:
<listen>tcp:host=localhost,bind=*,port=6667,family=ipv4</listen>
<listen>unix:tmpdir=/tmp</listen>
<auth>ANONYMOUS</auth>
<allow_anonymous/>
Here we should add PORT. We can find it in dbus config file.
On the client side I have to set DBUS_SESSION_BUS_ADDRESS with corresponding address (IP of PC where is service):
export DBUS_SESSION_BUS_ADDRESS=tcp:host=192.50.88.10,port=6667,family=ipv4.
Thats all. We can check it just started dbus-monitor.
But now I have another issue: how to connect my client to my service?
I need to do something more to connect to my service.
I guess that this is something like:
QDBusConnection::connectToBus("tcp:host=<host>,port=<port>","connectionName");
I've tried to connect with any random name, but this is not correct.
So, my question is - where I can get correct connection name?
Correct answer to my question is:
On the PC where is one application add additional lines to
/etc/dbus-1/session.conf:
<listen>tcp:host=localhost,bind=*,port=6667,family=ipv4</listen>
<listen>unix:tmpdir=/tmp</listen>
<auth>ANONYMOUS</auth>
<allow_anonymous/>
Correct port you can find in dbus config file.
On the client side It's needed to set DBUS_SESSION_BUS_ADDRESS with
corresponding address (IP of PC where is service):
export DBUS_SESSION_BUS_ADDRESS=tcp:host=192.50.88.10,port=6667,family=ipv4.
To connect to session where is alive your remote app use the next
connection string:
DBusConnection::connectToBus("tcp:host=192.50.88.10,port=6667", "qt_default_session_bus"));
How to know session name? I've added log in my service:
qDebug() << "Connection name: " << connection.name();
Then started app, copy printed name and set it in my client.
Now it works!

Get COM/DCOM Connections?

I'd like to figure out what IP address a DCOM client is connecting from. I have an OPC server, and from the context of the COM/DCOM code, there is no way to get the client IP that I can find. This article appears to confirm that (http://blogs.msdn.com/b/sanpil/archive/2004/03/01/82302.aspx).
I tried using a C++ library that netstat uses (iphlpapi.lib, GetExtendedTcpTable) to get open connections with no luck. It looks like COM/DCOM defaults to UDP.
The only other thing I can think of is using WireShark pcap to try and capture inbound traffic on port 135?
Does anyone have any clever ideas?
It's said "Using DCOM through firewalls becomes problematic because it dynamically allocates one port per process (configurable through the registry) and requires UPD and TCP ports 135-139 to be open. ".

Find DBus server by broadcast (or otherwise)

In my Qt application I am using a peer to peer DBus connection. Server runs on computer A, client on B, connected via DBus TCP/IP connection. Works fine.
I wonder if I can somehow find out whether the server is running and what its IP address is? So far I need to provide the correct address/port in the client.
Both, server and client run in a local network. Of course, I can use a trial and error approach and ping all machines in my network. Is there something better, something like a broadcast asking for the server, and the server responding appropriately? Is Qt providing something for this?

Find all clients in network

I'm writing client-server application and I need my server to find all clients in some network. I've already found some info here: Discovering clients on a wifi network, but I still don't understand how to implement this. Maybe somebody can say where I can find some code examples.
Thanks in advance.
PS. Working on c++, windows.
Generally TCP/IP is used as a communication protocol between client and server. For Windows platform Winsock library is used to implement TCP/IP. The server binds and listens on a port for incoming connections. Just like a webserver like stackoverflow listens by default on port 80 and then client (browsers) connects to it.
Here is a link to start. Here is sample
Normally all the client connects to server which listens on a well defined port. The server is only one hence the IP address and port is well know to all the client and hence they can connect to it.
In you case you want your server to have ablity to discover all the clients in the network. To achieve this the server needs to broadcast to network some message. The client will receive this message and will respond to the server that they are available on such IP and they can connect to server or provide additional information to server. Normally instead of broadcast, multicast is used which is limited broadcast. All the clients and server will subscribe to the multicast group which is a special kind of IP address. When server send a message to this multicast address all the client, which are subscribers of this address will receive this message and can respond back. Here is a sample
Edit: you can also use boost lib to implement multicast: sender eg., receiver eg.

New To Socket Programming, Need Help Understanding How To Connect

I have a C++ Program listening for incoming socket connections on port 2222.
I have an Adobe AIR/Flex application that attempts to connect to it, when I click a button.
When I Connect To My Socket Over My Intranet, My C++ program hears and accepts the incoming socket connection and Everything Works Fine:
var Sock:Socket=new Socket("192.168.1.100",2222);
But When I Try To Connect Using My Real IP, I Get Error #2031:
var Sock:Socket=new Socket("76.18.24.118",2222);
Both programs are running on my laptop computer. I am behind a router, but have configured that router with port forwarding so that port 2222 maps to my laptop.
What Am I Missing? I am very new to this, so perhaps I'm missing something obvious to you smarties.
What does the error code mean (see your docs for socket)
Are you sure the receiving server process is alive?
The traditional starting point is Beej's guide
Port forwarding does not mean that you can use an external IP address, your laptop is still on the private 192.168 network. What it means is that when someone tries to connect on 78.18.24.118:2222 the router converts the IP address to 192.168.1.100:2222. This effectively allows you to run a server inside your network but allows an outside client to connect to you.
Look up DNAT for more information.