Golang的HTTP服务器从一个请求中获取了两个远程端口。

huangapple go评论119阅读模式
英文:

Golang http server got two remote ports from one request

问题

我正在使用OSX 10.11.3的Macbook Pro进行工作,Golang版本为1.5。

以下是我的测试代码:

  1. package main
  2. import (
  3. "net/http"
  4. "fmt"
  5. )
  6. func main() {
  7. http.HandleFunc("/", processGET)
  8. c := &http.Server{
  9. Addr: ":8080",
  10. }
  11. c.ListenAndServe()
  12. }
  13. func processGET(w http.ResponseWriter, r *http.Request) {
  14. fmt.Println(r.RemoteAddr)
  15. }

预期结果应该在命令行屏幕上打印出客户端的IP和请求端口。

然而,每次我连接到HTTP服务器(分别在iPhone和Macbook上使用Chrome进行测试),我都会得到两条记录,IP相同(当然是同一个请求),但端口不同(请求的端口),像这样:

  1. 192.168.11.2:53061
  2. 192.168.11.2:53062

这是否意味着客户端同时发起了两个请求?我搜索了关于HTTP协议请求信息的资料,但仍然不清楚。

谢谢!

英文:

I'm working on a Macbook Pro with OSX 10.11.3, and the Golang version is 1.5,

following is my testing code

  1. package main
  2. import (
  3. "net/http"
  4. "fmt"
  5. )
  6. func main(){
  7. http.HandleFunc("/", processGET)
  8. c := &http.Server{
  9. Addr: ":8080",
  10. }
  11. c.ListenAndServe()
  12. }
  13. func processGET(w http.ResponseWriter, r *http.Request){
  14. fmt.Println(r.RemoteAddr)
  15. }

The expected result should print the ip and the request port of the client on the command line screen,

however, each time I connected to the http server(testing with Chrome on iPhone and Macbook separately), I got two records with same ip(same request, of course) and different ports(requesting port), like this,

  1. 192.168.11.2:53061
  2. 192.168.11.2:53062

Does that mean the client initiate two requests at one time?
I searched about the http protocol request information but still have no idea.

Thanks!

答案1

得分: 0

你的浏览器正在寻找一个favicon.ico并发起了两个请求。尝试使用curl命令,你将只会得到一个请求。

Golang的HTTP服务器从一个请求中获取了两个远程端口。

英文:

Your browser is looking for a favicon.ico initiating two requests. Try with curl and you will get one request.

Golang的HTTP服务器从一个请求中获取了两个远程端口。

huangapple
  • 本文由 发表于 2016年2月19日 17:12:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/35501440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定