Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Mental difficulty understanding what is wrong...



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



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    @SPlatten Sorry, I actually mean to check errno.




  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    This post is deleted!


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


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

    @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



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



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



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



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

    All sorted now !

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


Log in to reply