Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Server - client communication over LAN

[solved] Server - client communication over LAN

Scheduled Pinned Locked Moved General and Desktop
lanserver - client
10 Posts 4 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    catalin1122
    wrote on 3 Sept 2015, 18:26 last edited by catalin1122 9 May 2015, 18:12
    #1

    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!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 3 Sept 2015, 20:05 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on 3 Sept 2015, 20:06 last edited by
        #3

        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

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Sept 2015, 20:25 last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            catalin1122
            wrote on 3 Sept 2015, 20:47 last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 3 Sept 2015, 21:03 last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                catalin1122
                wrote on 5 Sept 2015, 16:40 last edited by
                #7

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

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hskoglund
                  wrote on 5 Sept 2015, 17:18 last edited by
                  #8

                  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.

                  C 1 Reply Last reply 5 Sept 2015, 18:11
                  0
                  • H hskoglund
                    5 Sept 2015, 17:18

                    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.

                    C Offline
                    C Offline
                    catalin1122
                    wrote on 5 Sept 2015, 18:11 last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 5 Sept 2015, 20:12 last edited by
                      #10

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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      9/10

                      5 Sept 2015, 18:11

                      • Login

                      • Login or register to search.
                      9 out of 10
                      • First post
                        9/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved