// handle each connection as a thread, say Hello, then receive data: func connHandler(conn net.Conn) { connFrom := conn.RemoteAddr().String() println("Connection from: ", connFrom) sayHello(conn) for { // create an input buffer to store received data: var ibuf []byte = make([]byte, maxRead + 1) // store 25-bytes of received data in the buffer: length, err := conn.Read(ibuf[0:maxRead]) // set last byte to prevent aginst buffer overflow: ibuf[maxRead] = 0 // error handeling when reading data from client: switch err { case nil: handleMsg(length, err, ibuf) case os.EAGAIN: // try again continue case default: goto DISCONNECT } } DISCONNECT: err := conn.Close() println("Closed connection: ", connFrom) checkError(err, "Close: ") }
func doServerStuff(conn net.Conn) { // infinite for loop, read bytes from connection for { // buffer for temporary storage buf := make(byte[], 512) _, err := conn.Read(buf) if err != nil { fmt.Println("Error reading data", err.Error()) return // terminate program } fmt.Printf("Received data: %v", string(buf)) } }