mark dragan

mark dragan

  • NA
  • 38
  • 0

Named pipes keep it open connection?

Sep 19 2009 1:08 PM

Hi everyone I need help with named pipes.How can I create connection between server and client,so that client can recieve new information from server in any time.Meanwhile client executes hi code.In this code:
SERVER:
static
void Main()
{
using (NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.Out))
{
Console.WriteLine("NamedPipeServerStream object created.");
// Wait for a client to connect
Console.Write("Waiting for client connection...");
pipeServer.WaitForConnection();
Console.WriteLine("Client connected.");
try
{
// Read user input and send that to the client process.
using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush =
true;
Console.Write("Enter text: ");
sw.WriteLine(
Console.ReadLine());
}
}
// Catch the IOException that is raised if the pipe is
// broken or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}
CLIENT:
 

static void Main(string[] args)
{
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.In))
{
// Connect to the pipe or wait until the pipe is available.
Console.Write("Attempting to connect to pipe...");
pipeClient.Connect();
Console.WriteLine("Connected to pipe.");
Console.WriteLine("There are currently {0} pipe server instances open.",
pipeClient.NumberOfServerInstances);
using (StreamReader sr = new StreamReader(pipeClient))
{
// Display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("Received from server: {0}", temp);
}
}
}

Console.Write("Press Enter to continue...");

Console.ReadLine();

}
In this code client will recieve message from server and then close connection.Is there anyway that client can keep connection alive.
 

Answers (2)