package main
import (
"bufio"
"fmt"
"io"
"net"
)
func main() {
l, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
}
ID := 0
for {
fmt.Println("Waiting for client...")
conn, err := l.Accept()
if err != nil {
panic(err)
}
fmt.Printf("Client ID: %d connected.
", ID)
go func(c net.Conn, clientID int) {
fmt.Fprintf(conn, "Welcome client ID: %d
", clientID)
for {
msg, err := bufio.NewReader(conn).ReadString('
')
if err != nil {
fmt.Println(err)
break
}
_, err = io.WriteString(conn, "Received: "+string(msg))
if err != nil {
fmt.Println(err)
break
}
}
fmt.Println("Closing connection")
conn.Close()
}(conn, ID)
ID++
}
}
/*
telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Welcome client ID: 1
dfsdaf
Received: dfsdaf
dsfasnf
Received: dsfasnf
sdfa
Received: sdfa
*/