-
Notifications
You must be signed in to change notification settings - Fork 20
/
08.go
50 lines (44 loc) · 852 Bytes
/
08.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"net"
"os"
)
type myConn struct {
conn net.Conn
prefix string
}
func handleConn(mconn *myConn) {
fmt.Println("Reading once from connection")
var buf [1024]byte
n, err := mconn.conn.Read(buf[:])
if err != nil {
fmt.Println("Error on read: ", err)
os.Exit(-1)
}
fmt.Println(mconn.prefix, ":", string(buf[0:n]))
mconn.conn.Close()
}
func main() {
ln, err := net.Listen("tcp", ":15440")
if err != nil {
fmt.Println("Error on listen: ", err)
os.Exit(-1)
}
connNumber := 0
for {
fmt.Println("Waiting for a connection via Accept")
conn, err := ln.Accept()
if err != nil {
fmt.Println("Error on accept: ", err)
os.Exit(-1)
}
mconn := &myConn{
conn:conn,
prefix: fmt.Sprintf("%d says", connNumber),
}
go handleConn(mconn)
connNumber++
}
fmt.Println("Exiting")
}