Skip to content

Commit

Permalink
read all data from connection after EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
ljmsc committed Dec 13, 2021
1 parent c27347f commit 4d0a1fd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions ch-2/echo-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ func echo(conn net.Conn) {
for {
// Receive data via conn.Read into a buffer.
size, err := conn.Read(b[0:])
if err == io.EOF {
log.Println("Client disconnected")
if err != nil && err != io.EOF {
log.Println("Unexpected error")
break
}
if err != nil {
log.Println("Unexpected error")

if err == io.EOF && size == 0 {
log.Println("Client disconnected")
break
}

log.Printf("Received %d bytes: %s", size, string(b))

// Send data via conn.Write.
Expand Down

0 comments on commit 4d0a1fd

Please sign in to comment.