-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
42 lines (36 loc) · 763 Bytes
/
Copy pathmain.go
File metadata and controls
42 lines (36 loc) · 763 Bytes
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
// TCP client that connects to localhost:8080
// and sends a message, then prints the response.
//
// Usage example:
//
// so run apps/echo-client hello
package main
import (
"solod.dev/so/net"
"solod.dev/so/os"
)
func main() {
message := "hello"
if len(os.Args) > 1 {
message = os.Args[1]
}
// Resolve the server address.
raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
if err != nil {
panic(err)
}
// A nil laddr lets the system choose the local address.
conn, err := net.DialTCP("tcp", nil, &raddr)
if err != nil {
panic(err)
}
defer conn.Close()
// Send a request and read the reply.
conn.Write([]byte(message))
var buf [256]byte
n, err := conn.Read(buf[:])
if err != nil {
panic(err)
}
println(string(buf[:n]))
}