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. How to not close socket?

How to not close socket?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 3.9k 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.
  • F Offline
    F Offline
    foxgod
    wrote on last edited by
    #1

    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
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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
      0
      • F Offline
        F Offline
        foxgod
        wrote on last edited by
        #3

        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
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          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
          0
          • F Offline
            F Offline
            foxgod
            wrote on last edited by
            #5

            @ 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
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              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
              0
              • D Offline
                D Offline
                dbzhang800
                wrote on last edited by
                #7

                [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
                0
                • F Offline
                  F Offline
                  foxgod
                  wrote on last edited by
                  #8

                  [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
                  0
                  • D Offline
                    D Offline
                    dbzhang800
                    wrote on last edited by
                    #9

                    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
                    0
                    • F Offline
                      F Offline
                      foxgod
                      wrote on last edited by
                      #10

                      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
                      0

                      • Login

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