如果网络连接被人为断开,程序应该如何处理这一事件.

allen_zhaozhencn 2005-06-05 11:02:00
如果网络连接被人为断开,程序应该如何处理这一事件,recv,send函数会返回什么值? 是否可以根据这些函数的返回值进行判断?
...全文
283 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
fisker0303 2005-06-06
  • 打赏
  • 举报
回复
用心跳包
oyljerry 2005-06-06
  • 打赏
  • 举报
回复
heartbeat心跳包探测
54783szg 2005-06-06
  • 打赏
  • 举报
回复
在程序中加上不定时的PING功能,如果TIMEOUT不就是断了吗!
luoxiangdong 2005-06-06
  • 打赏
  • 举报
回复
ActiveTest
魔芋 2005-06-06
  • 打赏
  • 举报
回复
设超时就完了
jerry 2005-06-06
  • 打赏
  • 举报
回复
心跳包. 每隔一定的时间就发送一次心跳包, 如果网络出了问题, 发送必定失败. 此时发送和接收都会返回错误
aiyue2010 2005-06-06
  • 打赏
  • 举报
回复
我试了一下,如果客户端把网线拨掉,在服务端的recv函数根本无法返回.挂死在那里了.
搂主描述的这种情况确实有可能出现,我也曾经遇到过这样的情况:连接建立后传输数据的过程中把网线拔掉,有一端马上就会返回错误,而另一端确没有返回。当时为了解决这个问题自己加了一个定时器,设置一个超时时间timeout,超过这个超时时间就认为连接端开了。
allen_zhaozhencn 2005-06-06
  • 打赏
  • 举报
回复
ACE_SOCK_Dgram m_SockDgram
allen_zhaozhencn 2005-06-06
  • 打赏
  • 举报
回复
我这样处理的: Client建立一个SOCKET, 并bind到指定端口A. Server也建立一个SOCKET,同样绑定到一个指定端口B. 在服务器启用一个线程,定时发送数据到Client A, 用UDP.

现在的问题是, 当Client网线被拨后,Server端的Send不会返回.我明明设置了超时...

发送代码如下:

iSendBytes = m_SockDgram.send(ptrBuf.get(), iSize, RemoteAddr, 0, &Time);
if (iSendBytes == ACE_ERROR_CODE && errno == ETIME) //发送"心跳包"超时
{
(*iter)->m_pOwner->EndThread( ); //通知线程结束
}
jetwins 2005-06-06
  • 打赏
  • 举报
回复
如果人为断开时对端程序closesocket了的话,你得到的消息将是FD_CLOSE,如果对端没有发closesocket就断开了,如程序崩溃之类的话,你WSAGETSELECTERROR(lParam)应该返回TRUE.
一上我指的是异步SOCKET情况。
如果是同步Socket的话,应该recv时会返回SOCKET_ERROR。
allen_zhaozhencn 2005-06-06
  • 打赏
  • 举报
回复
给我一份相关的代码参考一下: zhaozhencn@126.com
allen_zhaozhencn 2005-06-06
  • 打赏
  • 举报
回复
谢谢各位.有没有相关的代码阿...
kingzai 2005-06-05
  • 打赏
  • 举报
回复
How to Determine Loss of Client/Server Connection

Q140325

SUMMARY
This article explains how to use keepalive probes to determine loss of stream connection.



MORE INFORMATION
In the context of this article, the "client" is the peer computer requesting a connection, and the "server" is the peer computer accepting a connection. This notation does not reflect Client/Server relationships as an architectural principal.

Consider the following scenario (half-open connection):

The client has established stream connection with the server. If client crashes or a network error occurs (for example, intermediate router goes down), the server has no way of knowing this, unless it tries to use send(). In this case, send fails with the WSAECONNRESET error. If the server does not try to send data to a client, the connection is kept open on the server side for infinite amount of time.

Even if WSAAsyncSelect() with the FD_CLOSE parameter is used to determine loss of connection, event notification is not triggered. No events get posted because virtual circuit never go to a FIN WAIT or CLOSE WAIT state (crashed client never called shutdown or closesocket).

This situation can be avoided if keepalive probe packets are used. Note that keepalive probe packets are not a required part of the TCP specification. Keepalive packets are used to probe a connection that has been inactive for a long time. The peer initiates a disconnect when the probes do not get through. Keepalive packets for TCP connection can be turned on by using setsockopt() call with SO_KEEPALIVE option.

The following registry entries control keepalive probe packet parameters on computers running Windows NT or Windows 95 (note that not all of the entries can be found in the registry by default). Changing registry parameters affects all TCP/IP stream connections on the system.

KeepAliveInterval - This parameter determines the interval separating keep alive retransmissions until a response is received.

KeepAliveTime - This parameter controls how often TCP attempts to verify that an idle connection is still intact by sending a keep alive packet.

TcpMaxDataRetransmissions - This parameter controls the number of times TCP will retransmit an individual data segment (non-connect segment) before aborting the connection.

NOTE: The TcpMaxDataRetransmission parameter is NOT a valid setting under Windows 95. For additional information on valid TCP/IP registry parameters in Windows 95, please see the following article in the Microsoft Knowledge Base:

Q158474 Windows 95 TCP/IP Registry Entries
The above entries are found in following registry locations:
Windows 95

\HKEY_LOCAL_MACHINE
\System
\CurrentControlSet
\Services
\VxD
\MSTCP
NOTE: To make the appropriate changes to the value above in the registry under Windows 95, go to the above location, click the Edit menu, point to New, and click String Value.
Windows NT

\HKEY_LOCAL_MACHINE
\System
\CurrentControlSet
\Services
\TCPIP
\Parameters
NOTE: To make the appropriate changes to the values above in the registry under Windows NT, go to the above location, click Add Value on the Edit menu. The Data Type should be REG_DWORD.

For information on how to configure above entries, please see the following article in the Microsoft Knowledge Base:
Q120642 TCP/IP & NBT Configuration Parameters for Windows NT
IMPORTANT: After you alter the registry, you must restart your computer.


int l_addr=TRUE,l_len=sizeof(l_addr);
ret =setsockopt(m_hSocket,SOL_SOCKET,SO_KEEPALIVE,(char*)&l_addr,l_len);
allen_zhaozhencn 2005-06-05
  • 打赏
  • 举报
回复
我试了一下,如果客户端把网线拨掉,在服务端的recv函数根本无法返回.挂死在那里了.
newbiestar 2005-06-05
  • 打赏
  • 举报
回复
估计到时候得到的是WSAENETDOWN
Tranquillo 2005-06-05
  • 打赏
  • 举报
回复
没有这么精细的错误定义,因为底层也不知道是线路不可用了还是被人拔掉了还是网络接口设备坏了,你只能报个模糊的网络断开的错
allen_zhaozhencn 2005-06-05
  • 打赏
  • 举报
回复
谢谢楼上的回答,我所指的就是"网线被人为的拨掉".
newbiestar 2005-06-05
  • 打赏
  • 举报
回复
不知道你的网络连接被认为断开指的是什么?

如果是说closesocket调用的话,recv返回的是0,如果是网线被掐掉或者其他的什么错误的话,产生的返回值是SOCKET_ERROR,具体什么错误可以通过WSAGetLastError得到。

对于send的话,只要不是处在正常的状态,那么就会给出SOCKET_ERROR,然后用WSAGetLastError得到错误具体原因。

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧