解释服务器的几个概念:
1. Request 用户请求的信息
2. Response 服务器返回的信息
3. Conn 用户的每次请求链接
4. Handler 处理请求和生成返回信息的处理逻辑
先写个小程序看看
func MyConn() {
// 监听9090端口
listener, _ := net.Listen("tcp", "localhost:9090")
defer listener.Close()
// 建立用户请求的连接
conn, _ := listener.Accept()
defer conn.Close()
// 读取Request的内容
buff := make([]byte, 10000)
len, _ := conn.Read(buff)
fmt.Fprintln(os.Stdout, string(buff[:len]))
}
对于代码,要解释的是读取conn的内容,你可以把conn看做是数据流,先看下Golang中的源码,先不去掉注释,Golang的注释真的是简单好懂,看完了注释,你就应该能理解Conn是什么了。
// Multiple goroutines may invoke methods on a Conn simultaneously.
type Conn interface {
// Read reads data from the connection.
// Read can be made to time out and return an Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetReadDeadline.
Read(b []byte) (n int, err error)
// Write writes data to the connection.
// Write can be made to time out and return an Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetWriteDeadline.
Write(b []byte) (n int, err error)
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
Close() error
// LocalAddr returns the local network address.
LocalAddr() Addr
// RemoteAddr returns the remote network address.
RemoteAddr() Addr
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future and pending
// I/O, not just the immediately following call to Read or
// Write. After a deadline has been exceeded, the connection
// can be refreshed by setting a deadline in the future.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
SetDeadline(t time.Time) error
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
SetWriteDeadline(t time.Time) error
}
在浏览器上输入 localhost:9090 ,按下回车键,你的命令行会有一下的内容
GET / HTTP/1.1 //请求行:请求方法(GET), 请求的URL(/),HTTP协议(HTTP/1.1)
Host: localhost:9090 //服务器主机名
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0 //浏览器信息
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 //客户端能接受的mine
Accept-Language: zh-CN,en-US;q=0.7,en;q=0.3 //请求头允许客户端声明它可以理解的自然语言,以及优先选择的区域方言
Accept-Encoding: gzip, deflate //是否支持流压缩
Connection: keep-alive //控制当前传输结束后是否保持网络连接
Upgrade-Insecure-Requests: 1 //Sends a signal to the server expressing the client’s preference for an encrypted and authenticated response, and that it can successfully handle the upgrade-insecure-requests directive.
//空行,分割请求头和消息体
//消息体,POST传递
关于请求头数据的更详细的资料可以看https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
目前比较重要的是这个数据的第一行
GET / HTTP/1.1
如果你在地址栏输入的网址是 localhost:9090/hello ,第一行会是这样
GET /hello HTTP/1.1
对于服务器,首先需要知道协议(是GET还是POST),然后需要知道你想要访问的地址。对于了解MVC的同学肯定了解Controller,在Golang中,也存在类似的路由机制,你可以将访问的URL跟Handler一一对应(感觉是不是有点像键值对),根据请求的URL就可以对请求内容进行操作并返回了,先写到这里,下一节再想想怎么写Handler.