- Go snippet for making a tcp connection with source port
1package main
2
3import (
4 "bufio"
5 "fmt"
6 "log"
7 "net"
8)
9
10func main() {
11 server, _ := net.ResolveTCPAddr("tcp", "google.com:80")
12 client, _ := net.ResolveTCPAddr("tcp", ":50000")
13 conn, err := net.DialTCP("tcp", client, server)
14 if err != nil {
15 log.Fatalf("%s", err)
16 }
17 defer conn.Close()
18 fmt.Println(conn.LocalAddr())
19 fmt.Println(conn.RemoteAddr())
20 fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
21 status, err := bufio.NewReader(conn).ReadString('\n')
22 fmt.Println(status)
23}