Created
May 16, 2016 03:48
-
-
Save higebu/96c09c8f03113997fd9a5338ca99764a to your computer and use it in GitHub Desktop.
Golang: Connect with specific source port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"net" | |
) | |
func main() { | |
server, _ := net.ResolveTCPAddr("tcp", "google.com:80") | |
client, _ := net.ResolveTCPAddr("tcp", ":50000") | |
conn, err := net.DialTCP("tcp", client, server) | |
if err != nil { | |
log.Fatalf("%s", err) | |
} | |
defer conn.Close() | |
fmt.Println(conn.LocalAddr()) | |
fmt.Println(conn.RemoteAddr()) | |
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") | |
status, err := bufio.NewReader(conn).ReadString('\n') | |
fmt.Println(status) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment