43 lines
681 B
Go
43 lines
681 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
)
|
|
|
|
const (
|
|
connPort = "3030"
|
|
connType = "tcp"
|
|
)
|
|
|
|
func main() {
|
|
l, err := net.Listen(connType, ":"+connPort)
|
|
if err != nil {
|
|
fmt.Printf("net.Listen fail: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer l.Close()
|
|
|
|
fmt.Println(fmt.Sprintf("Listening on port %s", connPort))
|
|
|
|
for {
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
fmt.Printf("Error accepting: %v\n", err)
|
|
}
|
|
go handleRequest(conn)
|
|
}
|
|
}
|
|
|
|
func handleRequest(conn net.Conn) {
|
|
buf := make([]byte, 1024)
|
|
_, err := conn.Read(buf)
|
|
fmt.Printf("read in %s.\nSending it back\n", string(buf))
|
|
if err != nil {
|
|
fmt.Printf("Error reading: %v\n", err)
|
|
}
|
|
conn.Write(buf)
|
|
conn.Close()
|
|
}
|