Is the port 5000 on 192.168.153.239 open? If it is open, is your server running on that port?
https://stackoverflow.com/questions/2972600/no-connection-could-be-made-because-the-target-machine-actively-refused-it On Monday, 6 November 2017 19:30:22 UTC+5:30, 28911...@gmail.com wrote: > > I try to modify the program,but the result is: host:portfatal error: dial > tcp 192.168.153.239:5000: connectex: No connection could be made because > the target machine actively refused it.exit status 1.How to solve it?? > > 在 2017年11月6日星期一 UTC+8下午5:08:26,rog写道: > >> On 30 October 2017 at 06:55, <28911...@gmail.com> wrote: >> > I write this code in the follwings: >> > package main >> > >> > import ( >> > "fmt" >> > "net" >> > "os" >> > ) >> > >> > func main() { >> > service := ":5000" >> > tcpAddr, err := net.ResolveTCPAddr("tcp", service) >> > checkError(err) >> > listener, err := net.ListenTCP("tcp", tcpAddr) >> > checkError(err) >> > for i := 0; i < 10; i++ { >> > conn, err := listener.Accept() >> > if err != nil { >> > continue >> > } >> > handleClient(conn) >> > conn.Close() >> > } >> > } >> > func handleClient(conn net.Conn) { >> > var buf [512]byte >> > for { >> > n, err := conn.Read(buf[0:]) >> > if err != nil { >> > return >> > } >> > rAddr := conn.RemoteAddr() >> > fmt.Println("receive from client", rAddr.String(), string(buf[0:n])) >> > _, err2 := conn.Write([]byte("welcome client!")) >> > if err2 != nil { >> > return >> > } >> > } >> > } >> > func checkError(err error) { >> > if err != nil { >> > fmt.Fprintf(os.Stderr, "fatal error: %s", err.Error()) >> > os.Exit(1) >> > } >> > } >> > >> > >> > I try to run it in Sublime Text3 but it has no reaction and I try to >> run it >> > in running windows, it shows: >> > fatal error: lsiten tcp:5000:bind:Only one usage of each socket >> > address(protocol/network address/port) is normally permitted.exit >> status 1 >> >> From that error message, I suspect that you already have a running >> instance >> of your program - as the message says, you are only allowed to listen >> on a port once. >> >> To avoid that, you could listen on port zero, which will choose an >> arbitrary >> port (but then you'll need to print out the port number so that you can >> know which port to use on the client). >> >> By the way, your code is a little bit more complex than >> it needs to be. Instead of this: >> >> > service := ":5000" >> > tcpAddr, err := net.ResolveTCPAddr("tcp", service) >> > checkError(err) >> > listener, err := net.ListenTCP("tcp", tcpAddr) >> >> You could do: >> >> service := ":5000" >> listener, err := net.Listen("tcp", tcpAddr) >> >> Similarly when dialing - there's no need to call ResolveTCPAddr >> explicitly (and it's actually not as good if you do that, >> because Dial will automatically use multiple IP addresses >> if it needs to, but if you use ResolveTCPAddr, it can only use one). >> >> Hope this helps, >> >> rog. >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.