Accept a persistent tcp connection in Golang Server

后端 未结 2 488
后悔当初
后悔当初 2021-02-06 15:59

I am experimenting with Go - and would like to create a TCP server which I can telnet to, send commands and receive responses.

const (
    CONN_HOST = \"localhos         


        
2条回答
  •  有刺的猬
    2021-02-06 16:40

    Not sure if this is what you're looking for. Taken from net/http implementation, wrapping your net.TCPListener's Accept method.

    tcpKeepAliveListener{listener.(*net.TCPListener)}

    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
    }
    

    Refer : Link 1 & Link 2

提交回复
热议问题