Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to to "bridge" a stream to another device? StreamReader to StreamWriter.

Discussion in 'Scripting' started by DCTShinobi, Oct 9, 2023.

  1. DCTShinobi

    DCTShinobi

    Joined:
    Jun 23, 2014
    Posts:
    51
    I'm attempting to make an application that will receive a stream from DCS's (a flight simulator) Export.lua file on the same machine, but then export specific data to another application on a tablet over wifi.

    I've learned how to receive the information into the first application through a TcpListener/StreamReader. But I don't if or how to simultaneously export out information to the tablet. I'm thinking of using a StreamWriter, but I can only find information about writing to a file, not to an actual stream/Tcp connection.

    Where can I find information and/or examples of such a use? Thank you! :)


    P.S. A second application would be unnecessary if there is a way to read only the last (most current) line from a stream. Unfortunately, I can't find a way to do that. So the stream information builds up two or three times faster than my tablet can read it... so the displayed information gets very old very quickly.
     
  2. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    341
    As simple as:
    Code (CSharp):
    1. sourceStream.CopyTo(destinationStream);
    Or if you want more control, report progress...etc

    Code (CSharp):
    1. var bufferSize = 8192;
    2. var buffer = new byte[bufferSize];
    3. int count;
    4. while ((count = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
    5. {
    6.     destinationStream.Write(buffer, 0, count);
    7. }
     
    DCTShinobi likes this.
  3. DCTShinobi

    DCTShinobi

    Joined:
    Jun 23, 2014
    Posts:
    51
    Thank you! Yes, I'm looking to just take the last line of the stream at a given interval (say every second) on my PC, parse it into the different variables (float for speed and altitude, etc.), convert into my preferred format (meters to feet, etc.), then send that new data to the tablet.

    Oddly enough, DCS has a function to export data at certain intervals, and it works wonderfully on my tablet with singleplayer, but but doesn't seem to work on the multiplayer server I play on, though the function to export every-frame does work. It's just overloading my tablet's speed of reading.

    I'm still not sure of the best course of action. I've even tried to use timers/coroutines on the Unity app to get it to use more current stream data (playing around with the ReadToEnd method), but haven't had any success.

    So maybe using a bridge with a form of your code will work. Thank you again! :)