日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

服務(wù)器如何在SOCKET通訊中知道客戶端斷開連接或掉線了???[

 haodafeng_org 2011-03-24
發(fā)表于:2002-06-14 09:52:34
我用Socket做了一個服務(wù)器和客戶端,服務(wù)器在客戶端連接時能知道,但客戶端斷開或掉線服務(wù)器就不知道了,請問大蝦們有好辦法嗎?
  • pfans用戶頭像
  • pfans
  • (pfans)
  • 等 級:
#1樓 得分:0回復(fù)于:2002-06-14 13:30:11
有人知道嗎?
#2樓 得分:0回復(fù)于:2002-06-14 13:59:37
幫你抬
#3樓 得分:0回復(fù)于:2002-06-14 14:07:05
如果Client端斷開連接,在Server端Recv時會返回0
但如果Client端掉線就不行了,你可以通過定時檢查某個連接是否有數(shù)據(jù)發(fā)送
來確定,如果長時間無數(shù)據(jù)發(fā)送就認為已掉線
#4樓 得分:0回復(fù)于:2002-06-14 14:11:16
判斷OnClose函數(shù),定時ping一次都可以
http://www.csdn.net/expert/topic/563/563231.xml?temp=.297085
#5樓 得分:0回復(fù)于:2002-06-14 14:26:25
用select啊。
  • pfans用戶頭像
  • pfans
  • (pfans)
  • 等 級:
#6樓 得分:0回復(fù)于:2002-06-22 08:59:45
我沒有用CSocket類,用的是SDK.
#7樓 得分:0回復(fù)于:2002-06-22 21:02:45
How   do   I   detect   when   my   TCP   connection   is   closed?
All   of   the   I/O   strategies   discussed   in   the   I/O   strategies   article   have   some   way   of   indicating   that   the   connection   is   closed.

First,   keep   in   mind   that   TCP   is   a   full-duplex   network   protocol.   That   means   that   you   can   close   the   connection   half-way   and   still   send   data   on   the   other   half.   An   example   is   a   web   browser:   it   sends   a   short   request   to   the   web   server,   then   closes   its   half   of   the   connection.   The   web   server   then   sends   back   the   requested   data   on   the   other   half   of   the   connection,   and   closes   its   sending   side,   which   terminates   the   TCP   session.

Normal   TCP   programs   only   close   the   sending   half,   which   the   remote   peer   perceives   as   the   receiving   half.   So,   what   you   normally   want   to   detect   is   whether   the   remote   peer   closed   its   sending   half,   meaning   you   won 't   be   receiving   data   from   them   any   more.

With   asynchronous   sockets,   Winsock   sends   you   an   FD_CLOSE   message   when   the   connection   drops.   Event   objects   are   similar:   the   system   signals   the   event   object   with   an   FD_CLOSE   notification.

With   blocking   and   non-blocking   sockets,   you   probably   have   a   loop   that   calls   recv()   on   that   socket.   recv()   returns   0   when   the   remote   peer   closes   the   connection.   As   you   would   expect,   if   you   are   using   select(),   the   SOCKET   descriptor   in   the   read_fds   parameter   gets   set   when   the   connection   drops.   As   normal,   you 'll   call   recv()   and   see   the   0   return   value.

As   you   might   have   guessed   from   the   discussion   above,   it   is   also   possible   to   close   the   receiving   half   of   the   connection.   If   the   remote   peer   then   tries   to   send   you   data,   the   stack   will   drop   that   data   on   the   floor   and   send   a   TCP   RST   to   the   remote   peer.

#8樓 得分:0回復(fù)于:2002-06-22 21:03:12
How   do   I   detect   an   abnormal   network   disconnect?
The   previous   question   deals   with   detecting   when   a   protocol   connection   is   dropped   normally,   but   what   if   you   want   to   detect   other   problems,   like   unplugged   network   cables   or   crashed   workstations?   In   these   cases,   the   failure   prevents   notifying   the   remote   peer   that   something   is   wrong.   My   feeling   is   that   this   is   usually   a   feature,   because   the   broken   component   might   get   fixed   before   anyone   notices,   so   why   force   everyone   to   restart?

If   you   have   a   situation   where   you   must   be   able   to   detect   all   network   failures,   you   have   two   options:

The   first   option   is   to   give   the   protocol   a   command/response   structure:   one   host   sends   a   command   and   expects   a   prompt   response   from   the   other   host   when   the   command   is   received   or   acted   upon.   If   the   response   does   not   arrive,   the   connection   is   assumed   to   be   dead,   or   at   least   faulty.

The   second   option   is   to   add   an   "echo "   function   to   your   protocol,   where   one   host   (usually   the   client)   is   expected   to   periodically   send   out   an   "are   you   still   there? "   packet   to   the   other   host,   which   it   must   promptly   acknowledge.   If   the   echo-sending   host   doesn 't   receive   its   response   or   the   receiving   host   fails   to   see   an   echo   request   for   a   certain   period   of   time,   the   program   can   assume   that   the   connection   is   bad   or   the   remote   host   has   gone   down.

If   you   choose   the   "echo "   alternative,   avoid   the   temptation   to   use   the   ICMP   "ping "   facility   for   this.   If   you   did   it   this   way,   you   would   have   to   send   pings   from   both   sides,   because   Microsoft   stacks   won 't   let   you   see   the   other   side 's   echo   requests,   only   responses   to   your   own   echo   requests.   Another   problem   with   ping   is   that   it 's   outside   your   protocol,   so   it   won 't   detect   a   failed   TCP   connection   if   the   hardware   connection   remains   viable.   A   final   problem   with   the   ping   technique   is   that   ICMP   is   an   unreliable   protocol:   does   it   make   a   whole   lot   of   sense   to   use   an   unreliable   protocol   to   add   an   assurance   of   reliability   to   another   protocol?

Another   option   you   should   not   bother   with   is   the   TCP   keepalive   mechanism.   This   is   a   way   to   tell   the   stack   to   send   a   packet   out   over   the   connection   at   specific   intervals   whether   there 's   real   data   to   send   or   not.   If   the   remote   host   is   up,   it   will   send   back   a   similar   reply   packet.   If   the   TCP   connection   is   no   longer   valid   (e.g.   the   remote   host   has   rebooted   since   the   last   keepalive),   the   remote   host   will   send   back   a   reset   packet,   killing   the   local   host 's   connection.   If   the   remote   host   is   down,   the   local   host 's   TCP   stack   will   time   out   waiting   for   the   reply   and   kill   the   connection.

There   are   two   problems   with   keepalives:

Only   Windows   2000   allows   you   to   change   the   keepalive   time   on   a   per-process   basis.   On   older   versions   of   Windows,   changing   the   keepalive   time   changes   it   for   all   applications   on   the   machine   that   use   keepalives.   (Changing   the   keepalive   time   is   almost   a   necessity   since   the   default   is   2   hours.)  

Each   keepalive   packet   is   40   bytes   of   more-or-less   useless   data,   and   there 's   one   sent   each   direction   as   long   as   the   connection   remains   valid.   Contrast   this   with   a   command/response   type   of   protocol,   where   there   is   effectively   no   useless   data:   all   packets   are   meaningful.   In   fairness,   however,   TCP   keepalives   are   less   wasteful   on   Windows   2000   than   the   "are   you   still   there "   strategy   above.  

Note   that   different   types   of   networks   handle   physical   disconnection   differently.   Ethernet,   for   example,   establishes   no   link-level   connection,   so   if   you   unplug   the   network   cable,   a   remote   host   can 't   tell   that   its   peer   is   physically   unable   to   communicate.   By   contrast,   a   dropped   PPP   link   causes   a   detectable   failure   at   the   link   layer,   which   propagates   up   to   the   Winsock   layer   for   your   program   to   detect.

#9樓 得分:0回復(fù)于:2002-06-22 21:04:33
select   FD_CLOSE|FD_READ|...
  • anjy用戶頭像
  • anjy
  • (泡泡oοО○)
  • 等 級:
#10樓 得分:0回復(fù)于:2002-06-22 21:05:48
Onclose()
#11樓 得分:0回復(fù)于:2002-06-22 21:24:58
設(shè)置超時設(shè)置或者單獨開1個線程檢查各個線程是否ACTIVE,是否超時,
如果超時的話就根據(jù)你的系統(tǒng)KILL掉或者掛起。
這里應(yīng)該設(shè)置有同步變量來控制。
#12樓 得分:0回復(fù)于:2002-06-22 21:52:36
你是用的sdk,

你使用WSAAsyncSelect()函數(shù),自定義消息WM_SOCKET,然后鋪捉FD_ACCEPT等等,在處理消息的開始的時候先判斷是否出現(xiàn)網(wǎng)絡(luò)錯誤
如下:

case     WM_SOCKET:
if(WSAGETSELECTERROR(lParam))
{
        if(WSAGETSELECTERROR(lParam)   ==   WSAECONNABORTED)
      {
          //   如果客戶端關(guān)閉socket,就產(chǎn)生這個錯誤
        }
        else
        {
          //   其他socket錯誤
        }
}

OK?
  • limin用戶頭像
  • limin
  • (不留名)
  • 等 級:
#13樓 得分:0回復(fù)于:2002-06-22 22:17:29
我認為應(yīng)該設(shè)置單獨的檢查在線機制,關(guān)于   FD_CLOSE   或者   OnClose   僅僅只能判斷socket的正常的close   ,對于非正常的吊線是無法檢測的。
#14樓 得分:0回復(fù)于:2002-07-08 15:43:29
select   FD_CLOSE|FD_READ|FD_CLOSE
  • mfkzj用戶頭像
  • mfkzj
  • (鷹翔)
  • 等 級:
#15樓 得分:0回復(fù)于:2002-07-08 16:44:49
你使用WSAAsyncSelect()函數(shù),自定義消息WM_SOCKET,然后鋪捉FD_ACCEPT等等,在處理消息的開始的時候先判斷是否出現(xiàn)網(wǎng)絡(luò)
可以設(shè)置中斷時間   如果在相隔時間內(nèi)客戶端沒有進行任何操作   就可以設(shè)定對方超時   中斷連接
#16樓 得分:0回復(fù)于:2002-07-08 17:03:20
TCP/IP在設(shè)計時,并沒有考慮到偵測網(wǎng)絡(luò)連線斷開這種情形,因為設(shè)計目標是適應(yīng)惡劣的環(huán)境的,應(yīng)用程序必須自己檢測這種錯誤情況。一般是在recv函數(shù)檢查有沒有錯誤,具體情況可參照一些書籍的詳細介紹。
      推薦的方法就是如前面所說使用在線檢查機制,定時傳送數(shù)據(jù)報,確認對方是否還在線。路由器和路由器之間的檢測就是這種形式
  • hzyem用戶頭像
  • hzyem
  • (大峽)
  • 等 級:
#17樓 得分:0回復(fù)于:2002-07-08 17:21:59
使用定時器定時向客戶端發(fā)送數(shù)據(jù)包,接收方收到后返回應(yīng)答數(shù)據(jù)包,如果超過一定時間沒有應(yīng)答就可以認為連接已斷開。
#18樓 得分:0回復(fù)于:2002-07-08 17:27:35
不知道用GetlastEorror()可不可以?反正我每交斷了GetlastEorror就為10054
#19樓 得分:0回復(fù)于:2002-07-08 17:34:34
gz
#20樓 得分:0回復(fù)于:2002-07-08 19:31:01
幫你UP!
#21樓 得分:0回復(fù)于:2002-07-10 01:58:48
自己實現(xiàn)檢測機制。tcp/ip不提供這種功能
#22樓 得分:0回復(fù)于:2002-07-10 01:59:09
winsock也沒有
  • jfzsl用戶頭像
  • jfzsl
  • (剿匪總司令)
  • 等 級:
#23樓 得分:0回復(fù)于:2002-07-12 12:56:12
up
  • sans用戶頭像
  • sans
  • (長風(fēng)半日)
  • 等 級:
  • 2

#24樓 得分:0回復(fù)于:2002-07-12 13:22:59
我原來一直也在為這個問題煩惱,最近解決了,方法如下:
在Server端使用Select進行檢查,同時設(shè)定TimeOut,如10分鐘;
在Client端開一個線程,定一個時間,如1分鐘就向Server發(fā)送一個信號,當Server端如果10分鐘還收不到Client端發(fā)來的信號,就關(guān)閉該Client的服務(wù)。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多