c# - how to get connectionaborted immediately in Asynchronous Programming? -
i doing asynchronous programming in c# when came across question,when network aborted.
my program can exception of connectionaborted 15 seconds after send invaild message client server.
my question if want exception after network doesn't work,what need do.
namespace _10_tcp模块化编程 { class objectstate { public socket client; public mytcp obj; public const int buffersize = 256; // receive buffer. public byte[] buffer = new byte[buffersize]; // received data string. public stringbuilder sb = new stringbuilder(); } class mytcp { public delegate void dreceiver(object sender, string b); public event dreceiver receive; public socket webhabor; //private bool connected; public mytcp(ipendpoint iep, dreceiver deventcall) { //接收消息的委托; receive += deventcall; //创建socket连接; webhabor = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); objectstate obs = new objectstate(); obs.obj = this; obs.client = webhabor; webhabor.beginconnect(iep, new asynccallback(connectcallback), obs); //connected = webhabor.connected ? true : false; thread.sleep(10); if (webhabor.connected == true) { receive(); } else { program.postlog("连接失败,请检查ip和port"); } } public static void exceptionsolver(socketexception sep) { switch (sep.socketerrorcode) { case socketerror.notconnected: //捕获ip地址输入错误的情况; program.postlog("不存在网络连接"); break; case socketerror.connectionaborted: //在这里处理频繁出现的错误, //比如ip不对,网线没插 program.postlog("连接中止"); break; case socketerror.connectionrefused: //远程主机正在主动拒绝连接;可能是连接的时候ip或port写错了; program.postlog("对方不接受连接,更可能是port的原因"); break; case socketerror.hostunreachable: program.postlog("连接目标不可达"); break; case socketerror.timedout: //尝试连接ip超时; program.postlog("尝试连接ip超时,更可能是ip的原因"); break; default: program.postlog("捕获到" + sep.socketerrorcode); //这里直接报错,如果调试的时候出现这里的错误比较多,就移到上面解决,一般问题都是从来不出的 break; } } public void send(byte[] datatosend,int bytecount) { try { webhabor.beginsend(datatosend, 0, bytecount, 0, new asynccallback(sendcallback), webhabor); //system.threading.thread.sleep(10);//为了让其它线程跑起来; } catch (socketexception sep) { program.postlog("在send这里"); exceptionsolver(sep); } } public void receive() { byte[] buffer = new byte[256]; objectstate obs = new objectstate(); obs.obj = this;//这个传的是mytcp; obs.client = webhabor; try { webhabor.beginreceive(obs.buffer, 0, objectstate.buffersize, 0, new asynccallback(receivecallback), obs); } catch (socketexception sep) { program.postlog("在reveive这里"); exceptionsolver(sep); } //receive.invoke(this, buffer); } //beginconnect的回调函数; private static void connectcallback(iasyncresult ar) { objectstate obs = (objectstate)ar.asyncstate; try { // retrieve socket state object. socket client = obs.client; // complete connection. client.endconnect(ar); //console.writeline("socket connected {0}", client.remoteendpoint.tostring()); //连上就连上吧就不发数据了 //messagebox.show("socket connected " + client.remoteendpoint.tostring()); // signal connection has been made. //connectdone.set(); } catch (socketexception sep) { //console.writeline(e.tostring()); //messagebox.show("connect回调函数出错了" + e.tostring()); /********************************************************************* * * 此处需要考察一下连接失败的异常情况。 * *********************************************************************/ //obs.obj.connected = false; program.postlog("在connectcallback这里"); exceptionsolver(sep); } } private static void sendcallback(iasyncresult ar) { try { // retrieve socket state object. socket client = (socket)ar.asyncstate; //system.threading.thread.sleep(10);//为了让其它线程跑起来; // complete sending data remote device. int bytessent = client.endsend(ar); program.postlog("本次发送" + bytessent + "个字节"); //console.writeline("sent {0} bytes server.", bytessent); //messagebox.show("sent " + bytessent + " bytes server."); // signal bytes have been sent. //senddone.set(); } catch (socketexception sep) { //messagebox.show("send回调函数出错了" + e.tostring()); /********************************************************************* * * 此处需要考察一下发送失败的异常情况。 * *********************************************************************/ program.postlog("在sendcallback这里"); exceptionsolver(sep); } } private static void receivecallback(iasyncresult ar) { //byte[] buffer = new byte[256]; try { // retrieve state object , client socket // asynchronous state object. //stateobject state = (stateobject)ar.asyncstate; //socket client = state.worksocket; // read data remote device. objectstate objs = (objectstate)ar.asyncstate; socket client = objs.client; int bytesread = client.endreceive(ar); if (bytesread > 0) { // there might more data, store data received far. string getmsg = encoding.ascii.getstring(objs.buffer, 0, bytesread); //console.writeline("新得到的数据是" + getmsg + "呵呵"); //messagebox.show("新得到的数据是" + getmsg); string msg=encoding.default.getstring(objs.buffer, 0, bytesread); program.postlog(msg); //objs.obj.receive.invoke(objs.obj, msg);//因为这是静态函数,这个objs.obj实际是mytcp实例。 //messagebox.show(bytesread.tostring() + "更新数据后:" + state.sb.tostring()); // rest of data. client.beginreceive(objs.buffer, 0, objs.buffer.length, 0, new asynccallback(receivecallback), objs); } else { //接到0字节说明对方主动断开了连接; program.postlog("对方主动断开连接"); } } catch (socketexception sep) { //messagebox.show("receive回调函数出错了" + e.tostring()); /********************************************************************* * * 此处需要考察一下接收失败的异常情况。 * *********************************************************************/ program.postlog("在receivecallback这里"); exceptionsolver(sep); } } } }
i not sure, can try using tcpclient.client.poll method mentioned in 1 of post.
Comments
Post a Comment