我使用命名管道让在不同主机上运行的两个进程进行通信。 这是服务器代码:
ApplicationWindow {
title: qsTr("Testing")
width: 640
height: 480
visible: true
ColumnLayout {
anchors.centerIn: parent
Rectangle {
width: 150
height: 25
color: "#e67e22"
border.width: 1
border.color: "black"
Text {
anchors.centerIn: parent
text: "Mark Special Item"
}
MouseArea {
anchors.fill: parent
onClicked: {
var item = myCppModel.specialItem()
console.debug("how to color the special item: " + item)
}
}
}
ListView {
id: itemList
width: 200
height: 25 * count
model: myCppModel.itemList
delegate: Item {
width: parent.width
height: 25
Rectangle {
width: parent.width
height: 20
color: "#34495e"
border.width: 1
border.color: "black"
Text {
x: 10
anchors.verticalCenter: parent.verticalCenter
text: modelData.data
color: "white"
}
}
}
}
}
}
...这是客户端代码:
public class PipeServer
{
public static void Main()
{
PipeSecurity ps = new PipeSecurity();
ps.AddAccessRule(new PipeAccessRule("Everyone",
PipeAccessRights.FullControl,
System.Security.AccessControl.AccessControlType.Allow));
NamedPipeServerStream pipeServer = new NamedPipeServerStream(
"testpipe", PipeDirection.InOut, 4,
PipeTransmissionMode.Message, PipeOptions.WriteThrough,
1024, 1024, ps);
StreamReader sr = new StreamReader(pipeServer);
StreamWriter sw = new StreamWriter(pipeServer);
do {
try {
pipeServer.WaitForConnection();
sw.WriteLine("Waiting");
sw.Flush();
pipeServer.WaitForPipeDrain();
string message = sr.ReadLine();
sw.WriteLine("got message " + message);
} catch (Exception ex) { throw ex; } finally {
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected) { pipeServer.Disconnect(); }
}
} while (true);
}
}
在远程主机上启动public class PipeClient
{
static void Main(string[] args) {
NamedPipeClientStream pipeClient = new NamedPipeClientStream(
"10.225.154.59", "testpipe",
PipeDirection.InOut, PipeOptions.None,
TokenImpersonationLevel.None);
if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
StreamReader sr = new StreamReader(pipeClient);
StreamWriter sw = new StreamWriter(pipeClient);
string temp;
temp = sr.ReadLine();
if (temp == "Waiting") {
try {
sw.WriteLine("Hello");
sw.Flush();
pipeClient.Close();
} catch (Exception ex) { throw ex; }
}
}
}
后,我在工作站上运行客户端...但我总是收到以下错误消息:
PipeServer
在远程主机上运行Unhandled Exception: System.IO.IOException: Logon failure: unknown user name or bad password.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Pipes.NamedPipeClientStream.Connect(Int32 timeout)
at System.IO.Pipes.NamedPipeClientStream.Connect()
PipeClient.Program.Main(String[] args) in G:\My Projects\Lab\NamePipes\Program.cs:line 18
的用户与在我的工作站上运行PipeServer
的用户不同...但这应该不是问题,因为我添加了正确的访问规则。
我错过了什么吗?
修改 阿列克谢建议的一些额外信息。运行服务器的用户是本地管理员,而运行客户端的用户是域用户。有没有办法让任何人向服务器发送消息?