Update 15.1.md

This commit is contained in:
glight2000
2015-12-08 20:18:20 +08:00
parent 6ba196338e
commit b5f526993f

View File

@@ -225,6 +225,7 @@ func main() {
go connectionHandler(conn) go connectionHandler(conn)
} }
} }
func initServer(hostAndPort string) *net.TCPListener { func initServer(hostAndPort string) *net.TCPListener {
serverAddr, err := net.ResolveTCPAddr("tcp", hostAndPort) serverAddr, err := net.ResolveTCPAddr("tcp", hostAndPort)
checkError(err, "Resolving address:port failed: '"+hostAndPort+"'") checkError(err, "Resolving address:port failed: '"+hostAndPort+"'")
@@ -233,6 +234,7 @@ func initServer(hostAndPort string) *net.TCPListener {
println("Listening to: ", listener.Addr().String()) println("Listening to: ", listener.Addr().String())
return listener return listener
} }
func connectionHandler(conn net.Conn) { func connectionHandler(conn net.Conn) {
connFrom := conn.RemoteAddr().String() connFrom := conn.RemoteAddr().String()
println("Connection from: ", connFrom) println("Connection from: ", connFrom)
@@ -255,11 +257,13 @@ DISCONNECT:
println("Closed connection: ", connFrom) println("Closed connection: ", connFrom)
checkError(err, "Close: ") checkError(err, "Close: ")
} }
func sayHello(to net.Conn) { func sayHello(to net.Conn) {
obuf := []byte{'L', 'e', 't', '\'', 's', ' ', 'G', 'O', '!', '\n'} obuf := []byte{'L', 'e', 't', '\'', 's', ' ', 'G', 'O', '!', '\n'}
wrote, err := to.Write(obuf) wrote, err := to.Write(obuf)
checkError(err, "Write: wrote "+string(wrote)+" bytes.") checkError(err, "Write: wrote "+string(wrote)+" bytes.")
} }
func handleMsg(length int, err error, msg []byte) { func handleMsg(length int, err error, msg []byte) {
if length > 0 { if length > 0 {
print("<", length, ":") print("<", length, ":")
@@ -272,6 +276,7 @@ func handleMsg(length int, err error, msg []byte) {
print(">") print(">")
} }
} }
func checkError(error error, info string) { func checkError(error error, info string) {
if error != nil { if error != nil {
panic("ERROR: " + info + " " + error.Error()) // terminate program panic("ERROR: " + info + " " + error.Error()) // terminate program