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
20
tcpdelay.go
20
tcpdelay.go
|
@ -2,8 +2,8 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
"fmt"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
|
@ -16,7 +16,7 @@ var (
|
|||
server = app.Command("server", "Run as server")
|
||||
listenAddress = server.Arg("address", "Address:port to listen on").String()
|
||||
|
||||
client = app.Command("client", "Request measurement")
|
||||
client = app.Command("client", "Request measurement")
|
||||
serverAddress = client.Arg("address", "Address:port to connect to").String()
|
||||
)
|
||||
|
||||
|
@ -63,12 +63,16 @@ func tcpdelayClient() {
|
|||
o := 0
|
||||
buffer := make([]byte, 1024)
|
||||
for {
|
||||
n, err := conn.Read(buffer)
|
||||
if err != nil {
|
||||
log.Fatal("Cannot read from ", *serverAddress, err)
|
||||
}
|
||||
if n != len(buffer) {
|
||||
log.Fatal("Read ", n, " bytes intead of ", len(buffer))
|
||||
n := 0
|
||||
for n < len(buffer) {
|
||||
nr, err := conn.Read(buffer[n:])
|
||||
if err != nil {
|
||||
log.Fatal("Cannot read from ", *serverAddress, err.Error())
|
||||
}
|
||||
if nr <= 0 {
|
||||
log.Fatal("Read ", n, " bytes intead of ", len(buffer))
|
||||
}
|
||||
n += nr
|
||||
}
|
||||
tr := time.Now().UnixNano()
|
||||
if string(buffer[0:8]) != "tcpdelay" {
|
||||
|
|
Loading…
Reference in New Issue