解释服务器的几个概念:html
先写个小程序看看express
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 ,第一行会是这样app
GET /hello HTTP/1.1
对于服务器,首先须要知道协议(是GET仍是POST),而后须要知道你想要访问的地址。对于了解MVC的同窗确定了解Controller,在Golang中,也存在相似的路由机制,你能够将访问的URL跟Handler一一对应(感受是否是有点像键值对),根据请求的URL就能够对请求内容进行操做并返回了,先写到这里,下一节再想一想怎么写Handler.tcp