c# - Tcp/IP listener parsing xml string output advice(code provided just not working as it should) -
i advice on how fix code. it's tcp/ip listener
requestcount = requestcount + 1; networkstream networkstream = clientsocket.getstream(); byte[] bytesfrom = new byte[1000025]; networkstream.read(bytesfrom, 0, (int)clientsocket.receivebuffersize); string datafromclient = system.text.encoding.ascii.getstring(bytesfrom); datafromclient = datafromclient.substring(0, datafromclient.indexof("$")); var rap= xdocument.parse(datafromclient) .descendants("gag") .select(n => new { re= n.element("re").value, we= n.element("we").value }).tostring(); console.writeline(" >> data client : " + rap); string serverresponse = "last message client :" + datafromclient; byte[] sendbytes = encoding.ascii.getbytes(serverresponse); networkstream.write(sendbytes, 0, sendbytes.length); networkstream.flush(); console.writeline(" >> " + serverresponse);
that listens string
<?xml version="1.0" > <tt> <gag> <re>monkey</re> <we>chicken</we> </gag> <sap> <re>monkey</re> <we>chicken</we> </sap> </tt>
i attempting show output monkey , chicken in console advice appreciated still whole string please dont post answers in comments
thank much
byte[] bytesfrom = new byte[1000025]; networkstream.read(bytesfrom, 0, (int)clientsocket.receivebuffersize); string datafromclient = system.text.encoding.ascii.getstring(bytesfrom);
oh, let me count problems:
- a huge buffer - not sure why; not wrong per se, inadviseable
- not checking reply
read
- that's huge problem; how data did read? - not looping either read all data, or read frame (logical message) of data
- assuming ascii - well, maybe ascii; i'd buffer bytes, though
i suspect (assuming client closes outbound socket after send):
// *remove* `read` code etc // ... var rap= xdocument.load(networkstream) // before
and work beautifully.
if client not close outbound socket after send, you're going have harder time of it.
edit: jon notes - you'll need use rap
useful.
Comments
Post a Comment