This is a creation in Article, where the information may have evolved or changed.
An extremely simple tcpip written in Golang net Echoserver
Keyword: Linux golang tcpip echoserver
Although the efficiency is not very ideal, but can give beginners reference
Package Main
Import (
"FMT"
"NET"
"OS"
"Runtime"
)
Func Doecho (c net. Conn) {
Defer C.close ()
for {
BUF: = Make ([]byte, 4096)
N, Err: = C.read (buf[:])
if n = = 0 | | Err! = Nil {
Return
}
C.write (Buf[0:n])
}
}
Func Dostart (host string) {
If host = = "" {
Fmt. PRINTLN ("Press input host name")
Return
}
Fmt. Println ("Listening:" + host)
ln, ERR: = Net. Listen ("TCP", host)
If err! = Nil {
Fmt. Printf ("Listen Error:")
Return
}
Defer Ln. Close ()
for {
C, err: = ln. Accept ()
If err! = Nil {
Continue
}
Doecho (c)
}
}
Func Main () {
Fmt. Println ("CPU Num:", runtime.) NUMCPU ())
Runtime. Gomaxprocs (runtime. NUMCPU ())
Dostart (OS. ARGS[1])
}