net.Listen tcp bind: The Requested address is not valid in its context

Hi,

I’m trying create a listener and when I launch the Listener I got the error

listen tcp 192.168.200.75:49156: bind: The requested address is not valid in its context.

My code

func main(){
ln, err := net.Listen(“tcp”, “192.168.200.75:49156”)
if err != nil {
fmt.Println(“Erro1:”, err) // handle error
return
}
for {
_, err := ln.Accept()
if err != nil {
fmt.Println(“Erro2:”, err)
}
}
}

I know that IP Address is correct because if I make net.Dial I got success in connection.

Can anyone help me tracking about what is happening?

Thanks

Hi. This IP-number is on the server you’re trying to run the script on? Or maybe the second time you run the program is the port not yet released? Close the connections returned by Accept()

Hi Johan…

Yes the IP-number is on the server.

There’s my all code

package main

import (
“fmt”
“net”
“os”
“os/signal”
“syscall”
)

func main() {
ln, err := net.Listen(“tcp”, “192.168.200.75:49156”)
if err != nil {
fmt.Println(“Erro1:”, err) // handle error
return
}
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(“Erro2:”, err)
}
go handleConnection(conn)
}

waitForExitSignal()

}

func waitForExitSignal() {
sigchan := make(chan os.Signal, 10)
signal.Notify(sigchan, syscall.SIGTERM, os.Interrupt)
<-sigchan
}

func handleConnection(conn net.Conn) {
fmt.Println(“Handling new connection…”)

// Close connection when this function ends
defer func() {
	fmt.Println("Closing connection...")
	conn.Close()
}()

}

The program stops in net.Listen

Thanks

Could you post the code following these instructions: How to post code on this forum it makes it a bit easier to read

And because the for loop never exits will waitForExitSignal() never be called. But this is not your problem. Try to connect to all interfaces on the server if you can with net.Listen(“tcp”, “:49156”)

If I call

net.Listen("tcp",":49156") 

the programs works fine, but I don’t get any messages from server. If I call with net.List(“tcp”,":49156") I get listen from all port 49159 of my network?

Thanks

No. It just listens om all interfaces on the machine. Instead of just the one with the ip-number you used before.

Are you sure that the up address you used belongs to the computer where you started the program?

The error message you have shown is usually indicating exactly that: there is no interface visible that has this IP assigned.

If you are on Linux you can use ip addr to list all interfaces and their IP addresses. If your IP is not in that list, you need to choose another one.

Hi,

Yes I’m shure. The IP address is of an People Counter device.

If I try a net.Dial(“tcp”, “192.168.200.75”) it works…

Thanks

That net.Dial succeeds does only mean, that some device with that IP is visible to your network, it does not mean that this address is bound to any interface on your computer.

Edit

I’m not sure what you mean by “People Counter Device”. Are you running your program on that device?

The people counter is a device that count the persons that pass through a zone, and when it detects a new passage, the device sends tcp message to port.

My program is running in my computer and what I need is listen the events on port of device.

Thanks

Then you need to listen on an address that is available on your computer and tell the device to send events to your computer.

I’m new to Go programming (about two weeks), but have some experience with socket programming using other languages. And I agree with NobbZ, It seems like you’re trying to bind to an IP address that’s not assigned to the device.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.