c# - when client disconnects my tcp/ip litsener goes insane /how to improve or fix it -



c# - when client disconnects my tcp/ip litsener goes insane /how to improve or fix it -

hello have tcp/ip listener works perfect untill client disconnects or sends string 1 time again have no thought how prepare

tcplistener serversocket = new tcplistener(****); serversocket.start(); console.writeline(" >> server started"); tcpclient clientsocket = serversocket.accepttcpclient(); console.writeline(" >> take connection client"); int requestcount = 0; while ((true)) { seek { //sql section of code mysqldatareader rdr = null; mysql.data.mysqlclient.mysqlconnection conn = new mysqlconnection(*******); conn.open(); //tcp listening section of code requestcount = requestcount + 1; networkstream networkstream = clientsocket.getstream(); byte[] bytesfrom = new byte[100025]; networkstream.read(bytesfrom, 0, clientsocket.receivebuffersize); string datafromclient = system.text.encoding.ascii.getstring(bytesfrom); datafromclient = datafromclient.substring(0, datafromclient.indexof("$")); //char[] delimiterchars = { ' ', '<', '>', ':', '\t' }; //string[] finaldata = datafromclient.split(delimiterchars); //var datalines = xdocument.load(datafromclient) ////parsing xml string //var datalines = xdocument.parse(datafromclient) // .descendants("dataline") // .select(n => new // { // amount = n.element("amount").value, // tellno = n.element("tellno").value // }); //foreach (var item in datalines) //{ // mysqlcommand cmd = new mysqlcommand("stringlistenerupdate", conn); // cmd.commandtype = commandtype.storedprocedure; // cmd.parameters.add(new mysqlparameter("@customerbalance", item.amount)); // cmd.parameters.add(new mysqlparameter("@customeraccountid", item.tellno)); // rdr = cmd.executereader(); // conn.close(); // console.writeline("{0}-{1}", item.amount, item.tellno); //} var voicelines = xdocument.parse(datafromclient) .descendants("voiceline") .select(n => new { amount = n.element("amount").value, tellno = n.element("tellno").value }); foreach (var item in voicelines) { mysqlcommand cmd = new mysqlcommand("stringlistenerupdate", conn); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add(new mysqlparameter("customerbalance", item.amount)); cmd.parameters.add(new mysqlparameter("customeraccountid", item.tellno)); cmd.executenonquery(); conn.close(); console.writeline("{0}-{1}", item.amount, item.tellno); } /// console.writeline(" >> info client : " + datalines); string serverresponse = "last message client :" + datafromclient; byte[] sendbytes = encoding.ascii.getbytes(serverresponse); networkstream.write(sendbytes, 0, sendbytes.length); networkstream.flush(); console.writeline(" >> " + serverresponse); clientsocket.close(); serversocket.stop(); console.writeline(" >> exit"); console.readline(); } grab (invalidoperationexception ex) { console.writeline(ex.tostring()); } grab (overflowexception ex) { console.writeline(ex.tostring()); } grab (indexoutofrangeexception ex) { console.writeline(ex.tostring()); } grab (exception ex) { console.writeline("suppressed exception: " + ex); } } } }

}

any ideas or suggestions appreciated on how prepare this(with explanation why , how ) , improve if question not acceptable or more info required please allow me know have debugged no problems occur except when client disconnects , re-sends ( in general find primitive tcp/ip listener

thank

use homecoming value of read, don't utilize receivebuffersize because not think does.

don't stop server socket after having processed single connection. server socket responsible listening. never connected.

c# sockets console-application

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -