[solved] Server - client communication over LAN



  • 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!



  • 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.



  • 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


  • Lifetime Qt Champion

    Hi,

    in addition to @hskoglund, you could also use zeroconf to setup your server



  • @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.


  • Lifetime Qt Champion

    Qt Quarterly 23 is a bit old but AFAIK you can still use it



  • @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?



  • 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.



  • @hskoglund
    This is exactly what I meant :-).


  • Lifetime Qt Champion

    Yes, that's a standard approach that has worked quiet well already for a long time :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.