dpbf62565 2015-08-26 23:02
浏览 242

检测TCP断开连接的正确方法? 手动保持活动空闲,并且不起作用SetWriteDeadLine

I have some problem with a TCP connection.

I have a server written in go which listen tcp connection on some port. And I have client written in Java for Android. Client establish connection with server, and then exchange some data both without any laws.

When Android changes the network type, for example from mobile network to wifi. Connection is broken, but server don't know about this problem, and think that client all ready to take a some data.

I need to detect this problem on my server so quick as possible. Ideally immediately

First I used in go SetKeepAlive(true) and SetKeepAlivePeriod(10 * time.Second). It's worked, but not quickly as I need. Because go SetKeepAlivePeriod, set tcp keepalive time(idle) and tcp keepalive interval to same. For example in linux tcp keepalive retry=8, then problem with connection detected after (10 + 8*10) = 90sec = 1 min 30sec. It is very very slow :(. How to set idle and interval manual ? I watching this lib https://github.com/felixge/tcpkeepalive, but I don't trust this lib, because, in this lib socket fd copying without clear.

I can set SetKeepAlivePeriod(1 * time.Second), but whats about performance ? In my case, connection may be a heavily loaded or idle.

Then, i think about SetWriteDeadline. It's very useful for me, because i don't know when i receive message from client, but i know when i send message to client. But when i used SetWriteDeadline, is not working. For example

var request *http.Request

conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
err := request.Write(conn)
if err != nil {
    if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
        log.Println("Connection WriteDeadline timeout")
    }
    conn.Close();
} else {
    conn.SetWriteDeadline(time.Time{})
}

When after connection broken on client, I Write some data into connection, method Write return nil without error. As though data already send and server success accepted ACK from client, but how ?

Update 28.10.2015 22:35

I decided to try this https://github.com/felixge/tcpkeepalive library. And yet it works perfectly for my problem :).

ka, _ := tcpkeepalive.EnableKeepAlive(conn)
ka.SetKeepAliveCount(2)
ka.SetKeepAliveInterval(3 * time.Second)
ka.SetKeepAliveIdle(10 * time.Second)
  • 写回答

1条回答 默认 最新

  • doufen1890 2015-08-27 01:49
    关注

    to make thing simple, please do not use TCP level things to make sure your business session things. what you need in real is detecting broken session, so keep sending ping-pong in business level. you can calculate the load simply, like a "ping" string with tcp header, should be in 30bytes.

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了