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. Mental difficulty understanding what is wrong...

Mental difficulty understanding what is wrong...

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 1.4k Views 3 Watching
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I create a socket:

    int intSocket = socket(AF_INET, SOCK_STREAM, 0);
    if ( intSocket < 0 ) {
        clsDebugService::exitWhenDebugQueueEmpty("Failed to create socket!");
    }
    //Initliase and get address of localhost
    struct sockaddr_in srvAddr;
    bzero((char*)&srvAddr, sizeof(srvAddr));
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    srvAddr.sin_port = htons(uint16Port);
    char* pszIP =  inet_ntoa(srvAddr.sin_addr);
    
    if ( pszIP != nullptr ) {
        qdbg() << "Setting up socket on ip: " << pszIP << ", port: " << uint16Port
               << ((strPurpose.isEmpty() == true) ? "" : strPurpose);
    }
    socklen_t tSvrAddr = sizeof(srvAddr);
    

    The port I'm using is 8123, I then bind the address

     int intRC = bind(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr);
    if ( intRC < 0 ) {
      clsDebugService::exitWhenDebugQueueEmpty("Cannot bind to socket!");
    }
    

    So far so good, everything is ok, listen for incoming requests:

     if ( listen(intSocket, 5) < 0 ) {
        clsDebugService::exitWhenDebugQueueEmpty("Cannot listen to socket!");
    }
    

    Another application I'm working on running on the same system does exactly the same, where it will send requests to the first application on the same address and port, but this time it returns -1 on the bind. What I'm looking to achieve is a similar interface to a HTTP client / server where the web-server listens to port 80 and clients submit requests on the same address and port...what am I missing?

    Kind Regards,
    Sy

    jsulmJ 1 Reply Last reply
    0
    • SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by SPlatten
      #14

      All sorted now ! I haven't posted the answer, because it was a lot of source to write.

      Kind Regards,
      Sy

      Pablo J. RoginaP 1 Reply Last reply
      1
      • SPlattenS SPlatten

        I create a socket:

        int intSocket = socket(AF_INET, SOCK_STREAM, 0);
        if ( intSocket < 0 ) {
            clsDebugService::exitWhenDebugQueueEmpty("Failed to create socket!");
        }
        //Initliase and get address of localhost
        struct sockaddr_in srvAddr;
        bzero((char*)&srvAddr, sizeof(srvAddr));
        srvAddr.sin_family = AF_INET;
        srvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
        srvAddr.sin_port = htons(uint16Port);
        char* pszIP =  inet_ntoa(srvAddr.sin_addr);
        
        if ( pszIP != nullptr ) {
            qdbg() << "Setting up socket on ip: " << pszIP << ", port: " << uint16Port
                   << ((strPurpose.isEmpty() == true) ? "" : strPurpose);
        }
        socklen_t tSvrAddr = sizeof(srvAddr);
        

        The port I'm using is 8123, I then bind the address

         int intRC = bind(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr);
        if ( intRC < 0 ) {
          clsDebugService::exitWhenDebugQueueEmpty("Cannot bind to socket!");
        }
        

        So far so good, everything is ok, listen for incoming requests:

         if ( listen(intSocket, 5) < 0 ) {
            clsDebugService::exitWhenDebugQueueEmpty("Cannot listen to socket!");
        }
        

        Another application I'm working on running on the same system does exactly the same, where it will send requests to the first application on the same address and port, but this time it returns -1 on the bind. What I'm looking to achieve is a similar interface to a HTTP client / server where the web-server listens to port 80 and clients submit requests on the same address and port...what am I missing?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

        @SPlatten Is there a reason you're not using QTcpSocket?
        Also you should check what exactly bind() returns.

        SPlattenS 1 Reply Last reply
        2
        • jsulmJ jsulm

          @SPlatten Is there a reason you're not using QTcpSocket?
          Also you should check what exactly bind() returns.

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          @jsulm, the return from bind is -1, always -1 when it fails. The only reason I haven't used QTcpSocket is familiarity. I will take a look now.

          Kind Regards,
          Sy

          jsulmJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @jsulm, the return from bind is -1, always -1 when it fails. The only reason I haven't used QTcpSocket is familiarity. I will take a look now.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @SPlatten Sorry, I actually mean to check errno.

            SPlattenS 1 Reply Last reply
            0
            • jsulmJ jsulm

              @SPlatten Sorry, I actually mean to check errno.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              @jsulm , errno is 0x3c (60), which according to is a timeout...

              https://www.microfocus.com/documentation/enterprise-developer/ed40pu1/ED-VS2017/GUID-1872DF9A-0FE4-4093-9A1B-B743BFDDDBA1.html

              Kind Regards,
              Sy

              jsulmJ 1 Reply Last reply
              0
              • SPlattenS SPlatten

                @jsulm , errno is 0x3c (60), which according to is a timeout...

                https://www.microfocus.com/documentation/enterprise-developer/ed40pu1/ED-VS2017/GUID-1872DF9A-0FE4-4093-9A1B-B743BFDDDBA1.html

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @SPlatten Why do you call bind() in the second application? The second application should connect(), not bind().

                SPlattenS 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @SPlatten Why do you call bind() in the second application? The second application should connect(), not bind().

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #7

                  @jsulm , thank you, how do I call connect for the socket, the option connects appearing are for signals and slots.

                  Kind Regards,
                  Sy

                  jsulmJ 2 Replies Last reply
                  0
                  • SPlattenS SPlatten

                    @jsulm , thank you, how do I call connect for the socket, the option connects appearing are for signals and slots.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by SPlatten
                      #9

                      @jsulm , I realised that, but how do I get Qt to use that, I've modified the code to:

                          if ( blnIsModule == true ) {
                              intRC = connect(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr);
                          } else {
                              intRC = bind(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr);
                          }
                      

                      It won't compile. If I "Find references under cursor" for bind I get the header:

                      /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/socket.h
                      

                      For connect there are loads, but the above isn't one of them.

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @jsulm , thank you, how do I call connect for the socket, the option connects appearing are for signals and slots.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @SPlatten You can put this networking code into a class which does not inherit from QObject.
                        Or simply use networking functionality from Qt.

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          @jsulm said in Mental difficulty understanding what is wrong...:

                          You can put this networking code into a class which does not inherit from QObject.

                          Or learn C++ and use '::connect(...)' to access the non-namespaced function :-P

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          4
                          • SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by SPlatten
                            #12

                            I'm making a real meal of this and so far not doing very well. The main application creates a socket:

                            int intSocket = socket(AF_INET, SOCK_STREAM, 0);
                            struct sockaddr_in srvAddr;
                            bzero((char*)&srvAddr, sizeof(srvAddr));
                            srvAddr.sin_family = AF_INET;
                            srvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
                            srvAddr.sin_port = htons(8123);
                            socklen_t tSvrAddr = sizeof(srvAddr);
                            int intRC = bind(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr);
                            

                            intRC is 0, which is successful. I then launch a child process:

                            QString strName = strFullPath.mid(intLastSep + 1)
                                           ,strPath = strFullPath.mid(0, intLastSep + 1);            
                            clsMainWnd::mspobjProcess->setArguments(slstArgs);
                            clsMainWnd::mspobjProcess->setWorkingDirectory(strPath);
                            clsMainWnd::mspobjProcess->setProgram(strName);
                            clsMainWnd::mspobjProcess->startDetached(&int64PID);
                            

                            The child process is launched and I can see the process ID with:

                            ps -A | grep "mdFileIO"
                            

                            The child process is supposed to send a heartbeat, when it starts it creates a socket:

                            int intSocket = socket(AF_INET, SOCK_STREAM, 0);
                            struct sockaddr_in srvAddr;
                            bzero((char*)&srvAddr, sizeof(srvAddr));
                            srvAddr.sin_family = AF_INET;
                            srvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
                            srvAddr.sin_port = htons(8123);
                            socklen_t tSvrAddr = sizeof(srvAddr);
                            int intRC = ::connect(intSocket, (const struct sockaddr*)&srvAddr, tSvrAddr);
                            

                            intRC is 0 which is successful. I then install a thread that issues a heartbeat message using the socket connected to above:

                            QJsonObject objHeartbeat;
                            objHeartbeat.insert(clsModHelper::mscszModule, clsModHelper::msszTitle);
                            QJsonDocument objDoc(objHeartbeat);
                            QByteArray bytArr(objDoc.toJson(QJsonDocument::Compact));
                            tWritten = write(intSocket, bytArr, bytArr.size());
                            

                            tWritten is > 0, so sent successfully, but I do not see the heartbeat message in the main application where the receiving handler is:

                            while( mpThread != nullptr ) {
                                struct sockaddr_in cliAddr;
                                socklen_t tCliLen = sizeof(cliAddr);
                                int intNewSocket = accept(mintSocket, (struct sockaddr*)&cliAddr, &tCliLen);
                            
                                if ( intNewSocket < 0 ) {
                                    continue;
                                }
                                ...
                            }
                            

                            No accept is processed, so it appears although the connect is successful in the client, the main application isn't processing the accept.

                            Kind Regards,
                            Sy

                            1 Reply Last reply
                            0
                            • Kent-DorfmanK Offline
                              Kent-DorfmanK Offline
                              Kent-Dorfman
                              wrote on last edited by
                              #13

                              yeah, you're not using the BSD sockets interface properly. I'd also suggest using Qt sockets API since 1) it's higher level, and 2) you're asking in a Qt specific forum.

                              If you meet the AI on the road, kill it.

                              1 Reply Last reply
                              4
                              • SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by SPlatten
                                #14

                                All sorted now ! I haven't posted the answer, because it was a lot of source to write.

                                Kind Regards,
                                Sy

                                Pablo J. RoginaP 1 Reply Last reply
                                1
                                • SPlattenS SPlatten

                                  All sorted now ! I haven't posted the answer, because it was a lot of source to write.

                                  Pablo J. RoginaP Offline
                                  Pablo J. RoginaP Offline
                                  Pablo J. Rogina
                                  wrote on last edited by
                                  #15

                                  @SPlatten said in Mental difficulty understanding what is wrong...:

                                  All sorted now !

                                  great, so please don't forget to mark your post as solved!

                                  Upvote the answer(s) that helped you solve the issue
                                  Use "Topic Tools" button to mark your post as Solved
                                  Add screenshots via postimage.org
                                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                  1 Reply Last reply
                                  0

                                  • Login

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