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

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

Golang http server got two remote ports from one request

问题

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

以下是我的测试代码:

package main

import (
	"net/http"
	"fmt"
)

func main() {
	http.HandleFunc("/", processGET)
	c := &http.Server{
		Addr: ":8080",
	}
	c.ListenAndServe()
}

func processGET(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r.RemoteAddr)
}

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

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

192.168.11.2:53061
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

package main 
import (
   "net/http"
   "fmt"
)
func main(){

    http.HandleFunc("/", processGET)
	c := &http.Server{
    	Addr: ":8080",
    }
    c.ListenAndServe()
}
func processGET(w http.ResponseWriter, r *http.Request){
	fmt.Println(r.RemoteAddr)
}

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,

192.168.11.2:53061
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:

确定