- Home /
Using Pipes/Streams Freezes Unity
Hello,
So I'm trying to get two processes to communicate with each other. Specifically a Visual C# Program that does something and sends strings through the pipe as it does it, and a Unity C# script which receives those messages. The problem is that unity, whenever I run this script (Included below) freezes and nothing happens. For the sake of testing there is an empty Unity Game Object that only has the script in it.
If anyone knows anything about this problem, or can help me out, that would be very helpful. I'm using these programs from MSDN for reference on how to communicate using Pipes in C#: http://msdn.microsoft.com/en-us/library/bb546102.aspx
using UnityEngine;
using System.Collections;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;
public class iPhoneCommunicator : MonoBehaviour {
Process pipeClient;
AnonymousPipeServerStream pipeServer;
public void Start(){
Console.WriteLine("Hello, World!");
pipeClient = new Process();
// Point the program to the executable
pipeClient.StartInfo.FileName = "C:\\Users\\Eugene\\Briefcase\\School Documents\\CSE 4904 - Senior Design\\src\\TeamB-Code\\AirControl\\AirControlServerWindows\\AirControlServerAlpha\\bin\\Debug\\AirControlTest.exe";
// Initialize the Server Stream
pipeServer = new AnonymousPipeServerStream(PipeDirection.In,HandleInheritability.Inheritable);
//Set Transmission mode to bytes, since messages are not accepted
pipeServer.ReadMode = PipeTransmissionMode.Byte;
// Pass the client process a handle to the server.
pipeClient.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
// Disable Console output for the client
pipeClient.StartInfo.UseShellExecute = false;
// Start the client
pipeClient.Start();
pipeServer.DisposeLocalCopyOfClientHandle();
}
public void Update(){
Console.WriteLine("Updating");
using (StreamReader sr = new StreamReader(pipeServer))
{
// Display the read text to the console
string temp;
// Wait for 'sync message' from the server.
//Synchronize with the client, wait for a "SYNC" message
do
{
Console.WriteLine("[SERVER] Wait for sync...");
temp = sr.ReadLine();
}
while (!temp.StartsWith("SYNC"));
// Read the server data and echo to the console.
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("[SERVER] Echo: " + temp);
}
}
}
}
public static class Console {
public static void WriteLine(System.Object obj){
UnityEngine.Debug.Log(obj.ToString());
}
public static void WriteError(System.Object obj){
UnityEngine.Debug.LogError(obj.ToString());
}
}
Answer by Summit_Peak · Jan 02, 2017 at 05:12 PM
DisposeLocalCopyOfClientHandle()
throws an error. The only option is to comment this line out.
GetClientHandleAsString()
returns an integer in string format ("17116"), but the constructor on the client side cannot connect to this handle:
//pipe = new AnonymousPipeClientStream(PipeDirection.In, Environment.GetCommandLineArgs()[0]);
pipe = new AnonymousPipeClientStream(PipeDirection.In, "17116");
With no connection, the code gets stuck forever on this line: temp = sr.ReadLine();
Your answer
Follow this Question
Related Questions
ReadLine from Named Pipe freezes Unity 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Keep distance using NavMeshAgent causing freeze. 1 Answer
Aqueducts connecting. 0 Answers