TCP echo server/client to be used with strace
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 ( | |
"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 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