c# - SocketAsync, can't reconnect to server -
i have problem socketasync application, have server, , client (the code below), communication works great except when server application closed, client cant reconnect. if restart client, no problem @ all, connect again, has restarted. question how can modify code, reconnect without restarting application self.
this connection code!
public client() { try { this.ip = ini.getfromini("config", "ip"); this.port = ini.getfromini("config", "port"); data = new byte[60000]; receivebuffer = new byte[60000]; this.hostendpoint = new ipendpoint(ipaddress.parse(this.ip), convert.toint32(port)); this.clientsocket = new socket(this.hostendpoint.addressfamily, sockettype.stream, protocoltype.tcp); // instantiates endpoint , socket. starttimer = new system.timers.timer(); starttimer.elapsed += new elapsedeventhandler(startsendloop); starttimer.interval = 1000; starttimer.start(); sendtimer = new system.timers.timer(); sendtimer.elapsed += new elapsedeventhandler(sendloop); sendtimer.interval = 500; sendtimer.start(); } catch (exception e) { } } public void startsendloop(object sender, elapsedeventargs e) { try { starttimer.interval = 10000; starttimer.enabled = false; if (!this.clientsocket.connected) { this.hostendpoint = new ipendpoint(ipaddress.parse(this.ip), convert.toint32(port)); this.clientsocket = new socket(this.hostendpoint.addressfamily, sockettype.stream, protocoltype.tcp); if (connectargs != null) connectargs.dispose(); connectargs = new socketasynceventargs(); connectargs.usertoken = this.clientsocket; connectargs.remoteendpoint = this.hostendpoint; connectargs.completed += new eventhandler<socketasynceventargs>(onconnect); clientsocket.connectasync(connectargs); bool test = autoconnectevent.waitone(5000); gotdata = true; lasttime = datetime.now; } starttimer.enabled = true; } catch (exception ex) { messagebox.show(ex.tostring()); } }
¨
more code if wants see it... not directly linked connection.
private void onconnect(object sender, socketasynceventargs e) { autoconnectevent.set(); // set flag socket connected. this.connected = (e.socketerror == socketerror.success); } private void onsend(object sender, socketasynceventargs e) { // signals end of send. sendsuccess = true; autosendreceiveevents[receiveoperation].set(); if (e.socketerror == socketerror.success) { if (e.lastoperation == socketasyncoperation.send) { // prepare receiving. //socket s = e.usertoken socket; (int = 0; < receivebuffer.length; i++) { if (receivebuffer[i] != 0) receivebuffer[i] = 0; else break; } e.setbuffer(receivebuffer, 0, receivebuffer.length); e.completed += new eventhandler<socketasynceventargs>(onreceive); clientsocket.receiveasync(e); //sendloop(); } } else { this.processerror(e); } } private void onreceive(object sender, socketasynceventargs e) { try { while (true) { if (this.canreceive) { canreceive = false; string stringdata; int recv = 0; (int = 0; < e.buffer.length; i++) { if (e.buffer[i] != 0) recv++; else break; } if (recv > 0) { console.writeline(recv.tostring()); int count = 0; (int = 0; < data.length; i++) { if (data[i] != 0) count++; else break; } e.buffer.copyto(data, count); //currentdata = encoding.utf8.getstring(e.buffer, 0, recv); lasttime = datetime.now; gotdata = true; if ((byte)data[count + recv - 1] == (byte)255) { int cnt = -1; (int = 0; < count + recv; i++) { if (data[i] == (byte)254) { cnt = i; break; } } int nr = (count + recv) - cnt - 2; byte[] tmp = new byte[nr]; (int = 0; < nr; i++) { tmp[i] = data[cnt + + 1]; } string crc = encoding.default.getstring(tmp); stringdata = encoding.default.getstring(data, 0, cnt); msgstruct msgs = new msgstruct(stringdata); msgs.setcrc(crc); todo.add(msgs); (int = 0; < data.length; i++) { if (data[i] != 0) data[i] = 0; else break; } allreadygot = false; handletodo(); // signals end of receive. } } autosendreceiveevents[sendoperation].set(); socket s = e.usertoken socket; canreceive = true; break; } else { thread.sleep(10); } } } catch (exception ex) { canreceive = true; } } private void processerror(socketasynceventargs e) { socket s = e.usertoken socket; if (s.connected) { // close socket associated client try { s.shutdown(socketshutdown.both); } catch (exception) { // throws if client process has closed } { if (s.connected) { s.close(); } } } // throw socketexception //throw new socketexception((int32)e.socketerror); } public void sendloop(object sender, elapsedeventargs e) { try { if (this.clientsocket.connected) { sendtimer.enabled = false; bool waitextra = false; datetime tmpdate = lasttime.addseconds(30); if (datetime.now > tmpdate && tmpdate != datetime.minvalue) { gotdata = false; } if(sendsuccess) { canusebuffert = false; if (msghand.gotmsg() && data[0] == 0) { if (msghand.getnextmsg() != null && msghand.getnextmsg().getmsg().length != 0) { byte[] ba = encoding.default.getbytes(msghand.getnextmsg().getmsg()); if (msghand.getnextmsg().getcrc() == "") { ulong tmp = crc.calc_crc(ba, ba.length); msghand.getnextmsg().setcrc(tmp.tostring("x")); } if (msghand.getnextmsg().cansendbytimeout()) { string crcstr = "?" + msghand.getnextmsg().getcrc() + "?"; byte[] bb = encoding.default.getbytes(crcstr); crcstr = encoding.default.getstring(bb); byte[] fullmsg = new byte[ba.length + bb.length]; bb[0] = 254; bb[bb.length - 1] = 255; ba.copyto(fullmsg, 0); bb.copyto(fullmsg, ba.length); string s = system.text.encoding.default.getstring(fullmsg); completeargs = new socketasynceventargs(); completeargs.setbuffer(fullmsg, 0, fullmsg.length); completeargs.usertoken = this.clientsocket; completeargs.remoteendpoint = this.hostendpoint; completeargs.completed += new eventhandler<socketasynceventargs>(onsend); sendsuccess = false; // start sending asyncronally. clientsocket.sendasync(completeargs); // wait send/receive completed. //autoresetevent.waitall(autosendreceiveevents); // return data socketasynceventargs buffer. //stm.write(fullmsg, 0, fullmsg.length); if (!msghand.getnextmsg().isackneeded()) msghand.removenextmsgs(); } } } else { waitextra = true; byte[] bba = encoding.default.getbytes("x"); completeargs = new socketasynceventargs(); completeargs.setbuffer(bba, 0, bba.length); completeargs.usertoken = this.clientsocket; completeargs.remoteendpoint = this.hostendpoint; completeargs.completed += new eventhandler<socketasynceventargs>(onsend); sendsuccess = false; //dessa ska in även den andra applikationen // start sending asyncronally. clientsocket.sendasync(completeargs); // wait send/receive completed. //autoresetevent.waitall(autosendreceiveevents); } } canusebuffert = true; //if (!clientsocket.connected && !gotdata) //disconnect(); } sendtimer.enabled = true; } catch (exception ex) { system.diagnostics.debug.writeline(ex.tostring()); sendtimer.enabled = true; } } internal void disconnect() { clientsocket.disconnect(true); } public void dispose() { autoconnectevent.close(); autosendreceiveevents[sendoperation].close(); autosendreceiveevents[receiveoperation].close(); if (this.clientsocket.connected) { this.clientsocket.close(); } } public void handletodo() { //handle msg }
Comments
Post a Comment