Last active
October 29, 2015 15:54
-
-
Save mangalaman93/4fbe12f4169428a6c76a to your computer and use it in GitHub Desktop.
setting low level socket options in golang (SO_PRIORITY, SO_REUSEADDR)
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" | |
"net" | |
"os" | |
"syscall" | |
) | |
const ( | |
PRIORITY = 2 | |
PORT = "6000" | |
) | |
func main() { | |
con, err := net.Dial("tcp", "127.0.0.1:"+PORT) | |
if err != nil { | |
fmt.Println("error in connecting to server:", err) | |
os.Exit(1) | |
} | |
fmt.Println("connected to:", con.RemoteAddr()) | |
defer con.Close() | |
tcpconn, ok := con.(*net.TCPConn) | |
if !ok { | |
fmt.Println("error in casting *net.Conn to *net.TCPConn!") | |
os.Exit(1) | |
} | |
file, err := tcpconn.File() | |
if err != nil { | |
fmt.Println("error in getting file for the connection!") | |
os.Exit(1) | |
} | |
err = syscall.SetsockoptInt(int(file.Fd()), syscall.SOL_SOCKET, syscall.SO_PRIORITY, PRIORITY) | |
file.Close() | |
if err != nil { | |
fmt.Println("error in setting priority option on socket:", err) | |
os.Exit(1) | |
} | |
fmt.Fprintf(con, "Hello World!\n") | |
message, _ := bufio.NewReader(con).ReadString('\n') | |
fmt.Print("Message Received: ", string(message)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment