How can I let a conn alive, I have this code, the TCP server work but  it 
does not keep alive


func main() {
    // Listen for incoming connections.
    l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
    if err != nil {
        fmt.Println("Error listening:", err.Error())
        os.Exit(1)
    }
    // Close the listener when the application closes.
    defer l.Close()
    fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
    for {
        // Listen for an incoming connection.
        
        conn, err := l.Accept()
        if err != nil {
            fmt.Println("Error accepting: ", err.Error())
            os.Exit(1)
        }

        // Handle connections in a new goroutine.
        go handleRequest(conn)
    }
}

// Handles incoming requests.
func handleRequest(conn net.Conn) {
  conn.(*net.TCPConn).SetKeepAlive(true)
  buf := make([]byte, 1024)

  // Read the incoming connection into the buffer.
  reqLen, err := conn.Read(buf)
  buf = buf[:reqLen]

  if err != nil {
    fmt.Println("Error reading:", err.Error())
  }

  var packageData string = ""
  for i := 0; i < len(buf)-1; i++ {
    packageData += fmt.Sprintf("%02x", buf[i])
  }
  //...
  // Close the connection when you're done with it.
  conn.Close()
    
}







On Tuesday, July 26, 2016 at 12:21:51 PM UTC-5, Matt Harden wrote:
>
> This is because your conn variable is probably of the type net.Conn (an 
> interface) rather than *net.TCPConn, so the compiler doesn't know that the 
> SetKeepAlive method is available. If you use a type assertion as djadala 
> suggested: conn.(*net.TCPConn).SetKeepAlive then you are telling the 
> compiler that the value in the conn variable is actually a TCP connection 
> and so the SetKeepAlive method becomes available. This will panic if the 
> conn variable does not actually contain a *net.TCPConn.
>
> On Tue, Jul 26, 2016 at 12:09 AM <dja...@gmail.com <javascript:>> wrote:
>
>> try    conn.(*TCPConn <https://golang.org/pkg/net/#TCPConn>).SetKeepAlive
>>
>> On Tuesday, July 26, 2016 at 10:04:41 AM UTC+3, EdgarAlejandro Vintimilla 
>> wrote:
>>>
>>>
>>>
>>>
>>> when I use the conn.SetKeepAlive i get this error: reference to 
>>> undefined field or method ‘SetKeepAlive’
>>>
>>> https://golang.org/pkg/net/#TCPConn.SetKeepAlive
>>>
>>> why? 
>>>
>>> but If I use conn.SetReadDeadline it works ok
>>>
>>> https://golang.org/pkg/net/#IPConn.SetReadDeadline
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to