Skip to content

Commit ce7a573

Browse files
committed
Update examples/singleapp/unix/socket/03.nonblocking
1 parent 5410b8e commit ce7a573

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

examples/singleapp/unix/socket/03.nonblocking/client/client.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,25 @@ func run() error {
7373
for {
7474
copy(buf, []byte(msg))
7575

76-
_, err = unix.Write(sfd, buf[:len(msg)])
76+
err = unix.Send(sfd, buf[:len(msg)], 0)
7777
if err != nil {
78-
if errors.Is(err, unix.EAGAIN) {
78+
// 基本的に大抵のOSでは EAGAIN と EWOULDBLOCK は同じコードを示す (0xb) ので
79+
// EAGAINのみを見ていれば良いが、man send(2)の記載では EAGAIN または EWOULDBLOCK を返すと
80+
// 記載があるため、両方見ておくのが無難。
81+
//
82+
// send(2)
83+
// - https://ja.manpages.org/send/2
84+
switch {
85+
case errors.Is(err, unix.EAGAIN):
7986
log.Println("[CLIENT][SEND] --> unix.EAGAIN")
80-
81-
time.Sleep(100 * time.Millisecond)
82-
continue
87+
case errors.Is(err, unix.EWOULDBLOCK):
88+
log.Println("[CLIENT][SEND] --> unix.EWOULDBLOCK")
89+
default:
90+
return err
8391
}
8492

85-
return err
93+
time.Sleep(100 * time.Millisecond)
94+
continue
8695
}
8796

8897
log.Printf("[CLIENT] SEND %s", msg)
@@ -101,14 +110,23 @@ func run() error {
101110

102111
n, err = unix.Read(sfd, buf)
103112
if err != nil {
104-
if errors.Is(err, unix.EAGAIN) {
113+
// 基本的に大抵のOSでは EAGAIN と EWOULDBLOCK は同じコードを示す (0xb) ので
114+
// EAGAINのみを見ていれば良いが、man send(2)の記載では EAGAIN または EWOULDBLOCK を返すと
115+
// 記載があるため、両方見ておくのが無難。
116+
//
117+
// read(2)
118+
// - https://ja.manpages.org/read/2
119+
switch {
120+
case errors.Is(err, unix.EAGAIN):
105121
log.Println("[CLIENT][RECV] --> unix.EAGAIN")
106-
107-
time.Sleep(50 * time.Millisecond)
108-
continue
122+
case errors.Is(err, unix.EWOULDBLOCK):
123+
log.Println("[CLIENT][RECV] --> unix.EWOULDBLOCK")
124+
default:
125+
return err
109126
}
110127

111-
return err
128+
time.Sleep(50 * time.Millisecond)
129+
continue
112130
}
113131

114132
log.Printf("[CLIENT] RECV %s", buf[:n])

examples/singleapp/unix/socket/03.nonblocking/server/server.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func run() error {
9393
for {
9494
cfd, cAddr, err = unix.Accept(sfd)
9595
if err != nil {
96-
if errors.Is(err, unix.EAGAIN) {
97-
log.Println("[SERVER][ACCEPT] --> unix.EGAIN")
96+
if errors.Is(err, unix.EAGAIN) || errors.Is(err, unix.EWOULDBLOCK) {
97+
log.Println("[SERVER][ACCEPT] --> unix.EAGAIN")
9898

9999
time.Sleep(100 * time.Millisecond)
100100
continue
@@ -134,9 +134,13 @@ func run() error {
134134
if err != nil {
135135
return err
136136
}
137-
138137
log.Printf("[SERVER] RECV %s", string(buf[:n]))
139138

139+
// クライアントから受信した値を使って「何かの処理」を行った後に
140+
// クライアント側に返送するという流れをシミュレートするために
141+
// 意図的に少しディレイを入れる
142+
time.Sleep(150 * time.Millisecond)
143+
140144
//
141145
// Send
142146
//
@@ -152,7 +156,6 @@ func run() error {
152156
if err != nil {
153157
return err
154158
}
155-
156159
log.Printf("[SERVER] SEND %s", buf[:len(msg)])
157160
}
158161

0 commit comments

Comments
 (0)