package main import "fmt" import "net" import "net/http" import "net/url" //import "bufio" import "io/ioutil" import "bytes" import "encoding/binary" //import "compress/gzip" //import "testing" import "encoding/json" import b64 "encoding/base64" //import ( // //"code.google.com/p/mahonia" // "fmt" // "net" // "os" // "strings" //) func main() { fmt.Println("Hello, 世界") //resp, err := http.Get("http://www.baidu.com"); //payload := []byte("testing") //body := bytes.NewBuffer(payload) //res, err := http.Post("http://192.168.2.152/sanguo-tookit/Protocol?command=1000", // "application/x-www-form-urlencoded", body) //fmt.Println(res) //fmt.Println(err) v := make(url.Values) v.Set("command", "1000") res, err := http.PostForm("http://192.168.2.152/sanguo-tookit/Protocol", v) fmt.Println(res) fmt.Println(err) if err != nil { fmt.Println(err.Error()) return } result, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { fmt.Println(err.Error()) return } fmt.Printf("%s", result) ////result, err := ioutil.ReadAll(res.body) //res, err := http.Get("http://www.baidu.com") //fmt.Println(res) //fmt.Println(err) byt := []byte(result) var dat map[string]string if err := json.Unmarshal(byt, &dat); err != nil { panic(err) } fmt.Println("=======================") fmt.Println(dat["body"]) byt1 := []byte(dat["body"]) fmt.Println(len(byt1)) uDec, _ := b64.URLEncoding.DecodeString(dat["body"]) fmt.Println(len(uDec)) client, err := net.Dial("tcp", "192.168.2.152:9382") //client, err := net.Dial("tcp", "61.135.169.105:80") if err != nil { fmt.Println("server connect error", err.Error()) return } fmt.Println("connect server") // defer client.Close() //sendMsg := make(uDec) respMsg, err := client.Write(uDec) if err != nil { fmt.Println("message recv error", err.Error()) return } fmt.Println(respMsg) //client.Read() //fmt.Println(bufio.NewReader(client).Buffered()) //for { //} data := make([]byte, 49) client.Read(data) fmt.Println(data) aa := data[0] fmt.Println(data[0]) fmt.Println(aa) // read fingerprint 4 bytes fingerprint := make([]byte, 4) copy(fingerprint[:], data[0:4]) // from data to fingerprint fmt.Println(fingerprint) // read protocol version 1 byte protocolVersion := data[4] fmt.Println(protocolVersion) // read app id 4 bytes (int) appIdBytes := make([]byte, 4) copy(appIdBytes[:], data[5:9]) fmt.Println(appIdBytes) // read session id 32 bytes (string) sessionIdBytes := make([]byte, 32) copy(sessionIdBytes[:], data[9:41]) fmt.Println(sessionIdBytes) // read mask length 4 bytes (int) maskLengthBytes := make([]byte, 4) copy(maskLengthBytes[:], data[41:45]) fmt.Println(maskLengthBytes) // read command id 4 bytes(int) commandIdBytes := make([]byte, 4) copy(commandIdBytes[:], data[45:49]) fmt.Println(commandIdBytes) commandId := readInt(commandIdBytes) fmt.Println(commandId) //commandIdBuffer := bytes.NewBuffer(commandIdBytes) //var x int32 //binary.Read(commandIdBuffer, binary.BigEndian, &x) //fmt.Println(x) //data := make([]byte, 1024) //fmt.Println(data) //buf := bytes.NewBuffer(data) // b is []byte //finger := make([]byte, 49) //fmt.Println("=====fff======") //binary.Varint(finger) //fmt.Println(finger) //myfirstint, err := binary.ReadVarint(buf) ////anotherint, err := binary.ReadVarint(buf) //fmt.Println(myfirstint) ////fmt.Println(anotherint) ////data, err := read(client, 256) ////fmt.Println("===========") ////fmt.Println(len(data)) } func readInt(src []byte) int32 { readBuffer := bytes.NewBuffer(src) var intValue int32 binary.Read(readBuffer, binary.BigEndian, &intValue) return intValue } func read(conn net.Conn, length int) ([]byte, error) { data := make([]byte, length) buf_size := 1024 buf := make([]byte, buf_size) i := 0 for { if length < buf_size { if length == 0 { return data, nil } remain := make([]byte, length) r, err := conn.Read(remain) if err != nil { return nil, err } copy(data[i:(i+r)], remain[0:r]) i += r length -= r } else { r, err := conn.Read(buf) if err != nil { return nil, err } copy(data[i:(i+r)], buf[0:r]) i += r length -= r } } return data, nil }