Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    How to not close socket?

    General and Desktop
    3
    10
    3476
    Loading More Posts
    • 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.
    • F
      foxgod last edited by

      i see this web--
      http://doc.qt.digia.com/solutions/4/qtservice/qtservice-example-server.html
      but we need close socket when send messgae to client ? how to not close socket?

      1 Reply Last reply Reply Quote 0
      • dheerendra
        dheerendra Qt Champions 2022 last edited by

        Don't call socket->close();

        Honestly I'm not sure what is your requirement in boarder sense. This answer is very transactional.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply Reply Quote 0
        • F
          foxgod last edited by

          client send a http request to web server( nginx ), web server send this http request to The background server。 when background server accpet this requset , The background server send message to client,

          but i need close tcpsocket to send message to clinet how to not close socket?

          1 Reply Last reply Reply Quote 0
          • dheerendra
            dheerendra Qt Champions 2022 last edited by

            You don't have to close the socket to send the message. The moment it starts putting in stream it will start going. You can flush to send the data if you have any problem in receiving the data.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply Reply Quote 0
            • F
              foxgod last edited by

              @ QTcpSocket* socket = serverConnect;

                      QTextStream os(socket);
                      os.setAutoDetectUnicode(true);
              
              
                      os << "HTTP/1.0 200 Ok\r\n"
                            "Content-Type: text/html; charset=\"utf-8\"\r\n"
                             "Content-Length: 300""\r\n\r\n"
                         <<"i love you";
                      socket->flush();@
              

              client can't get any message,but we call socket->close() to replace socket->flush(),client can get message, i don't know why ? i also want to know how to not close socket and client can get message by this web address?
              http://doc.qt.digia.com/solutions/4/qtservice/qtservice-example-server.html

              1 Reply Last reply Reply Quote 0
              • dheerendra
                dheerendra Qt Champions 2022 last edited by

                Ok. Let me check this and revert back. I need to experiment this code and suggest you. In fact closing the socket is not the right way. For every data send if we need to close the socket it defeats over all design of client server. I' m surprised with this.

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply Reply Quote 0
                • D
                  dbzhang800 last edited by

                  [quote author="Dheerendra" date="1389841342"]Ok. Let me check this and revert back. I need to experiment this code and suggest you. In fact closing the socket is not the right way. For every data send if we need to close the socket it defeats over all design of client server. I' m surprised with this.[/quote]

                  As we can see, he told the server that he will send 300 bytes, but he only send no more than 10 bytes.

                  So the server will wait for another 290 bytes, unless he close the socket.

                  1 Reply Last reply Reply Quote 0
                  • F
                    foxgod last edited by

                    [quote]
                    As we can see, he told the server that he will send 300 bytes, but he only send no more than 10 bytes.

                    So the server will wait for another 290 bytes, unless he close the socket.

                    [/quote]

                    in fact , "Content-Length: 300""\r\n\r\n" is not exist, you can see this web address
                    http://doc.qt.digia.com/solutions/4/qtservice/qtservice-example-server.html

                    1 Reply Last reply Reply Quote 0
                    • D
                      dbzhang800 last edited by

                      You should give a proper Content-Length if you don't want to close the socket!

                      You had better make yourself familiar with HTTP protocol if you want to implement a http client or server.

                      1 Reply Last reply Reply Quote 0
                      • F
                        foxgod last edited by

                        http://doc.qt.digia.com/solutions/4/qtservice/qtservice-example-server.html
                        there is no Content-Length,
                        so ,you said my message only 10 bytes, i call

                        os << "HTTP/1.0 200 Ok\r\n"
                        "Content-Type: text/html; charset="utf-8"\r\n"
                        "Content-Length: 10""\r\n\r\n"
                        <<"i love you";
                        socket->flush();
                        client can't get message also.
                        when i call socket-close(),client can get message.
                        so what's wrong?

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post