c# - Why is my network read/write speed so slow on my app? -
ok, have made server\client app file transportation using single-thread in console app. whenever run both client , server on same network runs perfectly, whenever try across internet very, very, slow running @ 130000 bytes second. client using high-speed ethernet connection , server using wireless connection. using port forwarding send requests port 54321 computer running server. using tcplistener
listen connection, tcpclient
accept connection, streamreader
, streamwriter
attached networkstream
send/receive data. ideas on why might slow? way, know fact not wireless network causing lag, think program. here code :
server :
using system; using system.collections.generic; using system.linq; using system.text; using system.net; using system.net.sockets; using system.io; namespace server { class program { static bool dontloop; static string selectedfile; static string[] filearray; static bool authgood; static bool goodexit; static string version = "1.0.0"; static int timeout; static bool multifile; static bool ipblock; static bool auth; static string msg; static string authpath; static string banpath; static int port; static tcplistener tcp1; static tcpclient tcp2; static networkstream net; static streamreader sr; static streamwriter sw; static binaryreader br; static binarywriter bw; static long length; static int32 buffersize; static byte[] buffer; static filestream mainfile; static filestream secondaryfile; static string filelength; static filestream settingsfile; static streamreader settingsreader; static string path; static void main(string[] args) { while (true) { try { console.inputencoding = utf8encoding.utf8; console.outputencoding = utf8encoding.utf8; console.title = "file server [version : " + version + "] <sf>"; if (console.bufferwidth > 59) { console.writeline(@" $$$$$$$$\ $$\ $$\ $$ _____|\__|$$ | $$ | $$\ $$ | $$$$$$\ $$$$$\ $$ |$$ |$$ __$$\ $$ __| $$ |$$ |$$$$$$$$ | $$ | $$ |$$ |$$ ____| $$ | $$ |$$ |\$$$$$$$\ \__| \__|\__| \_______| $$$$$$\ $$ __$$\ $$ / \__| $$$$$$\ $$$$$$\ $$\ $$\ $$$$$$\ $$$$$$\ \$$$$$$\ $$ __$$\ $$ __$$\\$$\ $$ |$$ __$$\ $$ __$$\ \____$$\ $$$$$$$$ |$$ | \__|\$$\$$ / $$$$$$$$ |$$ | \__| $$\ $$ |$$ ____|$$ | \$$$ / $$ ____|$$ | \$$$$$$ |\$$$$$$$\ $$ | \$ / \$$$$$$$\ $$ | \______/ \_______|\__| \_/ \_______|\__| "); } else { console.writeline("[file server]"); } settingsfile = new filestream(environment.currentdirectory.tostring() + @"\settings.cfg", filemode.open, fileaccess.read); settingsreader = new streamreader(settingsfile); string temp0 = settingsreader.readtoend(); string[] settings = temp0.split(new string[] { environment.newline }, stringsplitoptions.none); port = toint(settings.skip(0).take(1).first().split('=').last()); path = settings.skip(1).take(1).first().split('=').last(); authpath = settings.skip(2).take(1).first().split('=').last(); auth = convert.toboolean(settings.skip(3).take(1).first().split('=').last()); banpath = settings.skip(4).take(1).first().split('=').last(); ipblock = convert.toboolean(settings.skip(5).take(1).first().split('=').last()); timeout = toint(settings.skip(6).take(1).first().split('=').last()); multifile = convert.toboolean(settings.skip(7).take(1).first().split('=').last()); settingsfile.close(); settingsreader.close(); tcp1 = new tcplistener(new ipendpoint(ipaddress.any, port)); tcp1.start(); console.writeline("server started. listening on '{0}'.", tcp1.localendpoint.tostring()); tcp2 = tcp1.accepttcpclient(); tcp1.server.close(); net = tcp2.getstream(); console.writeline("client '{0}' connected.", tcp2.client.remoteendpoint.tostring().split(':').first()); net.readtimeout = timeout; net.writetimeout = timeout; console.writeline("using '{0}' ms timeout.", timeout); sr = new streamreader(net); sw = new streamwriter(net); string temp1 = sr.readline(); if (temp1 != version) { throw new exception("client version '" + temp1 + "' not match server version '" + version + "'!"); } sw.writeline(net.readtimeout.tostring()); sw.flush(); if (ipblock == true) { string clientip = tcp2.client.remoteendpoint.tostring().split(':').first(); filestream banstream = file.openread(banpath); streamreader banreader = new streamreader(banstream); while (banreader.endofstream == false) { if (banreader.readline() == clientip) { throw new exception("client ip banned!"); } } console.writeline("client '{0}' not banned.", clientip); } if (auth == true) { sw.writeline("auth=true"); sw.flush(); console.writeline("waiting client response..."); string creds = sr.readline(); console.writeline("client attempting login '{0}'.", creds); filestream authstream = file.openread(authpath); streamreader authreader = new streamreader(authstream); while (authreader.endofstream == false) { if (authreader.readline() == creds) { authgood = true; sw.writeline("accept"); flush(); console.writeline("client login successful."); } } if (authgood == false) { sw.writeline("deny"); flush(); throw new exception("client authentication failed!"); } } if (auth == false) { sw.writeline("auth=false"); sw.flush(); } dontloop = false; while (dontloop == false) { if (multifile == true) { sw.writeline("multifile=true"); flush(); string temp3 = sr.readline(); if (temp3 != "ok") { throw new exception("client response incorrect! errcode=1."); } else { while (true) { filearray = directory.getfiles(path); foreach (string s in filearray) { try { secondaryfile = file.openread(s); filelength = secondaryfile.length.tostring(); secondaryfile.close(); } catch (exception) { filelength = "(length unavailable)"; } sw.writeline("{0} | {1} bytes.", s, filelength); sw.flush(); } sw.writeline("status=done"); sw.flush(); console.writeline("waiting client response..."); selectedfile = sr.readline(); console.writeline("received client response."); if (selectedfile != "0") { break; } else { console.writeline("client refreshed file list."); } } int index = convert.toint32(selectedfile) - 1; string temp5 = filearray.skip(index).take(1).toarray().first().tostring(); console.writeline("client selected '{0}'.", temp5); mainfile = file.openread(temp5); length = mainfile.length; if (length < 65000) { buffersize = toint(length.tostring()); } else { buffersize = 65000; } console.writeline("using '{0}' byte buffer.", buffersize.tostring()); } } if (multifile == false) { sw.writeline("multifile=false"); flush(); string temp3 = sr.readline(); if (temp3 != "ok") { throw new exception("client response incorrect! errcode=2."); } else { try { secondaryfile = file.openread(path); filelength = secondaryfile.length.tostring(); secondaryfile.close(); } catch (exception) { filelength = "(length unavailable)"; } sw.writeline("{0} | {1}", path, filelength); sw.flush(); console.writeline("waiting client response..."); string temp4 = sr.readline(); console.writeline("received client response."); if (temp4 != "ok") { throw new exception("client response incorrect! errcode=3."); } mainfile = file.openread(path); length = mainfile.length; if (length < 65000) { buffersize = toint(length.tostring()); } else { buffersize = 65000; } console.writeline("using '{0}' byte buffer.", buffersize.tostring()); } } sw.writeline(mainfile.length.tostring()); sw.flush(); sw.writeline("ready?"); sw.flush(); console.writeline("waiting client response..."); string temp6 = sr.readline(); if (temp6 != "yes") { throw new exception("client response incorrect! errcode=4."); } br = new binaryreader(mainfile); bw = new binarywriter(net); while (true) { try { string temp9 = sr.readline(); if (temp9.tostring() == "done") { console.writeline(); console.writeline("waiting client response..."); string temp7 = sr.readline(); if (temp7 == "close") { try { tcp2.close(); if (tcp2.connected == false) { throw new exception(); } } catch (exception) { goodexit = true; dontloop = true; break; } } if (temp7 == "again") { console.writeline("client wants download file."); mainfile.close(); dontloop = false; break; } } long temp8 = convert.toint64(temp9); mainfile.position = temp8; if (length - temp8 >= buffersize) { buffersize = 65000; } if (length - temp8 < buffersize) { buffersize = toint((length - temp8).tostring()); } buffer = new byte[buffersize]; buffer = br.readbytes(buffer.length); bw.write(buffer); string percent = math.round(((double)((double)temp8 / (double)length) * 100), 2, midpointrounding.awayfromzero).tostring(); if (percent == "100" && temp8 < length) { percent = "<100"; } console.write("\rclient requested {0} / {1} bytes | {2}% done. ", temp8, length, percent); bw.flush(); } catch (exception err) { dontloop = true; goodexit = false; closeall(); console.writeline(); console.writeline(err.message.tostring()); console.writeline(); break; } } if (goodexit == true) { closeall(); console.writeline("task complete."); break; } } } catch (exception err) { closeall(); console.writeline(); console.writeline(err.message.tostring()); console.writeline(); } console.writeline("server restarting in 10 seconds."); system.threading.thread.sleep(10000); } } static int toint(string string_) { int = convert.toint32(string_); return i; } static void halfclose() { mainfile.close(); bw.close(); br.close(); } static void flush() { sw.flush(); } static void closeall() { try { br.close(); } catch { } try { bw.close(); } catch { } try { sr.close(); } catch { } try { sw.close(); } catch { } try { net.close(); } catch { } try { tcp2.close(); } catch { } try { tcp1.server.close(); } catch { } try { tcp1.stop(); } catch { } } } }
client : can found here -> ftp://98.122.51.199/program.cs
(too many characters body, address above ftp server)
you shouldn't have flush stream. believe delays harddisk io.
Comments
Post a Comment