Golang1.7 Http和Tcp使用同一个端口做服务
来源:互联网 发布:柳州市一职校淘宝地址 编辑:程序博客网 时间:2024/05/16 19:19
先看一下标准库中http server的实现type tcpKeepAliveListener struct { *net.TCPListener}func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) { tc, err := ln.AcceptTCP() if err != nil { return } tc.SetKeepAlive(true) tc.SetKeepAlivePeriod(3 * time.Minute) return tc, nil}这里可以看出:tc.SetKeepAlivePeriod(3 * time.Minute) 设置了超时时间为3分钟,这个先到这里下面会用到
func main() { http.HandleFunc("/", route) http.ListenAndServe(":1789", nil)}func route(w http.ResponseWriter, r *http.Request) { log.Printf("Addr->%s\tURI->%s\n", r.RemoteAddr, r.URL.Path) defer r.Body.Close() switch r.URL.Path { case "/": w.Write([]byte("welcome to work-stacks")) case "/gettcp": gettcp(w, r) default: http.NotFound(w, r) }}func gettcp(w http.ResponseWriter, r *http.Request) { //这里返回的*bufio.ReadWriter 没有处理, 生产环境注意要情况bufferd conn, _, err := w.(http.Hijacker).Hijack() if err != nil { log.Printf("获取Hijacks失败:%s\n", err.Error()) return } if tcp,ok := conn.(*net.TCPConn);ok { tcp.SetKeepAlivePeriod(60 * time.Minute) } //然后就可以做自己要做的操作了. conn.Close()}
func main() { req, err := http.NewRequest("GET", "http://127.0.0.1:1789/gettcp", nil) if err != nil { log.Println("创建请求失败:%s\n", err.Error()) return } conn, _ := net.Dial("tcp", "127.0.0.1:1789") defer conn.Close() hclient := httputil.NewClientConn(conn, nil) err = hclient.Write(req) if err != nil { log.Printf("发送请求失败:%s\n", err) return }}
1 0
- Golang1.7 Http和Tcp使用同一个端口做服务
- Golang1.7中的TCP交互封装.
- apache和tomcat同时使用同一个端口
- Http服务器--使用Http服务做转发
- Golang1.7动态库的使用,C和Golang的动态库以及使用
- Golang1.7使用ICMP协议实现ping功能,带TIME和TTL
- 使用node搭建UDP,TCP,HTTP服务
- TCP和UDP端口号使用
- Samba 服务使用的端口和协议
- Samba 服务使用的端口和协议
- tcpmux TCP 端口服务多路复用
- http https 和一些常用的服务请求默认端口
- TCP/IP Server同一个端口 怎么区分不同socket
- 自己的服务器做网站服务,解决http被屏蔽、80端口穿透实现
- Golang1.7闲来无事写了一个基于Gob的tcp通讯用的包
- 使用Nginx为HTTP服务做负载均衡
- 在同一个tomcat中,配置两个端口进行提供服务
- 在同一个tomcat中,配置两个端口进行提供服务
- MY FIRST CSDN DAY
- 第七周项目1-建立顺序环形队列算法库
- 虚拟机怎么设置u盘启动
- thinkphp连接多数据库 数据库切换
- iOS NSEnumerator的用法
- Golang1.7 Http和Tcp使用同一个端口做服务
- 伟大的程序猿的10个特征
- 从零开始编写JavaScript框架
- 软件需求分析模板
- Java获取用户的真实iP
- Idea Project Structure配置
- 【codeforces】704D. Captain America 【上下界最小流】
- ios笔记layoutSubviews 什么时候调用
- windows 10 应用商店自带qq 消息推送打开方法