|
-
Feb 21st, 2007, 07:54 PM
#1
Thread Starter
Lively Member
TcpClient Question
I have a serial to ethernet device that transmits and receives data on a TCP port. I have a GPS device connected to the S-to-E device. The GPS is constantly spitting out data in NMEA string format. I need to be able to connect to the port and listen for the data coming in. I have a basic client working, but it slowly lags and eventually is not returning valid data. When I connect directly to the GPS on a serial port, it streams the data real time, but when I connect via TCP, it buffers it. Also, when connected to the serial port, I can call the ReadLine() method, which grabs everything up to the \r\n at the end.
My question is: Is it possible to connect to a TCP stream and read data one line at a time? The line lengths will be variable, so I can't specify a size of the buffer, that I am aware of.
That was kind of a jumbled mess, if you need any other information or have ideas, let me know.
Thanks
-
Feb 26th, 2007, 12:43 PM
#2
Thread Starter
Lively Member
Re: TcpClient Question
Can a mod please move this to the network programming forum? I'm thinking I might get an answer there, and I don't want to flood the forums with extra threads.
Thanks
-
Feb 26th, 2007, 05:31 PM
#3
Re: TcpClient Question
You can construct a StreamReader on the TcpClient's stream and use that to read line by line:
Code:
TcpClient tcp = new TcpClient();
// .. etc ..
StreamReader sr = new StreamReader(tcp.GetStream());
while(tcp.Connected())
{
string line = sr.ReadLine();
// parse GGA, etc..
}
The stream reader will block until a full line (\r\n) is available to be read, and then return that line. The underlying buffer on the tcp client shouldn't be an issue. Post back if you have more questions!
Hope this helps,
John
Last edited by sunburnt; Feb 26th, 2007 at 05:34 PM.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Feb 26th, 2007, 07:43 PM
#4
Thread Starter
Lively Member
Re: TcpClient Question
Works perfect! I never thought to use a standard streamreader, I assumed I had to use a network stream, seeing as it's a network connection. Thanks.
 Originally Posted by sunburnt
You can construct a StreamReader on the TcpClient's stream and use that to read line by line:
Code:
TcpClient tcp = new TcpClient();
// .. etc ..
StreamReader sr = new StreamReader(tcp.GetStream());
while(tcp.Connected())
{
string line = sr.ReadLine();
// parse GGA, etc..
}
The stream reader will block until a full line (\r\n) is available to be read, and then return that line. The underlying buffer on the tcp client shouldn't be an issue. Post back if you have more questions!
Hope this helps,
John
-
Apr 23rd, 2012, 12:49 AM
#5
New Member
Re: TcpClient Question
Readline in the above example only reads first line. Can't read all lines.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|