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

Issue! 10053 socket error while trying to send data from hercules



  • Hello guys, I am new to this forum. Kindly help me on solving the issue described below,
    I have developed client application using QT. i am able to send the commands from my QT applciation(GUI acts as client) to tool called "hercules"(acts as server) successfully.

    now, i want test my reception design, i.e recv() on a thread, hence i am sending data back to my QT application(client) from hercules(server).

    Problem description: the moment i send data from hercules to my client application the socket is getting closed/connection is aborted.

    I am stuck in my project progress due to this. kindly help me on this.

    thanks in advance

    code_text
    #include "Headers/r4_recv_cmd_response.h"
    #include "Headers/r4_all_headers.h"
    #include "Headers/r4_common.h"
    #include <string.h>
    
    extern SINT32 tj_connection_sts_g;
    extern INT32 sock_fd_g[2];
    
    r4_recv_cmd_response::r4_recv_cmd_response()
    {
    
    }
    
    /******************************************************************************
     *
     *Function Name: run
     *Arguments:
     *      Input Arguments      :void
     *      Output Arguments     :void
     *Return Value: void
     *Description: It processes commands received from target application.
     *****************************************************************************/
    void r4_recv_cmd_response::run()
    {
        debug_gui_print(MSG_VERBO,"%s","in thread")
        CHAR *recv_buff;/*Buffer to hold received packet*/
    
        INT32 buff_size = INIT_VALUE; /*Size of buffer that holds packets*/
    
        INT16 maxfd;
        INT16 NoOfFdsReady;
        UINT32 error_number = INIT_VALUE;
        UINT16 is_cmd_recvd = TRUE;
        fd_set readfds;
        maxfd = INIT_VALUE;
        timeval timeout = {10, INIT_VALUE};
        INT32 bytes_rcvd = INIT_VALUE; /*Size of msg*/
            INT16 ret_val= INIT_VALUE;
    
        debug_gui_print(MSG_VERBO,"%s",":Rx Thrd :In Qthread recv")
    
        while(TRUE)
        {
    #if 0
            CHAR *recv_buff;
            char str_buff[50];
            bytes_rcvd = ::recv((SOCKET)sock_fd_g[1], recv_buff, 50, INIT_VALUE);
            sprintf(
                        str_buff\
                    ,"CMD RECV:[Bytes recvd: %d] ",\
                    bytes_rcvd);
    
            for(int i =0;i<=5;i++)
            {
               // qDebug("%c",recv_buff[i]);
            }
    #endif
    
    #if 1
            FD_ZERO(&readfds);
            if(tj_connection_sts_g == TJ_CONNECTED)
            {
                debug_gui_print(MSG_INFO,"%s","--- cmd:Rx Thrd : connection exist")
                pc_gui_log_print(MSG_INFO,":cmd:Rx Thrd : connection exist")
                FD_SET(sock_fd_g[0], &readfds);
                //maxfd = (maxfd>sock_fd_g[0])?maxfd:sock_fd_g[0];
                if(maxfd>sock_fd_g[0])
                {
                    maxfd=maxfd;
                    qDebug("maxfd %d",maxfd);
                }
                else
                {
                    maxfd = sock_fd_g[0];
                    qDebug("maxfd =Socketfd :%d",sock_fd_g[0]);
                }
            }
            else
            {
                maxfd = 0;
                debug_gui_print(MSG_INFO,"%s","--- cmd:Rx Thrd : connection doesnot exist")
                pc_gui_log_print(MSG_INFO,":cmd:Rx Thrd : connection doesnot exist")
                QThread::msleep(CONSUMER_SLEEP_TIME_MILISEC);
                debug_gui_print(MSG_INFO,"%s","--- cmd:Rx Thrd : Waked up from msleep")
                pc_gui_log_print(MSG_INFO,":cmd:Rx Thrd : Waked up from msleep")
                continue;
            }
    
    #if 1
          if (is_cmd_recvd)/*TODO: should be disabled at different instants*/
            {
                debug_gui_print(MSG_INFO,"%s","cmd Rx Thrd:select infinite")
                /*Select/poll the FD's which are ready for read event*/
                NoOfFdsReady = select(maxfd+1, &readfds, nullptr, nullptr, nullptr);
                debug_gui_print(MSG_INFO,"%s","cmd Rx Thrdleaving infinite select")
                        QThread::msleep(CONSUMER_SLEEP_TIME_MILISEC);
            }
            else
    #endif
            {
                /*Select/poll the FD's which are ready for read event*/
                debug_gui_print(MSG_INFO,"%s","cmd Rx Thrd:Select: finite timeout")
                NoOfFdsReady = select(maxfd+1, &readfds, nullptr, nullptr, &timeout);
            }
    
            if(NoOfFdsReady)
            {
                debug_gui_print(MSG_INFO,"%s","cmd Rx:Data available in recv buffer- In the if loop")
                pc_gui_log_print(MSG_INFO,"%s",":cmd Rx:Data available in recv buffer- In the if loop")
    
                if(FD_ISSET(sock_fd_g[0],&readfds))
                {
                    ret_val = Generic_Recv_Call(recv_buff, 50,\
                                                    &bytes_rcvd);
                }
                else
                {
                    debug_gui_print(MSG_INFO,"%s","cmd Rx:Data not available in cmd recv buffer-FDSET")
                    pc_gui_log_print(MSG_INFO,"%s",":cmd Rx:Data not available in cmd recv buffer-FDSET")
    
                }
    
            }/*End of NoOfFdsReady condition check*/
            else
            {
                debug_gui_print(MSG_INFO,"%s","cmd Rx:Data not available in any recv buffer-In else loop")
                if(NoOfFdsReady == false)
                {
                    debug_gui_print(MSG_INFO,"%s","cmd Rx:Time out occured in reception")
                    pc_gui_log_print(MSG_INFO,"%s",":cmd Rx:Time out occured in reception")
    
                }
                else if(NoOfFdsReady == -1)
                {
                    debug_gui_print(MSG_INFO,"%s","cmd Rx:Error occured in reception")
                    pc_gui_log_print(MSG_INFO,"%s",":cmd Rx:Error occured in reception")
                }
                else
                {
                    debug_gui_print(MSG_INFO,"%s","cmd Rx: -empty else")
                    /**/
                }
            }
    #endif
        }
    }
    
    
    INT32 r4_recv_cmd_response::Generic_Recv_Call(CHAR* recv_buff, \
                                                      SINT32 buff_size,SINT32 *size)
    {
        debug_gui_print(MSG_INFO,"%s","cmd Rx:Rx Thrd: recv gen call start")
        pc_gui_log_print(MSG_INFO,"%s",":cmd Rx:Rx Thrd: recv gen call start")
    
        UINT32 error_number = INIT_VALUE;
        CHAR str_buff[BUFF_SIZE];
        SINT32 bytes_rcvd;
        SINT32 result;
    
        /*Receive Msg from SERVER*/
        debug_gui_print(MSG_INFO,"%s","cmd Rx:waiting for message from recv()")
        pc_gui_log_print(MSG_INFO,"%s",":cmd Rx:waiting for message from recv()")
    
        bytes_rcvd = recv(sock_fd_g[0], recv_buff, buff_size, INIT_VALUE);
        sprintf(str_buff\
                ,"CMD RECV:[Bytes recvd: %d] ",\
                bytes_rcvd);
        char msg[100];
        strncpy(msg,recv_buff,buff_size);
    
        debug_gui_print(MSG_INFO,"%s",str_buff)
        pc_gui_log_print(MSG_INFO,str_buff)
                debug_gui_print(MSG_INFO,"%s",msg)
    
         /*
         *Save the errno to ensure that errno is not overwritten by other
         *system calls
         */
        error_number = WSAGetLastError();
    
    
        if(bytes_rcvd > INIT_VALUE)
        {
              debug_gui_print(MSG_INFO,"%s","cmd Rx Thrd:Bytes recved > 0 i.e pkt successfully recvd")
            debug_gui_print(MSG_INFO,"%s",str_buff)
            pc_gui_log_print(MSG_INFO,"cmd Rx Thrd:Bytes recved > 0 i.e pkt successfully recvd")
            //*size = bytes_rcvd;
            result = TRUE;
        }
        else
        {
            if(bytes_rcvd == FALSE)
            {
                /*server has performed orderly shutdown*/
    
                debug_gui_print(MSG_INFO,"%s"," cmd Rx Thrd:Server disconnected orderly shutdown")
                pc_gui_log_print(MSG_INFO," cmd Rx Thrd:Server disconnected orderly shutdown")
                sprintf(str_buff\
                        ,"%d: ",\
                        error_number);
                sprintf(str_buff\
                        ,"CMD RECV:[error_number: %d]",\
                        bytes_rcvd);
                debug_gui_print(MSG_INFO,"%s",str_buff)
                pc_gui_log_print(MSG_INFO,str_buff)
                result = false;
            }
            else
            {
                if (error_number == WSAECONNRESET)
                {
    
                    debug_gui_print(MSG_INFO,"%s"," cmd Rx Thrd:Server disconnected forced shutdown")
                    pc_gui_log_print(MSG_INFO," cmd Rx Thrd:Server disconnected forced shutdown")
                    sprintf(str_buff\
                            ,"%d: ",\
                            error_number);
    
                    debug_gui_print(MSG_INFO,"%s",str_buff)
                    pc_gui_log_print(MSG_INFO,str_buff)
                    result = false;
                }
                /*to be done -Add other error cases as well*/
                else
                {
                    sprintf(str_buff\
                            ,"%d: ",\
                            error_number);
    
                    debug_gui_print(MSG_INFO,"%s",str_buff)
                    pc_gui_log_print(MSG_INFO,str_buff)
                    result = false;
                }
            }
        }
        debug_gui_print(MSG_INFO,"%s","Rx Thrd: recv gen call exit")
        pc_gui_log_print(MSG_INFO,"Rx Thrd: recv gen call exit")
    
        return result;
    }/*End of Generic_Recv_Call*/
    
    
    


  • I have not used any closeSocket() in my application.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are asking to help debug the server side code ?



  • Thanks for your reply
    No. the code snipet which i have posted is client side.

    i dont have server code, so i am using "Hercules" tool to server me as Server.

    Can you look into my client code and suggest me if i am going wrong in flow of receiving the data



  • I tried this code in while loop. even then simple data sent from "Hercules" is immediatly closing the connection. what is causing this close/abort in connection im not sure. im stuck still

    do {

                        bytes_rcvd = recv(sock_fd_g[0], recv_buff, 1, 0);
                        if ( bytes_rcvd > 0 )
                            printf("Bytes received: %d\n", bytes_rcvd);
                        else if ( bytes_rcvd == 0 )
                            printf("Connection closed\n");
                        else
                            printf("recv failed: %d\n", WSAGetLastError());
    
        } while( bytes_rcvd > 0 );
    

    The error is : recv failed:10053



  • Hello guys,

    The same code with simple syntax/compatibliy changes i worked on QT version 4.7.
    i worked absolutely fine.

    i was able to receive the data.
    can anybody please tell me why the same thing is not working for 5.13.2 QT version.
    do i need to apply anything more to my code to make it work



  • I tryed to execute the same code with 5.10 version of QT the recv() call is working absolutely fine.
    the connection is getting closed only when i execute the GUI in 5.13.2 and try to receive some one byte/n bytes of data from "Hercules" tool.

    Kindly share your view on this or if anybody is currently working on 5.13.2 then le me know if i am missing anything to get the right support from current version.

    Thank you


  • Qt Champions 2019

    Since you absolutely don't use any Qt socket stuff here I don't know how we could help. Don't see any relation to Qt here.



  • @Christian-Ehrlicher Thanks for your reply Chrisitian Ehrlicher. My major doubt is why does 5.13.2 below versions support my program. Y not the latest version. what is the difference thats causing the issue???

    i am looking for an answer for the same, Because im going to use the latest version for my project. if these API issues occure in future this will lead to delay in delivery of product....!!!!!!!!!!!!!!!



  • Hello guys.
    i am still stuck up, i have latest 5.14 version of qt but nothing workd for me @recv() case.
    im not fully using TCPSOCKET of QT. More of C language is used as i had used in my previous project while developing in qt.

    kindly look into it please.
    the only cause i want to find is why is my socket getting closed at recv() data from "Hercules"


  • Lifetime Qt Champion

    @ShruthiRich
    Hi
    Since Qt being a c++ framework,
    i cannot see how which version you use should affect your use of native windows api. ??

    recv() has nothing to do with Qt and I cannot find anything to try regarding this and a certain Qt version.
    It should simply have no effect.

    Did you try fully use Qt for network instead ?


Log in to reply