i'm using c# networkstream
read/write ip address (not dns address).
program replacing old assembly language program on mainframe. it's on windows.
i'm writing/reading less 200
bytes. strings end linefeed
character i'm using streamreader.readline()
read response, after stream.write()
. on ibm write/read cycle took 300ms
.
now after every 2nd or 3 read, takes 15
seconds read. when read log of sender sending data in less second. reason these 15
second delays.
i'm clueless on what's happening.
p.s. 1 weird thing noticed if set stream read timeout 4 seconds, times out around 4 seconds. if set timeout 10 seconds or no timeout, times out after 15 seconds.
tcpclient tcpc = null; networkstream stream = null; streamreader sr = null; tcpc = new tcpclient(); tcpc.nodelay = true; tcpc.exclusiveaddressuse = false; tcpc.connect("172.18.10.100", 4004); stream = tcpc.getstream(); sr = new streamreader(stream, encoding.ascii); sr.peek(); string message = null; message = "ix3543543" + '\r'; stream.write(encoding.ascii.getbytes(message), 0, message.length); string readmsg = null; (int = 0; < 4; i++) readmsg = sr.readline();
your connection stays open code never free idisposable
resources.
- i think code should run faster if add
using
constructure. - also can merge declaration , assignment, (this style comment, main concern `idisposable usage)
- and can
readtoend
message in return, , examine youself after releasing resources.
so code this:
string response = null; using(var tcpc = new tcpclient()) { tcpc.nodelay = true; tcpc.exclusiveaddressuse = false; tcpc.connect("172.18.10.100", 4004); using (var stream = tcpc.getstream()) using (var sr = new streamreader(stream, encoding.ascii)) { sr.peek(); var message = "ix3543543" + '\r'; stream.write(encoding.ascii.getbytes(message), 0, message.length); response = sr.readtoend(); } } // examine message here var lines = response.split(new string[] { environment.newline }, stringsplitoptions.removeemptyentries);