Reputation: 3570
I need to connect a server (with ip and port) and create a read-loop that will get messages from the server as XML. sometimes there are no messages from the server.
I tried to create a connection (works fine) and read messages, I get the first message from the server and when I'm trying to read another one - it get stuck. I think that maybe there are no messages right now but I need that the loop will continue until there will be messages... it doesn't even go to "catch" or "finally", just do 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)
{
//
}
finally {
server.Close();
}
}
}
Upvotes: 2
Views: 4302
Reputation: 1062855
The loop is continuing, waiting for data. The issue here seems to be simply that ReadLine()
is a blocking call. You mention that there might not be a message yet; well, ReadLine()
is going to block until one of two conditions is met:
null
So basically, ReadLine()
is going to wait until either a message comes in, or the socket is closed. That is simply the behaviour of ReadLine()
. If that is problematic, you could work closer to the socket, and check NetworkStream.DataAvailable
but: note that only tells you if some data is currently available; it doesn't mean "this is an entire message", nor can it be used to tell if more messages will arrive. The main use of DataAvailable
is to decide between sync and async access. Plus if you work close to the socket you'll have to do all your own buffering and encoding/decoding.
It looks to me like ReadLine()
is working successfully. The only thing I would do here is re-phrase it a bit:
string line;
while((line = reader.ReadLine()) != null) {
// line is meaningful; do something
}
One last thought: xml is not always trivially split into messages simply on a "per-line" basis. You might want to consider some other form of framing, but that may well mean working closer to the socket, rather than a StreamReader
.
Upvotes: 5
Reputation: 6490
You have to wait till data arrives at the stream, you could try using follwing,
if(reader.EndOfStream)
continue;
Upvotes: -2