Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Can a Unity program read from a network stream?

Discussion in 'Connected Games' started by Dirt-e, Jan 12, 2019.

  1. Dirt-e


    Jan 11, 2019
    Hey there :)

    Yes, my question really is that simple.

    I have a done a few C# projects a while ago, but this is my first time with Unity, so I think I’m overlooking something simple here: I would like my program to read data from a network stream. Just a few strings. I can make it do that by creating a TcpListener, with a few lines of C# code.

    Code (CSharp):
    1. TcpListener listener = null;
    3. listener = new TcpListener(IPAddress.Parse(„“), 48001);
    4. TcpClient client = listener.AcceptTcpClient();
    5. StreamReader reader = new StreamReader(client.GetStream());
    …that way I can read from a network stream :) awesome! But I run this code is in the start() function of an object :( probably not such a good idea.

    My problem is:
    1. Anytime I do that, the program freezes as it waits for incoming connections :-(
    2. It unfreezes ONLY when there actually IS a connection (UI unresponsive)
    3. Other objects cannot access the reader objects. Only the one in who’s start() function I have created it and it can only do so as long as it’s IN that function.
    I have solved this problem in another c# program by letting a BackgroundWorker object run the Server to keep the UI responsive, but that doesn’t seem to work in Unity, right?

    Basically, I want the reader object to be available to all other objects in the program. Or at least be able to call something like…

    Code (CSharp):
    1. if(reader.peek() != null)
    2. {
    3.     reader.readline();
    4.     //do something
    5. }
    6. Else
    7. {
    8.     //do nothing
    9. }
    …any time I like.

    Where or how shall I instantiate my TcpListener/client/reader objects to be able to use the object? Or are there other objects available to read from a network stream that I am unaware of?
  2. doctorpangloss


    Feb 20, 2013
    You need to accept connections and read data in a loop in a new Thread.

    As you finish reading the data, you have to put the data into a ConcurrentQueue.

    Poll the queue until it's empty in the Update function of a MonoBehaviour.
    Joe-Censored likes this.
  3. Dirt-e


    Jan 11, 2019
    Thank you very much for your help :)

    That was actually the path I initially pursued. I created a thread…

    Code (CSharp):
    1. Thread t = new Thread(run_server_run);
    2. t.start();
    And inside „run_server_run()“ I could do anything I wanted :) I spun up a server, connected a client and happily send and received data :) However, none of that was available from outside the thread :-(

    My attempts at solving this were:
    1. Raising an event inside the thread and have an object outside subscribe to it. The data was contained in the event Arguments. It worked (at least in a C# WinForm application), but I still felt like a (data-)trafficker smuggling illegal immigrant-data across the heavily guarded thread-border.
    2. Pipes ?!? Didn’t pursue that too far though… just an idea.
    3. MSMQ to get the data out and have my parent process read the messages. Not really what I would call „real-time“ though. Also didn’t pursue that too far.
    So, I take it that a „concurrent queue“ is the way to go to get data out of another thread? I will read into this and see how far I get before bothering you guys with further questions.


    Last edited: Jan 15, 2019 at 10:47 AM