Hi, I'm trying to get familiar with named pipes in Unity. To get started I ave a simple test program where the server sends '1' to the client, and the client sends '2' to the server. I got the program running for two console application. Now I want Unity to communicate with my console application. I have the following code.
The console application:
using System;
using System.Text;
using System.IO.Pipes;
namespace PipeSample
{
class Program
{
static void Main(string[] args)
{
using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("test_pipe",PipeDirection.InOut))
{
Console.WriteLine("Waiting for connection on named pipe");
namedPipeServer.WaitForConnection();
namedPipeServer.WriteByte(1);
int byteFromClient = namedPipeServer.ReadByte();
Console.WriteLine(byteFromClient);
}
Console.ReadLine();
}
}
}
Unity Code:
using UnityEngine;
using System.Collections;
using System.IO.Pipes;
public class PipeWork : MonoBehaviour
{
void ReceiveByteAndRespond()
{
using (NamedPipeClientStream clientStream = new NamedPipeClientStream("test_pipe"))
{
clientStream.Connect();
Debug.Log(clientStream.ReadByte());
clientStream.WriteByte(2);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ReceiveByteAndRespond();
}
}
}
So when I run the code and press space, in the Unity console I see '1' as expected.
Unfortunately in the console application I see '1' too after some delay.
Can somebody explain to me why this happens?
↧