forked from cmu440/intro-to-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.go
36 lines (31 loc) · 713 Bytes
/
05.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
package main
import (
"fmt"
"net"
"os" // go fmt puts these in alphbetical order.
)
func main() {
ln, err := net.Listen("tcp", ":15440")
if err != nil { // No ()s around cond
fmt.Println("Error on listen: ", err)
os.Exit(-1)
}
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)
}
fmt.Println("Reading once from connection")
var buf [1024]byte
n, err := conn.Read(buf[:])
if err != nil {
fmt.Println("Error on read: ", err)
os.Exit(-1)
}
fmt.Println("Client sent: ", string(buf[0:n]))
conn.Close()
}
fmt.Println("Exiting")
}