-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (44 loc) · 903 Bytes
/
main.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 (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"github.com/reynoldsbd3/golang-challenge2/secnet"
)
// Serve starts a secure echo server on the given listener.
func Serve(l net.Listener) error {
return nil
}
func main() {
port := flag.Int("l", 0, "Listen mode. Specify port")
flag.Parse()
// Server mode
if *port != 0 {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatal(err)
}
defer l.Close()
log.Fatal(Serve(l))
}
// Client mode
if len(os.Args) != 3 {
log.Fatalf("Usage: %s <port> <message>", os.Args[0])
}
conn, err := secnet.Dial("localhost:" + os.Args[1])
if err != nil {
log.Fatal(err)
}
if _, err := conn.Write([]byte(os.Args[2])); err != nil {
log.Fatal(err)
}
buf := make([]byte, len(os.Args[2]))
n, err := conn.Read(buf)
if err != nil && err != io.EOF {
log.Fatal(err)
}
fmt.Printf("%s\n", buf[:n])
}