-
-
Save lukebakken/6a4d1a0dd4f7493142344a9b62a3f1df to your computer and use it in GitHub Desktop.
A simple echo server testing a few interesting Go language features, goroutines and channels.
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" | |
"net" | |
"os" | |
"time" | |
) | |
func main() { | |
servAddr := "localhost:3540" | |
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr) | |
if err != nil { | |
println("ResolveTCPAddr failed:", err.Error()) | |
os.Exit(1) | |
} | |
for i := 0; i < 4096; i++ { | |
fmt.Println("Echo client: ", i) | |
go echo_client(tcpAddr, i) | |
} | |
fmt.Println("WAITING!") | |
time.Sleep(5 * time.Minute) | |
} | |
func echo_client(tcpAddr *net.TCPAddr, i int) { | |
strEcho := fmt.Sprintf("hello %d", i) | |
conn, err := net.DialTCP("tcp", nil, tcpAddr) | |
defer conn.Close() | |
if err != nil { | |
fmt.Println("Dial failed:", err.Error()) | |
os.Exit(1) | |
} | |
_, err = conn.Write([]byte(strEcho)) | |
if err != nil { | |
println("Write to server failed:", err.Error()) | |
os.Exit(1) | |
} | |
println("write to server = ", strEcho) | |
reply := make([]byte, 1024) | |
_, err = conn.Read(reply) | |
if err != nil { | |
println("Write to server failed:", err.Error()) | |
os.Exit(1) | |
} | |
fmt.Println("reply from server=", string(reply)) | |
time.Sleep(5 * time.Minute) | |
// time.Sleep(10 * time.Second) | |
} |
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
// $ 6g echo.go && 6l -o echo echo.6 | |
// $ ./echo | |
// | |
// ~ in another terminal ~ | |
// | |
// $ nc localhost 3540 | |
package main | |
import ( | |
"net" | |
"bufio" | |
"strconv" | |
"fmt" | |
) | |
const PORT = 3540 | |
func main() { | |
server, err := net.Listen("tcp", ":" + strconv.Itoa(PORT)) | |
if server == nil { | |
panic(fmt.Sprintln("couldn't start listening: " , err)) | |
} | |
conns := clientConns(server) | |
for { | |
go handleConn(<-conns) | |
} | |
} | |
func clientConns(listener net.Listener) chan net.Conn { | |
ch := make(chan net.Conn) | |
i := 0 | |
go func() { | |
for { | |
client, err := listener.Accept() | |
if client == nil { | |
fmt.Println("couldn't accept: ", err) | |
continue | |
} | |
i++ | |
fmt.Printf("%d: %v <-> %v\n", i, client.LocalAddr(), client.RemoteAddr()) | |
ch <- client | |
} | |
}() | |
return ch | |
} | |
func handleConn(client net.Conn) { | |
b := bufio.NewReader(client) | |
for { | |
line, err := b.ReadBytes('\n') | |
if err != nil { // EOF, or worse | |
break | |
} | |
client.Write(line) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment