Skip to content

Commit 2ac82aa

Browse files
committed
Add tcp socket example
1 parent 5c3cffc commit 2ac82aa

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

examples/socket/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
- [TCP:基本パターン](./tcp_basic/)
44
- 一番基本的なパターン
5+
- [TCP:io.Copyを使う](./tcp_use_iocopy/)
6+
- io.Copyを使っているパターン
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
client/client
2+
server/server
3+
.task/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
version: '3'
2+
3+
tasks:
4+
default:
5+
cmds:
6+
- task: run
7+
run:
8+
cmds:
9+
- task: build-server
10+
- task: build-client
11+
- task: start-server
12+
- task: run-client
13+
build-server:
14+
dir: server
15+
cmds:
16+
- go build
17+
sources:
18+
- ./*.go
19+
generates:
20+
- server{{exeExt}}
21+
method: checksum
22+
build-client:
23+
dir: client
24+
cmds:
25+
- go build
26+
sources:
27+
- ./*.go
28+
generates:
29+
- client{{exeExt}}
30+
method: checksum
31+
start-server:
32+
dir: server
33+
cmds:
34+
- ./server &
35+
run-client:
36+
dir: client
37+
cmds:
38+
- ./client
39+
clean:
40+
cmds:
41+
- (cd client; go clean)
42+
- (cd server; go clean)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
"io"
7+
"log"
8+
"net"
9+
"os"
10+
)
11+
12+
var (
13+
appLog = log.New(os.Stdout, "[client] ", 0)
14+
//go:embed main.go
15+
data []byte
16+
)
17+
18+
func main() {
19+
//
20+
// Connect
21+
//
22+
laddr, _ := net.ResolveTCPAddr("tcp", "localhost:")
23+
raddr, _ := net.ResolveTCPAddr("tcp", "localhost:8888")
24+
25+
conn, _ := net.DialTCP("tcp", laddr, raddr)
26+
defer func() {
27+
appLog.Println("close connection...")
28+
conn.Close()
29+
}()
30+
31+
//
32+
// Send
33+
//
34+
conn.Write(data)
35+
appLog.Printf("%dbyte(s) send", len(data))
36+
37+
conn.CloseWrite()
38+
appLog.Println("close client-side write stream")
39+
40+
//
41+
// Recv
42+
//
43+
buf := new(bytes.Buffer)
44+
io.Copy(buf, conn)
45+
appLog.Printf("%dbyte(s) recv", len(buf.Bytes()))
46+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
"io"
7+
"log"
8+
"net"
9+
"os"
10+
11+
"github.com/devlights/gomy/errs"
12+
)
13+
14+
var (
15+
appLog = log.New(os.Stdout, "[server] ", 0)
16+
//go:embed main.go
17+
data []byte
18+
)
19+
20+
func main() {
21+
//
22+
// Start
23+
//
24+
server, _ := net.ListenTCP("tcp", errs.Drop(net.ResolveTCPAddr("tcp", "localhost:8888")))
25+
defer func() {
26+
server.Close()
27+
appLog.Println("shutting down...")
28+
}()
29+
30+
//
31+
// Accept
32+
//
33+
conn, _ := server.AcceptTCP()
34+
defer func() {
35+
appLog.Println("close connection...")
36+
conn.Close()
37+
}()
38+
39+
//
40+
// Recv
41+
//
42+
buf := new(bytes.Buffer)
43+
io.Copy(buf, conn)
44+
appLog.Printf("%dbyte(s) recv", len(buf.Bytes()))
45+
46+
//
47+
// Send
48+
//
49+
conn.Write(data)
50+
appLog.Printf("%dbyte(s) send", len(data))
51+
conn.CloseWrite()
52+
appLog.Println("close server-side write stream")
53+
}

0 commit comments

Comments
 (0)