This is my code:
package main
import (
"fmt"
"net"
"net/http"
"os"
)
const RECV_BUF_LEN = 1024
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Test")
}
func main() {
http.HandleFunc("/", handler)
s := &http.Server{Addr: ":8080", Handler: nil}
listener, err := net.Listen("tcp", s.Addr)
if err != nil {
fmt.Println("Error: ", err.Error())
fmt.Println("Close Server")
os.Exit(1)
}
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error: ", err.Error())
continue
}
go ClientLogic(conn)
}
}
func ClientLogic(conn net.Conn) {
fmt.Println("Connect Successful")
conn.Close()
}
I know I can use following code to build a server but I want do some stuffs, such as printing some datas.
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
When the code run, the server-side can print "Connect Successful" successfully.
However, the it doesn't display anything in client-side.
In other words, the client's browser display anything and keep loading but the server-side can display the message about connecting successfully.
PS: The code refer to http://lolikitty.pixnet.net/blog/post/148420590 and the golang's source code ( http://golang.org/src/pkg/net/http/server.go?s=50405:50451#L1684 )