package main import ( "fmt" "net" ) func main() { listener, _ := net.Listen("tcp", "127.0.0.1:5000") for { conn, _ := listener.Accept() remoteAddr := conn.RemoteAddr().String() fmt.Println("Client connected from", remoteAddr) conn.Close() } }
package main import ( "fmt" "net" ) func main() { conn, _ := net.Dial("tcp", "google.com:80") remoteAddr := conn.RemoteAddr().String() fmt.Println("Connected to", remoteAddr) conn.Close() }In the above example, we connect to Google's web server on port 80 using the Dial function of the net package. Once the connection is established, we get the remote address of the server using the RemoteAddr method of TCPConn and log it to the console. Both of the above examples use the net package of Go.