Created
August 21, 2017 14:12
-
-
Save madper/a9eebfc4c85add4eb8ae0d7423b7c817 to your computer and use it in GitHub Desktop.
read len first.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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