level 1
在MSDN中,提到了——如何:使用命名管道通过网络在进程之间进行通信 这个示例。
可是要怎么才能做出它提到的客户端与服务器端的程序呢?
它的代码完全不知道要放在哪里呀!
2011年05月04日 13点05分
1
level 1
MSDN中是这样说的:
名管道提供的功能比匿名管道多。其功能包括通过网络进行全双工通信和多个服务器实例;基于消息的通信;以及客户端模拟,这使得连接进程可在远程服务器上使用其自己的权限集。
示例下面的示例演示如何使用 NamedPipeClientStream 类创建命名管道。 在此示例中,服务器进程创建了四个线程。每个线程可以接受一个客户端连接。连接的客户端进程随后向服务器提供一个文件名。如果客户端具有足够的权限,服务器进程就会打开文件并将其内容发送回客户端。
然后给出了如下代码:
Imports System
Imports System.IO
Imports System.IO.Pipes
Imports System.Threading
Class PipeServer
Shared numThreads As Integer = 4
Shared Sub Main()
Dim i As Integer
For i = 1 To numThreads
Dim newThread As New Thread(New ThreadStart(AddressOf ServerThread))
newThread.Start()
Next
Console.WriteLine("Press enter to exit.")
Console.ReadLine()
End Sub
Private Shared Sub ServerThread()
Dim pipeServer As 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.
Dim sr As New StreamReader(pipeServer)
Dim sw As 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 one true server!")
' Obtain the filename from the connected client.
Dim filename As String = sr.ReadLine()
2011年05月04日 13点05分
2
level 1
' Read in the contents of the file while impersonating
' the client.
Dim fileReader As New ReadFileToStream(pipeServer, filename)
' Display the name of the clientr we are impersonating.
Console.WriteLine("Reading file: {0} as user {1}.", _
filename, pipeServer.GetImpersonationUserName())
pipeServer.RunAsClient(AddressOf fileReader.Start)
pipeServer.Disconnect()
Catch ex As Exception
Console.WriteLine(ex.GetType().FullName)
Console.WriteLine(ex.StackTrace)
Console.WriteLine()
Console.WriteLine("ERROR: {0}", ex.Message)
End Try
pipeServer.Close()
End Sub
End Class
Public Class ReadFileToStream
Private m_filename As String
Private m_stream As Stream
Public Sub New(ByVal stream1 As Stream, ByVal filename As String)
m_filename = filename
m_stream = stream1
End Sub
Public Sub Start()
Dim sw As New StreamWriter(m_stream)
Dim contents As String = File.ReadAllText(m_filename)
sw.WriteLine(contents)
sw.Flush()
End Sub
End Class
2011年05月04日 13点05分
3
level 1
下面的示例演示使用 NamedPipeClientStream 类的客户端进程。 客户端连接服务器进程并向服务器发送一个文件名。服务器随后将文件内容发送回客户端。文件内容随后显示在控制台上。
然后是如下代码:
Imports System
Imports System.IO
Imports System.IO.Pipes
Imports System.Security.Principal
Class PipeClient
Shared Sub Main(ByVal args As String())
Try
Dim pipeClient As New NamedPipeClientStream("localhost", _
"testpipe", PipeDirection.InOut, PipeOptions.None, _
TokenImpersonationLevel.Impersonation)
Dim sw As New StreamWriter(pipeClient)
Dim sr As New StreamReader(pipeClient)
pipeClient.Connect()
sw.AutoFlush = True
' Verify that this is the "true server"
If sr.ReadLine()= "I am the one true server!" Then
' The client security token is sent with the first write
sw.WriteLine("C:\textfile.txt")
' Print the file to the screen.
Dim buffer(32) As Char
Dim n As Integer
Do
n = sr.Read(buffer, 0, buffer.Length)
Console.Write(buffer, 0, n)
Loop While n > 0
Else
Console.WriteLine("Server could not be verified.")
End If
pipeClient.Close()
Catch ex As Exception
Console.WriteLine("ERROR: {0}", ex.Message)
End Try
End Sub
End Class
2011年05月04日 13点05分
4