Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: The requested address is not valid in its context. #34171

eycorsican opened this issue Sep 7, 2019 · 4 comments

net: The requested address is not valid in its context. #34171

eycorsican opened this issue Sep 7, 2019 · 4 comments


Copy link

@eycorsican eycorsican commented Sep 7, 2019

What version of Go are you using (go version)?

$ go version
go version go1.13 windows/amd64

Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\eric\AppData\Local\go-build
set GOENV=C:\Users\eric\AppData\Roaming\go\env
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\eric\go
set GOPROXY=,direct
set GOROOT=c:\go
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\eric\AppData\Local\Temp\go-build933876154=/tmp/go-build -gno-record-gcc-switches

What did you do?

Run the following code, is the IP address of the local network adapter.

package main

import (

func main() {
        d := &net.Dialer{
                LocalAddr: &net.TCPAddr{
                        IP: net.ParseIP(""),
                        Port: 0,
        c, e := d.Dial("tcp", "")
        if e != nil {
        log.Printf("conn %v %v %v", c, c.LocalAddr(), c.RemoteAddr())

What did you expect to see?

Successfully connects to the TCP server listening at ":8080".

What did you see instead?

panic: dial tcp> connectex: The requested address is not valid in its context.

goroutine 1 [running]:
C:/share/gotest/main.go:17 +0x2bf
exit status 2

I am not sure if this is the expected behavior on Windows, but the code works fine on macOS, if that's expected, perhaps we should have it documented.

Copy link

@antong antong commented Sep 8, 2019

This is not a Go issue, it is a Windows feature (google "strong host model"). The same thing happens with any other programming language. You can connect to "" in your example, but not as you bound to another interface. What are you trying to achieve by binding the tcp client to a specific local address?

Copy link

@eycorsican eycorsican commented Sep 8, 2019

There are multiple default routes to different gateways on my machine, something looks like this:

Network Destination        Netmask          Gateway       Interface  Metric

By default traffic are going out through, while some traffic are desired to go out through, via binding to that interface.

But if it's the case that the destination address is, and we are binding to, the above error occurs. In fact, it's quite easy to distinguish traffic targeting and don't bind to a specific interface while sending them, just would like to know if there is a cleaner solution.

@antong I tried to disable the strong host model with the following commands, and the program still fails, am I missing something?

PS C:\Windows\system32> netsh interface ipv4 set interface "WiFi" WeakHostSend=enabled

PS C:\Windows\system32> netsh interface ipv4 set interface "WiFi" WeakHostReceive=enabled

PS C:\Windows\system32> netsh interface ipv4 show interface 'WiFi'

Interface WiFi Parameters
IfLuid                             : wireless_32768
IfIndex                            : 24
State                              : connected
Metric                             : 100
Link MTU                           : 1500 bytes
Reachable Time                     : 36500 ms
Base Reachable Time                : 30000 ms
Retransmission Interval            : 1000 ms
DAD Transmits                      : 3
Site Prefix Length                 : 64
Site Id                            : 1
Forwarding                         : enabled
Advertising                        : disabled
Neighbor Discovery                 : enabled
Neighbor Unreachability Detection  : enabled
Router Discovery                   : dhcp
Managed Address Configuration      : enabled
Other Stateful Configuration       : enabled
Weak Host Sends                    : enabled
Weak Host Receives                 : enabled
Use Automatic Metric               : disabled
Ignore Default Routes              : disabled
Advertised Router Lifetime         : 1800 seconds
Advertise Default Route            : disabled
Current Hop Limit                  : 0
Force ARPND Wake up patterns       : disabled
Directed MAC Wake up patterns      : disabled
ECN capability                     : application
RA Based DNS Config (RFC 6106)     : disabled
DHCP/Static IP coexistence         : disabled
Copy link

@eycorsican eycorsican commented Sep 8, 2019

I also tried to send traffic to another interface (, with the strong host model enabled for both interfaces ( and, I got a different error:

panic: dial tcp> connectex: A connection attempt failed because the connected party did not properly respond after a period of time
, or established connection failed because connected host has failed to respond.

With the strong host model disabled, it worked.

But it failed anyway when sending to the loopback interface, it's seems that the loopback is somewhat special, and it's not related to the strong host model.

Copy link

@eycorsican eycorsican commented Sep 8, 2019

Closing since it's indeed not a Go specific issue.

@eycorsican eycorsican closed this Sep 8, 2019
@golang golang locked and limited conversation to collaborators Sep 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.