如何保持socket 通讯的长连接

解决方案 »

  1.   

    The keepalive related parameters for TCP socket connection are optional parameters for Windows 2000, so these parameters do not exist in the registry. They may be created to modify the default behavior of the TCP/IP protocol driver.
    Below is the steps for configuring this parameter:
    1.Start Registry Editor (Regedt32.exe).
    2.From the HKEY_LOCAL_MACHINE subtree, go to the following key: 
    \SYSTEM\CurrentControlSet\Services
    3.Add a value to the key Tcpip\Parameters for parameter KeepAliveTime.
    4.Click OK.
    5.Quit Registry Editor.
    6.Reboot the computer to make the change take effect. Below is the explanation for this parameter:
    KeepAliveTime 
    Key: Tcpip\Parameters
    Value Type: REG_DWORD - Time in milliseconds
    Valid Range: 1 - 0xFFFFFFFF
    Default: 7,200,000 (two hours)
    Description: The parameter controls how often TCP attempts to verify that an idle connection is still intact by sending a keep alive packet. If the remote system is still reachable and functioning, it will acknowledge the keep alive transmission. Keep alive packets are not sent by default. This feature may be enabled on a connection by an application据我了解,如果想在java的Socket中实现,就得自己扩展SocketImplFactoryle ,jdk自身的缺省的SocksSocketImplFactory不支持Tcp的这个选项,你可以看一下java.net...关于Socket的source.以上仅供参考
      

  2.   

    Javafan0(狂刀) ( ) 信誉:88  2006-08-07 17:17:00  得分: 0  
     
     
       在超时时间之内发送激活包(有人称心跳包)具体代码该怎么写?是否有演示程序?
      
     
    ------------------------------给你的连接定义一个无意义的包,哪怕只有一个head都可以.对方收到后反馈一个类似的res.定义一个任务隔一段时间发一次.
      

  3.   

    一般来说,只要你不去主动断掉,Socket会一直连接下去的,不用去刻意地发数据包