Web 使用Golang构建服务器但客户端有问题
这是我的代码:Web 使用Golang构建服务器但客户端有问题,web,go,webserver,Web,Go,Webserver,这是我的代码: 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("/"
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()
}
我知道我可以使用以下代码来构建服务器,但我想做一些事情,比如打印一些数据
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
当代码运行时,服务器端可以成功打印“连接成功”
但是,它不会在客户端显示任何内容
换句话说,客户端浏览器显示任何内容并保持加载,但服务器端可以显示有关连接成功的消息
PS:代码引用了golang的源代码()您应该使用处理程序或accept,但是如果您使用,这将处理新的连接,您将忽略处理程序。您将进入http连接的较低抽象级别 这段代码按照您的预期工作:
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Test")
fmt.Println("Connect Successful")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
当你打电话给他时,他会做出正确的接受,这样你就不必担心了。我不确定你想达到什么目的,但这段代码存在一些主要问题
http.HandleFunc(“/”,handle)
是DefaultServeMux.HandleFunc(pattern,HandleFunc)
的别名,但由于您的代码无论如何都不涉及DefaultServeMux
,甚至不涉及http.Server
,因此它是无用的Accept()
中的错误,然后继续,但是Accept中的任何错误都是致命的,大多数情况下您无法从中恢复func ClientLogic(conn net.conn)
立即关闭连接,不做任何操作,您希望发生什么我不确定您试图实现什么,但该代码存在一些主要问题 1)
http.HandleFunc(“/”,handle)
是DefaultServeMux.HandleFunc(pattern,HandleFunc)
的别名,但由于您的代码在任何情况下都不涉及DefaultServeMux,甚至不涉及http.Server,因此它是无用的
3) func ClientLogic(conn net.conn)
立即关闭连接,不做任何操作,您希望发生什么
1) 在golang的包中,它说DefaultServeMux
是handler的默认值。所以我从来不在乎它
3) 中的代码可以显示如下数据:
接收 409 個 byte 資料: GET / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/9.10 (karmic) Firefox/3.6.17
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cache-Control: max-age=0
我想做的是,我的服务器可以显示这些信息
mcuadros:
您应该使用处理程序或accept,但是如果您使用accept,这将由新连接处理,您将忽略处理程序。您将进入http连接的较低抽象级别 当你打电话给ListendServe时,他会做出正确的回答,这样你就不用担心了 谢谢你的代码,它可以做我想要的
我想确定一下。我的代码的问题是我使用
listener.Accept()
建立新连接。因此,我的处理程序被忽略。由于新连接,服务器可能会显示一些内容。客户端无法显示任何内容,因为它来自旧的处理程序,不是吗?您的处理程序函数从未被调用,因为您不启动服务器,而是启动另一台服务器。
接收 409 個 byte 資料: GET / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/9.10 (karmic) Firefox/3.6.17
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cache-Control: max-age=0