Skip to content

Commit db95971

Browse files
committed
Add examples/syscall/syscallnoerror
1 parent 0def8b5 commit db95971

File tree

6 files changed

+189
-3
lines changed

6 files changed

+189
-3
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
app_noerror
2-
app_client
1+
app_*
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# これは何?
2+
3+
[unix](https://pkg.go.dev/golang.org/x/sys/unix) パッケージの
4+
5+
- SyscallNoError()
6+
7+
を使っているサンプルです。
8+
9+
ついでに、同じ実装を
10+
11+
- unix.Syscall()
12+
- unixパッケージのラッパー関数 (ex: unix.Listen()など)
13+
- 標準ライブラリの関数(net.Listen()など)
14+
15+
でしてみて、どのように変わるかも試しています。
16+
17+
## 実行結果
18+
19+
```sh
20+
task: [run_noerror] rm -f ./app_noerror
21+
task: [run_noerror] go build -o app_noerror main.go
22+
task: [run_noerror] go build -o app_client client/main.go
23+
task: [run_noerror] ./app_noerror &
24+
task: [run_noerror] sleep 1
25+
task: [run_noerror] ./app_client
26+
[accept] EP: 127.0.0.1:53418
27+
task: [run_noerror] pkill app_noerror
28+
-------------------------------------------------
29+
task: [run_unix] rm -f ./app_unix
30+
task: [run_unix] go build -o app_unix unix/main.go
31+
task: [run_unix] go build -o app_client client/main.go
32+
task: [run_unix] ./app_unix &
33+
task: [run_unix] sleep 1
34+
task: [run_unix] ./app_client
35+
[accept] EP: 127.0.0.1:34092
36+
task: [run_unix] pkill app_unix
37+
-------------------------------------------------
38+
task: [run_stdlib] rm -f ./app_stdlib
39+
task: [run_stdlib] go build -o app_stdlib stdlib/main.go
40+
task: [run_stdlib] go build -o app_client client/main.go
41+
task: [run_stdlib] ./app_stdlib &
42+
task: [run_stdlib] sleep 1
43+
task: [run_stdlib] ./app_client
44+
[accept] EP: 127.0.0.1:34094
45+
task: [run_stdlib] pkill app_stdlib
46+
```

examples/syscall/syscallnoerror/Taskfile.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ version: '3'
44

55
tasks:
66
default:
7+
cmds:
8+
- task: run_noerror
9+
- task: hr
10+
- task: run_unix
11+
- task: hr
12+
- task: run_stdlib
13+
hr:
14+
cmds:
15+
- echo '-------------------------------------------------'
16+
silent: true
17+
run_noerror:
718
cmds:
819
- rm -f ./app_noerror
920
- go build -o app_noerror main.go
@@ -13,3 +24,23 @@ tasks:
1324
- ./app_client
1425
- pkill app_noerror
1526
ignore_error: true
27+
run_unix:
28+
cmds:
29+
- rm -f ./app_unix
30+
- go build -o app_unix unix/main.go
31+
- go build -o app_client client/main.go
32+
- ./app_unix &
33+
- sleep 1
34+
- ./app_client
35+
- pkill app_unix
36+
ignore_error: true
37+
run_stdlib:
38+
cmds:
39+
- rm -f ./app_stdlib
40+
- go build -o app_stdlib stdlib/main.go
41+
- go build -o app_client client/main.go
42+
- ./app_stdlib &
43+
- sleep 1
44+
- ./app_client
45+
- pkill app_stdlib
46+
ignore_error: true

examples/syscall/syscallnoerror/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
func main() {
1414
log.SetFlags(0)
15-
1615
if err := run(); err != nil {
1716
log.Fatal(err)
1817
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net"
6+
"time"
7+
)
8+
9+
func main() {
10+
log.SetFlags(0)
11+
if err := run(); err != nil {
12+
log.Fatal(err)
13+
}
14+
}
15+
16+
func run() error {
17+
//
18+
// unix.SyscallNoError()を用いたサンプルと同じものを
19+
// 標準ライブライブラリで提供されている関数を使って実装
20+
//
21+
// 元のサンプルと同様にエラー処理は割愛する
22+
//
23+
var (
24+
ln net.Listener
25+
)
26+
ln, _ = net.Listen("tcp", ":8888")
27+
defer ln.Close()
28+
29+
for {
30+
var (
31+
conn net.Conn
32+
addr *net.TCPAddr
33+
)
34+
conn, _ = ln.Accept()
35+
addr = conn.RemoteAddr().(*net.TCPAddr)
36+
37+
log.Printf("[accept] EP: %v:%d", addr.IP, addr.Port)
38+
39+
go func(conn net.Conn) {
40+
time.Sleep(1 * time.Second)
41+
conn.Close()
42+
}(conn)
43+
}
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net"
6+
"time"
7+
8+
"golang.org/x/sys/unix"
9+
)
10+
11+
func main() {
12+
log.SetFlags(0)
13+
if err := run(); err != nil {
14+
log.Fatal(err)
15+
}
16+
}
17+
18+
func run() error {
19+
//
20+
// unix.SyscallNoError()を用いたサンプルと同じものを
21+
// unixパッケージで提供されている各ラッパー関数を使って実装
22+
//
23+
// 元のサンプルと同様にエラー処理は割愛する
24+
//
25+
26+
// socket(2)
27+
var (
28+
fd int
29+
)
30+
fd, _ = unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
31+
defer unix.Close(fd)
32+
33+
// setsockopt(2) (SO_REUSEADDR)
34+
_ = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
35+
36+
// ソケットアドレス生成
37+
var (
38+
sAddr = unix.SockaddrInet4{
39+
Port: 8888,
40+
Addr: [4]byte{127, 0, 0, 1},
41+
}
42+
)
43+
44+
// bind(2)
45+
_ = unix.Bind(fd, &sAddr)
46+
47+
// listen(2)
48+
_ = unix.Listen(fd, 5)
49+
50+
for {
51+
// accept(2)
52+
var (
53+
cfd int
54+
sa unix.Sockaddr
55+
ca *unix.SockaddrInet4
56+
)
57+
cfd, sa, _ = unix.Accept(fd)
58+
ca = sa.(*unix.SockaddrInet4)
59+
60+
log.Printf("[accept] EP: %v:%d", net.IP(ca.Addr[:]), ca.Port)
61+
62+
go func(fd int) {
63+
time.Sleep(1 * time.Second)
64+
_ = unix.Close(fd)
65+
}(cfd)
66+
}
67+
}

0 commit comments

Comments
 (0)