如何处理服务器或客户端socket断开.doc_第1页
如何处理服务器或客户端socket断开.doc_第2页
如何处理服务器或客户端socket断开.doc_第3页
如何处理服务器或客户端socket断开.doc_第4页
如何处理服务器或客户端socket断开.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

编程经验谈 如何处理socket连接后服务器端或socket编程经验谈-如何处理socket连接后服务器端或客户端的断开(转)2010年12月29日星期三14:50现象:服务器端等待客户断连接,当socket连接建立后,如果客户端异常断开,服务器会抛出异常,从而导致程序运行中断目标:希望服务器一直等待连接,客户端中断后程序不退出,而客户端重新恢复后可以继续保持连接代码:public class receivepublic static byte buffer=new byte1024;public static manualresetevent socketevent=new manualresetevent(false);public static socket slistener=new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);public static socket handler=null;public static string clientbroken=an connection was forcibly closed by the remote host;public static void receive()tryconsole.writeline(main threadid:+appdomain.getcurrentthreadid();byte bytes=new byte1024;ipaddress ipaddr=ipaddress.parse();int port=10001;ipendpoint epserver=new ipendpoint(ipaddr,port);/binding asocket slistener.bind(epserver);/start listening slistener.listen(10);while(true)if(handler=null)/first must make aconnect console.writeline(waiting for aconnection.);/asychronous function for accepting connections slistener.beginaccept(new asynccallback(acceptcallback),slistener);socketevent.waitone();handler.beginreceive(buffer,0,buffer.length,0,new asynccallback(receivecallback),handler);socketevent.waitone();elseconsole.writeline(waiting next message.);socketevent.reset();handler.beginreceive(buffer,0,buffer.length,0,new asynccallback(receivecallback),handler);socketevent.waitone();console.readline();catch(exception e)console.writeline(e.tostring();console.readline();public static void acceptcallback(iasyncresult ar)tryconsole.writeline(acceptcallback thread id:+appdomain.getcurrentthreadid();socket listener=(socket)ar.asyncstate;/new socket handler=listener.endaccept(ar);handler.beginreceive(buffer,0,buffer.length,0,new asynccallback(receivecallback),handler);catch(exception e)console.writeline(e.tostring();public static void receivecallback(iasyncresult ar)string err_message=null;tryconsole.writeline(receivecallback thread id:+appdomain.getcurrentthreadid();string content=string.empty;handler=(socket)ar.asyncstate;int bytesread=handler.endreceive(ar);/if there is some data.if(bytesread 0)/append it to the main string content+=encoding.ascii.getstring(buffer,0,bytesread);/if we encounter the end of message character if(content.indexof(char)3)-1|content.indexof(char)16)-1)console.writeline(read+content.length+bytes from socket.n data:+content);socketevent.set();else/otherwise receive the remaining data handler.beginreceive(buffer,0,buffer.length,0,new asynccallback(receivecallback),handler);catch(exception e)err_message=e.message;if(err_message.indexof(an existing connection was forcibly closed by the remote host)-1)console.writeline(an existing connection was forcibly closed by the remote host);/handler.shutdown(socketshutdown.both);/handler.close();console.writeline(waiting for aconnection.);/asychronous function for accepting connections slistener.beginaccept(new asynccallback(acceptcallback),slistener);elseconsole.writeline(e.tostring();说明:关键在于最后这段的异常处理,接收中断后,服务器端重新等待接收。现象:客户端与服务器连接,当socket连接建立后,如果服务器端异常断开,客户端会抛出异常,从而导致程序运行中断目标:希望客户端出现提示,服务器端中断后程序不退出,而服务器端重新恢复后可以继续保持连接代码:public class asynccommpublic static string theresponse=;public static byte buffer=new byte1024;public static manualresetevent socketevent=new manualresetevent(false);public static socket sclient=new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);public static ipendpoint epserver=new ipendpoint(ipaddress.parse(),10001);public static void send(string data)byte bytedata=null;bytedata=encoding.ascii.getbytes(data);tryif(!sclient.connected)console.writeline(system.datetime.now.tostring(yyyy-mm-dd hh:mm:ss:ffff)+connect begining.);sclient.beginconnect(epserver,new asynccallback(connectcallback),sclient);socketevent.waitone();sclient.beginsend(bytedata,0,bytedata.length,0,new asynccallback(sendcallback),sclient);socketevent.waitone();catch(exception e)console.writeline(server side is broken.);socketevent.reset();return;public static void connectcallback(iasyncresult ar)trythread thr=thread.currentthread;console.writeline(connectcallback thread state:+appdomain.getcurrentthreadid();socket sclient=(socket)ar.asyncstate;sclient.endconnect(ar);console.writeline(socket connected to+sclient.remoteendpoint.tostring();socketevent.set();catch(exception ex)console.writeline(system.datetime.now.tostring(yyyy-mm-dd hh:mm:ss:ffff)+|+appdomain.getcurrentthreadid()+|3-level 3server connection

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论