您的位置:首页 > 理论基础 > 计算机网络

使用命名管道通过网络在进程之间进行通信

2010-05-02 23:34 841 查看
命名管道提供的功能比匿名管道多。其功能包括通过网络进行全双工通信和多个服务器实例;基于消息的通信;以及客户端模拟,这使得连接进程可在远程服务器上使用其自己的权限集。

示例
下面的示例演示如何使用 NamedPipeClientStream 类创建命名管道。在此示例中,服务器进程创建了四个线程。每个线程可以接受一个客户端连接。连接的客户端进程随后向服务器提供一个文件名。如果客户端具有足够的权限,服务器进程就会打开文件并将其内容发送回客户端。

VBC#C++F#JScript
复制
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;

class PipeServer
{
static int numThreads = 4;

static void Main()
{
for (int i = 0; i < numThreads; i++)
{
Thread newThread = new Thread(new ThreadStart(ServerThread));
newThread.Start();
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
} // Main()

private static void ServerThread()
{
using (NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.InOut, numThreads))
{
Console.WriteLine("NamedPipeServerStream thread created.");

// Wait for a client to connect
pipeServer.WaitForConnection();

Console.WriteLine("Client connected.");
try
{
// Read the request from the client. Once the client has
// written to the pipe its security token will be available.
using (StreamReader sr = new StreamReader(pipeServer))
using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush = true;

// Verify our identity to the connected client using a
// string that the client anticipates.
sw.WriteLine("I am the true server!");

// Obtain the filename from the connected client.
string filename = sr.ReadLine();

// Read in the contents of the file while impersonating
// the client.
ReadFileToStream fileReader = new
ReadFileToStream(pipeServer, filename);

// Display the name of the user we are impersonating.
Console.WriteLine("Reading file: {0} as user {1}.",
pipeServer.GetImpersonationUserName(), filename);

pipeServer.RunAsClient(fileReader.Start);

pipeServer.Disconnect();
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}
} // ServerThread()
} // PipeServer

class ReadFileToStream
{
private string m_filename;
private Stream m_stream;

public ReadFileToStream(Stream stream, string filename)
{
m_filename = filename;
m_stream = stream;
} // ReadFileToStream(stream, filename)

public void Start()
{
using (StreamWriter sw = new StreamWriter(m_stream))
{
string contents = File.ReadAllText(m_filename);
sw.WriteLine(contents);
sw.Flush();
}
} // Start()
} // ReadFileToStream

下面的示例演示使用 NamedPipeClientStream 类的客户端进程。客户端连接服务器进程并向服务器发送一个文件名。服务器随后将文件内容发送回客户端。文件内容随后显示在控制台上。

VBC#C++F#JScript
复制
using System;
using System.IO;
using System.IO.Pipes;
using System.Security.Principal;

class PipeClient
{
static int numThreads = 4;

static void Main()
{
using (NamedPipeClientStream pipeClient =
new NamedPipeClientStream("localhost", "testpipe",
PipeDirection.InOut, PipeOptions.None,
TokenImpersonationLevel.Impersonation))
using (StreamWriter sw = new StreamWriter(pipeClient))
using (StreamReader sr = new StreamReader(pipeClient))
{
sw.AutoFlush = true;
pipeClient.Connect();

// Verify that this is the "true server"
if (sr.ReadLine() == "I am the true server!")
{
// The client security token is sent with the first write.
sw.WriteLine(@"c:\textfile.txt");

// Print the file to the screen.
char[] buffer = new char[32];
int n;
while ((n = sr.Read(buffer, 0, buffer.Length)) != 0)
{
Console.Write(buffer, 0, n);
}
}
else
{
Console.WriteLine("Server could not be verified.");
}
}
} // Main()
}

可靠编程
此示例中的客户端进程和服务器进程预期在同一台计算机上运行,因此提供给 NamedPipeClientStream 对象的服务器名称是 "localhost"。如果客户端进程和服务器进程位于不同的计算机上,则应该用运行服务器进程的计算机的网络名称来替换 "localhost"。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: