13.9 处理客户端接收到的数据
/Users/qinjianquan/publicChain/Part78-Net-Conn
使用goroutine并行处理请求
/Users/qinjianquan/publicChain/Part78-Net-Conn/src/Server.go
func StartServer(nodeID string, mineAddress string) {
--
for {
//receive data from client
conn, err1 := ln.Accept()
if err != nil {
log.Panic(err1)
}
go HandleConnection(conn)
}
}
func HandleConnection(conn net.Conn) {
//read data from client
request, err := ioutil.ReadAll(conn)
if err != nil {
log.Panic(err)
}
fmt.Printf("received a message:%s\n", request[:COMMANDLENGTH])
}
配置handle方法文章来源:https://www.toymoban.com/news/detail-403892.html
func handleAddr(request []byte, bc *Blockchain) {}
func handleVersion(request []byte, bc *Blockchain) {}
func handleBlock(request []byte, bc *Blockchain) {}
func handleInv(request []byte, bc *Blockchain) {}
func handleGetblocks(request []byte, bc *Blockchain) {}
func handleTx(request []byte, bc *Blockchain) {}
func handleGetData(request []byte, bc *Blockchain) {}
配置常量文章来源地址https://www.toymoban.com/news/detail-403892.html
const COMMAND_VERSION = "version"
const COMMAND_ADDR = "addr"
const COMMAND_BLOCK = "block"
const COMMAND_INV = "inv"
const COMMAND_GETBLOCKS = "getblocks"
const COMMAND_GETDATA = "getdata"
const COMMAND_TX = "tx"
到了这里,关于区块链项目 - 13 简易的网络服务的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!