This article analyzes the Go Language Simple network program. Share to everyone for your reference. The specific analysis is as follows:
The service-side code is as follows:
Copy Code code as follows:
Package Main
Import (
"NET"
"OS"
)
Func serve (s net. Conn) {
var buf [1024]byte
for {
N, Err: = S.read (&BUF)
If Err!= Nil | | n = = 0 {
Break
}
S.write (Buf[0:n])
}
}
Func Main () {
L, E: = Net. Listen ("TCP", ": 12345")
If e!= Nil {
Os. Exit (-1)
}
for {
S, E: = L.accept ()
If e!= Nil {
Os. Exit (-1)
}
Go serve (s)
}
}
The client code is as follows:
Copy Code code as follows:
Package Main
Import (
"NET"
"OS"
)
Func Main () {
S, err: = Net. Dial ("TCP", "", "192.168.1.168:12345")
If Err!= nil {
Os. Exit (-1)
}
for {
var buf [1024]byte
N, err: = OS. Stdin.read (&BUF)
If Err!= Nil | | n = = 0 {
Os. Exit (-1)
}
S.write (Buf[0:n])
N, err = S.read (&BUF)
If Err!= Nil | | n = = 0 {
Os. Exit (-1)
}
Os. Stdin.write (Buf[0:n])
}
}
I hope this article will help you with your go language program.