Skip to content

Instantly share code, notes, and snippets.

@madper
Created August 21, 2017 14:12
Show Gist options
  • Save madper/a9eebfc4c85add4eb8ae0d7423b7c817 to your computer and use it in GitHub Desktop.
Save madper/a9eebfc4c85add4eb8ae0d7423b7c817 to your computer and use it in GitHub Desktop.
read len first.
package main
import "net"
import "io"
import "encoding/binary"
import "fmt"
func handleConnection(conn net.Conn) {
defer conn.Close()
p := make([]byte, 4)
_, err := io.ReadFull(conn, p)
if err != nil {
return
}
len := binary.LittleEndian.Uint32(p)
fmt.Println("len: ", len)
s := make([]byte, len)
_, err = io.ReadFull(conn, s)
fmt.Printf("str: %s", string(s))
}
func main() {
ln, _ := net.Listen("tcp", ":8888")
defer ln.Close()
for {
conn, _ := ln.Accept()
go handleConnection(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment