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. Server crash after client calls QtTcpSocket::disconnectFromHost();
QtWS25 Last Chance

Server crash after client calls QtTcpSocket::disconnectFromHost();

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 744 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.
  • V Offline
    V Offline
    Vadru
    wrote on last edited by
    #1

    Hello!
    I have a server running in linux and a client running in windows.
    When the client close the application or lost connection everything works fine, but when the application runs the code:

    void Network::disconnect()
    {
       ...
        socket->disconnectFromHost();
     ...
    }
    

    The server will crash here::

    void SendJSON(json j)
    {
    	vector <uint8_t> bson = json::to_bson(j);
    	auto s = bson.size();
    
    	vector <uint8_t> final;
    	final.push_back(s >> 0);
    	final.push_back(s >> 8);
    	final.push_back(s >> 16);
    	final.push_back(s >> 24);
    
    	for (int i = 0; i < s; i++)
    	{
    		final.push_back(bson[i]);
    	}
    
        if (write(client, &final.front(), final.size()) <= 0)  <- at this line the application will crash
    	{
    		connected = false;
    		printf("connection failed\n");
    		close(client);
    
    		piThreadCreate(CheckForConnections);
    	}
    }
    

    Is the problem that I'm using QtTcpSocket for the client and not for the server and if so, how can I disconnect in a safe way without crashing the server or closing the application?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What are you using of the server ?

      Please provide the stack trace of your crash.

      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
      • V Offline
        V Offline
        Vadru
        wrote on last edited by
        #3

        I'm new to linux and I dont know how can I get the stack trace?

        The server is running on a RaspberryPI.
        The RaspberryPI is controlling my boiler and gathering some information like temperature, etc.
        And the server is sending that information to the client so I can watch my boiler on my pc even when I'm not home.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Use gdb to run your application.

          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
          1
          • V Offline
            V Offline
            Vadru
            wrote on last edited by
            #5

            This is the stack trace:

            Thread 5 "test" received signal SIGPIPE, Broken pipe.
            [Switching to Thread 0xb4fff450 (LWP 7463)]
            __GI___libc_write (nbytes=52, buf=0xb50008a0, fd=5)
                at ../sysdeps/unix/sysv/linux/write.c:26
            26      ../sysdeps/unix/sysv/linux/write.c: No such file or directory.
            
            
            1 Reply Last reply
            0
            • V Offline
              V Offline
              Vadru
              wrote on last edited by Vadru
              #6
              __libc_write (int fd, const void *buf, size_t nbytes)
              {
                return SYSCALL_CANCEL (write, fd, buf, nbytes); <- this is that line of code
              }
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Are you using a QTcpServer ?

                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
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  If not, are you handling that signal ? See here for some suggestions related to that.

                  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
                  1
                  • V Offline
                    V Offline
                    Vadru
                    wrote on last edited by
                    #9

                    Such a simple mistake I have not used TCP in such a long time that I forgot all about signals.
                    I was getting so frustrated trying to solve it for several hours.
                    Now it works, thank you so much!

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      Vadru
                      wrote on last edited by
                      #10

                      Just of curiosity, why does disconnectFromHost() sends a sigpipe but when I close application it does not send a sigpipe?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Sorry, I currently don't know although I am also intrigued.

                        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

                        • Login

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