How to not close socket?
-
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? -
Don't call socket->close();
Honestly I'm not sure what is your requirement in boarder sense. This answer is very transactional.
-
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?
-
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.
-
@ 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 -
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 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.
-
[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 -
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.
-
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 callos << "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?