c# - How To Reconnect to Tcp server from tcp client upon connection failure -
c# - How To Reconnect to Tcp server from tcp client upon connection failure -
i have server, sends info in irregular periods of time , sends heartbeats every 30 seconds of ideal time.
i wrote tcp client connect server , read data. works ok, when wrong network , server becomes unavailable need next actions.
when connection fails between client , server need seek reconnect server (repeat process 3 times 30 seconds interval if server not available after interval, on successful re-connection need go on working process else need display message server not available.)
how can accomplish this? please help me. here's code.
private void servicemethod(string port, string host) { seek { //create new client socket ... m_socclient = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp); string szipselected = host; string szport = port; int alport = system.convert.toint16(szport, 10); system.net.ipaddress remoteipaddress = system.net.ipaddress.parse(szipselected); system.net.ipendpoint remoteendpoint = new system.net.ipendpoint(remoteipaddress, alport); //m_socclient = new socket(remoteendpoint.address.addressfamily, sockettype.stream, protocoltype.tcp); m_socclient.beginconnect(remoteendpoint, new asynccallback(onclientconnect), null); } grab (socketexception se) { //code handle exception } grab (exception ex) { } } public void onclientconnect(iasyncresult asyn) { seek { object objdata = "client connected"; byte[] bydata = system.text.encoding.ascii.getbytes(objdata.tostring()); m_socclient.send(bydata); traceservice("client connected @ " + m_socclient.remoteendpoint); m_socclient.endconnect(asyn); waitfordata(m_socclient); } grab (objectdisposedexception) { system.diagnostics.debugger.log(0, "1", "\n onclientconnection: socket has been closed\n"); } grab (socketexception se) { // messagebox.show(se.message); } } public class csocketpacket { public system.net.sockets.socket thissocket; public byte[] databuffer = new byte[100]; } public void waitfordata(system.net.sockets.socket soc) { seek { if (pfnworkercallback == null) { pfnworkercallback = new asynccallback(ondatareceived); } csocketpacket thesocpkt = new csocketpacket(); thesocpkt.thissocket = soc; // start hear data... soc.beginreceive(thesocpkt.databuffer, 0, thesocpkt.databuffer.length, socketflags.none, pfnworkercallback, thesocpkt); } grab (socketexception se) { //messagebox.show(se.message); } } public void ondatareceived(iasyncresult asyn) { seek { csocketpacket thesockid = (csocketpacket)asyn.asyncstate; //end receive... int irx = 0; irx = thesockid.thissocket.endreceive(asyn); char[] chars = new char[irx + 1]; system.text.decoder d = system.text.encoding.utf8.getdecoder(); int charlen = d.getchars(thesockid.databuffer, 0, irx, chars, 0); asciiencoding asen = new asciiencoding(); response = system.convert.tochar(system.convert.touint32("6", 16)).tostring(); m_socclient.send(asen.getbytes(response)); grab (objectdisposedexception) { system.diagnostics.debugger.log(0, "1", "\nondatareceived: socket has been closed\n"); if (m_socclient.connected) { waitfordata(m_socclient); } } grab (socketexception se) { if (m_socclient.connected) { waitfordata(m_socclient); } } grab (exception e) { //messagebox.show(e.message); if (m_socclient.connected) { waitfordata(m_socclient); } } }
c# sockets asynchronous tcp tcpclient
Comments
Post a Comment