Results 1 to 5 of 5

Thread: TcpClient Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    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

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    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

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    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.

    Quote 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

  5. #5
    New Member
    Join Date
    Apr 2012
    Posts
    1

    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
  •  



Click Here to Expand Forum to Full Width