[solved] Server - client communication over LAN
-
wrote on 3 Sept 2015, 18:26 last edited by catalin1122 9 May 2015, 18:12
Hello guys,
I'm am trying to make a server and a client to communicate over LAN. I started to use Qt a few days ago and I need your help :).
I connect two laptops on same WiFi and I want one to be server and the other one to be client. But the client can not see the server.This is a part of the server code:
int port = 1234; if(!this->listen(QHostAddress::AnyIPv4, port)) { qDebug() << "Could not start server"; } else { qDebug()<"Server started!"; }
This is a part of the client in which I get (at least this is what i want to do) the IP address of all the connected devices and try to connect to one of them.
foreach ( const QHostAddress &address, QNetworkInterface :: allAddresses() ) { if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost)) { qDebug() << address.toString(); socketClient->connectToHost(address.toString(), 1234); if( socketClient->waitForConnected(500) ) { qDebug()<<"Connected!"; break; } } }
The problem is that I can not make it work on 2 laptops. If I run the server and the client on the same machine it works but if i try over the LAN in doesn't.
Can you suggest me something to make it work? Is it even possible?
Thank you!
-
wrote on 3 Sept 2015, 20:05 last edited by
Hi, alas QNetworkInterface :: allAddresses() gives you only the IP addresses for the PC you're running on :-(
If you want to know about IP addresses on other computers, you can either use IP addresses agreed on in advance or use some kind of broadcast to query for them.
-
wrote on 3 Sept 2015, 20:06 last edited by
Hi and welcome to devnet,
QNetworkInterface::allAddresses() returns the IP addresses of your client laptop.
You must know the address of the your server laptop -
Hi,
in addition to @hskoglund, you could also use zeroconf to setup your server
-
wrote on 3 Sept 2015, 20:47 last edited by
@hskoglund @mcosta @SGaist
Thank you for replying!@SGaist
Do you know a good tutorial or something like this to help me understanding zeroconf?
I'm using Windows. -
Qt Quarterly 23 is a bit old but AFAIK you can still use it
-
wrote on 5 Sept 2015, 16:40 last edited by
@SGaist
Thanks! I did not try it because i found a way to make it work. But I will save that page for later in case I need it.For now I use a QUdpSocket to broadcast my IP over the network, the clients listens, receives the IP and connects. And it works.
I managed to connect 2 laptops and a PC to my server . I plan to encrypt the IP address.Do you think it's a good approach?
-
wrote on 5 Sept 2015, 17:18 last edited by
Your approach sounds good :-)
I'm not a network expert, but If you mean you're broadcasting to your LAN segment (like 192.168.1.255) then that is the same approach used by many games when setting up the multiplayer lobby. -
Your approach sounds good :-)
I'm not a network expert, but If you mean you're broadcasting to your LAN segment (like 192.168.1.255) then that is the same approach used by many games when setting up the multiplayer lobby.wrote on 5 Sept 2015, 18:11 last edited by@hskoglund
This is exactly what I meant :-). -
Yes, that's a standard approach that has worked quiet well already for a long time :-)
6/10