Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go: first example using Go #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The following examples show how enable Multipath TCP with different programming
- [Using Multipath TCP in python](python/README.md)
- [Using Multipath TCP in perl](perl/README.md)
- [Using Multipath TCP in Rust](rust/README.md)
- [Using Multipath TCP in Go](go/README.md)



Expand Down
14 changes: 14 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Using Multipath TCP in Go

To use Multipath TCP in Go, you can create the socket manually with syscalls
like you would do in other languages but this is not an usual and practical way.

Instead, from Go 1.21, MPTCP support has been added in the net package:

- https://go.dev/issue/56539
- https://go.dev/issue/59166

Examples:

- [simple client](mptcp-client.go)
- [simple server](mptcp-server.go)
38 changes: 38 additions & 0 deletions go/mptcp-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"log"
"net"
"os"
"time"
)

func main() {
addr := os.Args[1]

d := &net.Dialer{}
d.SetMultipathTCP(true)
conn, err := d.Dial("tcp", addr)
if err != nil {
log.Fatal(err)
}
defer conn.Close()

tcp, ok := conn.(*net.TCPConn)
if !ok {
log.Fatal("struct is not a TCPConn")
}

/* This API is still under discussion: https://go.dev/issue/59166
mptcp, err := tcp.MultipathTCP()
if err != nil {
log.Fatal(err)
}
*/mptcp := tcp != nil

log.Printf("connection to %s mptcp %t", addr, mptcp)

time.Sleep(time.Second)

conn.Close()
}
44 changes: 44 additions & 0 deletions go/mptcp-server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"log"
"net"
"os"
)

func main() {
addr := os.Args[1]

lc := &net.ListenConfig{}
lc.SetMultipathTCP(true)

ln, err := lc.Listen(context.Background(), "tcp", addr)
if err != nil {
log.Fatal(err)
}
defer ln.Close()

for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}

tcp, ok := conn.(*net.TCPConn)
if !ok {
log.Fatal("struct is not a TCPConn")
}

/* This API is still under discussion: https://go.dev/issue/59166
mptcp, err := tcp.MultipathTCP()
if err != nil {
log.Fatal(err)
}
*/mptcp := tcp != nil

log.Printf("connection from %s mptcp %t", addr, mptcp)

conn.Close()
}
}