golang first network program example code (for archvie)

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
}

 

Sure, here's some example Go code for basic matrix operations: ```go package main import ( "fmt" ) type Matrix [][]int func main() { // create two matrices matrix1 := Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} matrix2 := Matrix{{9, 8, 7}, {6, 5, 4}, {3, 2, 1}} // print the matrices fmt.Println("Matrix 1:") printMatrix(matrix1) fmt.Println("Matrix 2:") printMatrix(matrix2) // perform matrix addition matrix3 := addMatrices(matrix1, matrix2) fmt.Println("Matrix 3 (matrix1 + matrix2):") printMatrix(matrix3) // perform matrix multiplication matrix4 := multiplyMatrices(matrix1, matrix2) fmt.Println("Matrix 4 (matrix1 * matrix2):") printMatrix(matrix4) } func addMatrices(matrix1 Matrix, matrix2 Matrix) Matrix { rows := len(matrix1) cols := len(matrix1[0]) result := make(Matrix, rows) for i := 0; i < rows; i++ { result[i] = make([]int, cols) for j := 0; j < cols; j++ { result[i][j] = matrix1[i][j] + matrix2[i][j] } } return result } func multiplyMatrices(matrix1 Matrix, matrix2 Matrix) Matrix { rows1 := len(matrix1) cols1 := len(matrix1[0]) rows2 := len(matrix2) cols2 := len(matrix2[0]) if cols1 != rows2 { panic("Invalid matrix dimensions for multiplication") } result := make(Matrix, rows1) for i := 0; i < rows1; i++ { result[i] = make([]int, cols2) for j := 0; j < cols2; j++ { sum := 0 for k := 0; k < cols1; k++ { sum += matrix1[i][k] * matrix2[k][j] } result[i][j] = sum } } return result } func printMatrix(matrix Matrix) { for i := 0; i < len(matrix); i++ { fmt.Println(matrix[i]) } fmt.Println() } ``` This code defines a `Matrix` type as a slice of slices of integers. It then defines functions for adding and multiplying matrices, as well as a function for printing matrices. The `main` function creates two matrices, performs matrix addition and multiplication, and prints the results.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值