Created
March 17, 2018 05:44
-
-
Save ks888/606aaf88bbb8cdbd4c297f67c935a03d to your computer and use it in GitHub Desktop.
TCP echo server/client to be used with strace
This file contains hidden or 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 ( | |
"log" | |
"net" | |
) | |
func main() { | |
conn, err := net.Dial("tcp", "localhost:8080") | |
if err != nil { | |
log.Fatal(err) | |
} | |
_, err = conn.Write([]byte("test")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
buff := make([]byte, 64) | |
_, err = conn.Read(buff) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = conn.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
This file contains hidden or 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 ( | |
"fmt" | |
"io" | |
"log" | |
"net" | |
) | |
func main() { | |
fmt.Println("\n===== net.Listen() =====") | |
ln, err := net.Listen("tcp", ":8080") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for { | |
fmt.Println("\n===== ln.Accept() =====") | |
conn, err := ln.Accept() | |
if err != nil { | |
log.Print(err) | |
continue | |
} | |
// Not start go routine here for the readability of the strace output. | |
fmt.Println("\n===== io.Copy() =====") | |
_, err = io.Copy(conn, conn) // blocked until the client closes the conn. | |
if err != nil { | |
log.Print(err) | |
continue | |
} | |
fmt.Println("\n===== conn.Close() =====") | |
if err = conn.Close(); err != nil { | |
log.Print(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment