在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、服务器如何知道用户需要什么资源 示例: package main import ( "fmt" "net" ) func main() { //监听 listener, err := net.Listen("tcp", ":8000") if err != nil { fmt.Println("Listen err = ", err) return } defer listener.Close() //阻塞等待用户的连接 conn, err1 := listener.Accept() if err1 != nil { fmt.Println("Accept err1 = ", err1) return } defer conn.Close() //接收客户端的数据 buf := make([]byte, 1024*4) n, err2 := conn.Read(buf) if n == 0 { fmt.Println("Read err = ", err2) return } fmt.Printf("#%v#", string(buf[:n])) } 执行结果: #GET /mike.html HTTP/1.1 #接文件名称 Host: 127.0.0.1:8000 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9
|
请发表评论