c# - read-loop from TCP/IP -


i need connect server (with ip , port) , create read-loop messages server xml. there no messages server.

i tried create connection (works fine) , read messages, first message server , when i'm trying read 1 - stuck. think maybe there no messages right need loop continue until there messages... doesn't go "catch" or "finally", nothing..

public class connection {     public connection()     {         socket server = null;         try         {             string p = string.empty;             using (var client = new tcpclient(myipaddress, myport))             using (var stream = client.getstream())             using (var reader = new streamreader(stream))             {                while (p != null)                {                     try                     {                         p = reader.readline();                     }                     catch (exception e)                     {                        //                     }                }             }         }         catch (exception e)         {             //         }         {              server.close();          }     } } 

the loop is continuing, waiting data. issue here seems readline() blocking call. mention there might not message yet; well, readline() going block until 1 of 2 conditions met:

  • it can read data, terminated newline (or eof, i.e. message without newline followed socket closure) - in case returns line of data
  • no more data received and stream closed, in case returns null

so basically, readline() going wait until either message comes in, or socket closed. behaviour of readline(). if problematic, work closer socket, , check networkstream.dataavailable but: note tells if data currently available; doesn't mean "this entire message", nor can used tell if more messages will arrive. main use of dataavailable decide between sync , async access. plus if work close socket you'll have own buffering , encoding/decoding.

it looks me readline() working successfully. thing here re-phrase bit:

string line; while((line = reader.readline()) != null) {     // line meaningful; } 

one last thought: xml not trivially split messages on "per-line" basis. might want consider other form of framing, may mean working closer socket, rather streamreader.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -