Reassemble partial packets on the client
The sender's network stack assembles tcp packets without regard for write boundaries, so the receiver has to be prepared for partial reads.
This commit is contained in:
parent
70d3daeccb
commit
3d9133f93f
12
tcpdelay.go
12
tcpdelay.go
|
@ -2,8 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"gopkg.in/alecthomas/kingpin.v2"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gopkg.in/alecthomas/kingpin.v2"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -63,13 +63,17 @@ func tcpdelayClient() {
|
||||||
o := 0
|
o := 0
|
||||||
buffer := make([]byte, 1024)
|
buffer := make([]byte, 1024)
|
||||||
for {
|
for {
|
||||||
n, err := conn.Read(buffer)
|
n := 0
|
||||||
|
for n < len(buffer) {
|
||||||
|
nr, err := conn.Read(buffer[n:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Cannot read from ", *serverAddress, err)
|
log.Fatal("Cannot read from ", *serverAddress, err.Error())
|
||||||
}
|
}
|
||||||
if n != len(buffer) {
|
if nr <= 0 {
|
||||||
log.Fatal("Read ", n, " bytes intead of ", len(buffer))
|
log.Fatal("Read ", n, " bytes intead of ", len(buffer))
|
||||||
}
|
}
|
||||||
|
n += nr
|
||||||
|
}
|
||||||
tr := time.Now().UnixNano()
|
tr := time.Now().UnixNano()
|
||||||
if string(buffer[0:8]) != "tcpdelay" {
|
if string(buffer[0:8]) != "tcpdelay" {
|
||||||
log.Fatal("Unexpected magic ", buffer[0:8])
|
log.Fatal("Unexpected magic ", buffer[0:8])
|
||||||
|
|
Loading…
Reference in New Issue