HTTP Connection header and sockets

Don Cherry
Уже с Приветом
Posts: 166
Joined: 13 Oct 2003 20:11
Location: Canada

HTTP Connection header and sockets

Post by Don Cherry »

В принципе, как связаны HTTP Connection header и sockets в browser-server TCP/IP connection?

GET ... HTTP/1.1
Connection: close или keep-alive

Documentation says
...
When you accept server side, you generate a new socket for that client only.
When you have finished dealing with the client [receive...send] you must close() that socket.
...

Если на сервере сделали socket.close(), то connection больше нет, как Connection: keep-alive header может помочь/работать для reuse connection?
User avatar
liamkin
Уже с Приветом
Posts: 2601
Joined: 19 Jun 2003 20:22
Location: USA

Re: HTTP Connection header and sockets

Post by liamkin »

Все что я знаю про HTTP Headers - это что они для сервера и-или браузера. Они как костыли. Любой сторонний клиент или сервер не обязан их выполнять. Но, если вы хотите соблюдать правила, то конечно, следуйте им. Например вы выполняете из вашей программы Rest API вызов на удаленный сервер. Сервер вам выдаст кучу заголовков, которые вам не особенно нужны, многие из них связаны с безопасностью. Вам же нужен только результат в форме JSON.
User avatar
VovaK98
Уже с Приветом
Posts: 1828
Joined: 04 Mar 2002 10:01
Location: Tampa

Re: HTTP Connection header and sockets

Post by VovaK98 »

Don Cherry wrote: 30 Apr 2019 22:47 Если на сервере сделали socket.close(), то connection больше нет, как Connection: keep-alive header может помочь/работать для reuse connection?
КМК если уж закрыто, так закрыто.
https://blog.insightdatascience.com/lea ... be0efa209d

"Keep-alive connections allow the client and server to use the same TCP connection to send and receive multiple HTTP requests and responses. This helps avoid
3-way handshake for new connections— a full roundtrip of latency
slow-start
Keep-alive connections are enabled by default in HTTP/1.1 while not in HTTP/1.0. HTTP/1.0 was designed to close the connection after every request between client and server. We can actually check this difference using telnet.
Showing Connection header keep-alive and closed for HTTP/1.1 and HTTP/1.0 respectively

The good thing is that most modern browsers will use persistent HTTP connections as long as servers comply. This means if you are building a web application, enabling persistent/keep-alive connections will help improve the performance. HTTP/1.1 also allows to set a timeout for keep-alive connections (how long to wait before resetting the connection after idleness) and the number of requests to be allowed from the client before resetting the connection."
Несите чушь бережно, стараясь не расплескать. Чушь хороша, когда она полная.

Return to “Вопросы и новости IT”