-
Notifications
You must be signed in to change notification settings - Fork 1
/
TCPserver.go
64 lines (57 loc) · 1.36 KB
/
TCPserver.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"time"
)
/*
start this server:
go run TCPserver.go 8001
connect to this server:
nc 127.0.0.1 8001
*/
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide port number")
return
}
PORT := ":" + arguments[1]
// The `net.Listen()` function listens for connections.
//
// If the second parameter does not contain an IP address, but only a port number,
// `net.Listen()` will listen on all available IP addresses of the local system.
l, err := net.Listen("tcp", PORT)
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
// The `Accept()` function waits for the next connection and returns a generic `Conn` variable.
c, err := l.Accept()
if err != nil {
fmt.Println(err)
return
}
// The only thing that is wrong with this particular TCP server is that it can only serve the first TCP client,
// which is going to connect to it because the `Accept()` call is outside the for loop that is coming next.
for {
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
if strings.TrimSpace(string(netData)) == "STOP" {
fmt.Println("Exiting TCP server!")
c.Close()
return
}
fmt.Print("-> ", string(netData))
t := time.Now()
myTime := t.Format(time.RFC3339) + "\n"
c.Write([]byte(myTime))
}
}