我有以下 http 客戶端/服務器代碼:服務器func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Println("Req: ", r.URL) w.Write([]byte("OK")) // <== PROBLEMATIC LINE // w.WriteHeader(200) // Works as expected }) log.Fatal(http.ListenAndServe(":5008", nil))}客戶func main() { client := &http.Client{} for i := 0; i < 500; i++ { url := fmt.Sprintf("http://localhost:5008/%02d", i) req, _ := http.NewRequest("GET", url, nil) _, err := client.Do(req) if err != nil { fmt.Println("error: ", err) } else { fmt.Println("success: ", i) } time.Sleep(10 * time.Millisecond) }}當我在服務器上運行上面的客戶端時,在 250 個連接之后,我從 client.Do 收到以下錯誤:error: Get http://localhost:5008/250: dial tcp: lookup localhost: no such host并且沒有更多的連接會成功。但是,如果我從w.Write([]byte("OK"))==>更改服務器中的行,w.WriteHeader(200)則連接數量沒有限制,并且可以按預期工作。我在這里缺少什么?
- 3 回答
- 0 關注
- 243 瀏覽
添加回答
舉報
0/150
提交
取消