All new technologies have experienced some growing pains, and blockchain technology is the same. However, although it is still at an early stage of development, it has great potential to improve many industries, many applications and systems.
The go language has its key features. The go language is so powerful because it always captures the programmer's itching needs in the development of the server, and solves and implements the problem in the most direct, simple, efficient and stable way.
For a simple content today, write a simple echo server with Golang.
TCP/IP protocol
We all know that the TCP/IP protocol cluster is the fact standard protocol of today's network world, and it corresponds to the OSI seven-layer model, for this seven-layer model is no longer described here, here is just a simple introduction of TCP/IP four-tier model.
TCP/IP four-layer model the bottom is a physical layer, which is the network layer, and then the transport layer, and finally the application layer, the concept of layering to believe that everyone, so the change between the different layers is packaged or unpacked, simply to add the head information or to turn the information.
Golang API
Golang NET package has a very convenient API for us to use, this API can be divided into Addr,listen,connection,dialer,ip and other network APIs, such as DNS API, etc. There are no other Web APIs (mainly self-study _).
Addr mainly have ipaddr,tcpaddr,udpaddr and unixaddr, the main is different address, this is the basis of network programming, no address is like a letter without an address, can not send Ah!
Listener is mainly Tcplistener,unixlistener (why, why not Udplistener?) ), listener is generally a service-side need to call, because the server needs to monitor-listen to the client Ah, always look at Y to not come to greet me ah.
Connection is connected, it is two nodes communication bridge ah, without it you are not with the object of the words AH.
Dialer is the requestor ah, when the server is ready, the client can dial a bit, if the smooth can get a connection, so that you can talk to the other end of the person.
Simple echo Server
OK, now with an echo server to simply show how to use TCP for network communication, of course, you can also use UDP.
Server
Package Main
Import (
"NET"
"OS"
"FMT"
"IO"
)
Const BUFF_SIZE = 1024
var buff = make ([]byte,buff_size)
Accept a tcpconn processing content
Func handleconn (Tcpconn *net. Tcpconn) {
if Tcpconn = = Nil {
Return
}
for {
N,err: = Tcpconn.read (Buff)
If err = = Io. EOF {
Fmt. Printf ("The remoteaddr:%s is closed!\n", tcpconn.remoteaddr (). String ())
Return
}
HandleError (ERR)
If string (buff[:n]) = = "Exit" {
Fmt. Printf ("The client:%s has exited\n", tcpconn.remoteaddr (). String ())
}
If n > 0 {
Fmt. Printf ("read:%s", String (Buff[:n]))
}
}
}
Error handling
Func handleError (err error) {
If Err = = Nil {
Return
}
Fmt. Printf ("error:%s\n", err. Error ())
}
Func Main () {
If Len (OS. Args)! = 2 {
Fmt. Println ("Usage:")
Return
}
PORT: = OS. ARGS[1]
Tcpaddr,err: = Net. RESOLVETCPADDR ("TCP4", "localhost:" +port)
HandleError (ERR)
Tcplistener,err: = Net. Listentcp ("TCP4", tcpaddr)//monitoring
HandleError (ERR)
Defer Tcplistener.close ()
for {
Tcpconn,err: = Tcplistener.accepttcp ()
Fmt. Printf ("The client:%s has connected!\n", tcpconn.remoteaddr (). String ())
HandleError (ERR)
Defer Tcpconn.close ()
Go Handleconn (Tcpconn)//from a goroutine processing
}
}
Client
Package Main
Import (
"NET"
"FMT"
"OS"
"Bufio"
)
Const BUFF_SIZE = 1024
var input = make ([]byte,buff_size)
Func handleError (err error) {
If Err = = Nil {
Return
}
Fmt. Printf ("error:%s\n", err. Error ())
}
Func Main () {
If Len (OS. Args)! = 2 {
Fmt. Println ("Usage:")
Return
}
PORT: = OS. ARGS[1]
Tcpaddr,err: = Net. RESOLVETCPADDR ("TCP4", "localhost:" +port)
HandleError (ERR)
Tcpconn,err: = Net. Dialtcp ("TCP4", nil,tcpaddr)
HandleError (ERR)
Reader: = Bufio. Newreader (OS. Stdin)
var continued = True
var inputstr string
For continued {
N,err: = reader. Read (Input)
HandleError (ERR)
If n > 0 {
K,_: = Tcpconn.write (Input[:n])
If k > 0 {
Inputstr = string (Input[:k])
Fmt. Printf ("write:%s", Inputstr)
if inputstr = = "exit\n" {//In comparison due to a carriage return character, so add \ n
Continued = false//can also be inputstr = TrimRight (inputstr, "\ n")
}
}
}
}
}
high-energy early warning, brother even education blockchain live course August continues to hit the hot!
The original price of 1188 Yuan of 12 block chain advanced courses, now only need 1 Yuan!
Also can receive free "Go Language foundation actual combat project development" and "Go language Advanced Combat Project development" textbook Two!!
Limited time limit!! First come first served!!
Http://www.ydma.cn/open/course/24
Follow brother Lian Blockchain technology public number get more technical dry Goods Oh!!!