简体   繁体   English

使用Delphi写入远程计算机上的邮筒

[英]Write to mailslot on remote computer using Delphi

I have read everything I can find about mailslots and am still left with the problem of writing to a mailslot that has been opened for reading by a mailslot server on another computer. 我已经阅读了有关邮槽的所有内容,但仍然存在写入已打开以供另一台计算机上的邮槽服务器读取的邮槽的问题。

I do not have an issue with mailslots on the same computer. 我在同一台计算机上的邮筒没有问题。 For example, if I try to write to a mailslot on the local computer like this: 例如,如果我尝试像这样在本地计算机上写一个邮槽:

var
  sMsg: string;
  iBytes: DWORD;
begin
  SlotName := '\\.\mailslot\testslot';
  Handle := CreateFile(PChar(SlotName), GENERIC_WRITE, FILE_SHARE_READ, nil,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  Result := WriteFile(Handle, sMsg[1], Length(sMsg), iBytes, nil);
end;

CreateFile will fail with file does not exist error code if the mailslot server program has not created the mailslot. 如果邮筒服务器程序尚未创建邮筒,则CreateFile将失败,并显示文件不存在的错误代码。

However, if I pass something like this \\\\MYSERVER\\mailslot\\testslot as the mailslot name, it always returns a valid file handle. 但是,如果我将类似\\\\MYSERVER\\mailslot\\testslot作为\\\\MYSERVER\\mailslot\\testslot槽名称\\\\MYSERVER\\mailslot\\testslot ,它将始终返回有效的文件句柄。 Even if the computer name is not valid, it still returns a file handle and then the WriteFile also succeeds. 即使计算机名无效,它仍然会返回文件句柄,然后WriteFile也将成功。

I was hoping to have CreateFile fail if either the computername is incorrect or the mailslot server on that computer had not created the mailslot, but this does not seem to be the case, it only seems to be the case when writing to mailslot on the local computer. 如果计算机名称不正确或该计算机上的邮筒服务器未创建邮筒,我希望CreateFile失败,但这似乎并非如此,仅当在本地写入邮筒时才是这种情况电脑。

Can someone enlighten me ? 有人可以启发我吗?

The CreateFile() documentation states: CreateFile()文档指出:

Mailslots 邮筒

If CreateFile opens the client end of a mailslot, the function returns INVALID_HANDLE_VALUE if the mailslot client attempts to open a local mailslot before the mailslot server has created it with the CreateMailSlot function. 如果CreateFile打开邮筒的客户端,则如果邮筒客户端在邮筒服务器使用CreateMailSlot函数创建本地邮筒之前尝试打开本地邮筒,则该函数返回INVALID_HANDLE_VALUE。

That implies that CreateFile() is not able to validate the existence of a remote mailslot, only a local mailslot. 这意味着CreateFile()无法验证远程邮槽的存在,只能验证本地邮槽的存在。 Which is consistent with the behavior you are seeing. 这与您看到的行为一致。 Windows can easily validate if a local mailslot exists, but cannot validate if a remote mailslot exists. Windows可以轻松地验证是否存在本地邮槽,但无法验证是否存在远程邮槽。 This makes sense when you take into account that mailslots are implemented using datagrams (aka, UDP). 当您考虑使用数据报 (aka,UDP)实现邮筒时,这很有意义。 UDP has no way of knowing whether a remote destination exists or not. UDP无法知道是否存在远程目标。 All it can do is put a packet on the network and hope it reaches its destination. 它所能做的就是将一个数据包放在网络上,并希望它到达目的地。 This is stated as nuch in the Mailslot documentation : Mailslot 文档中对此进行了详细说明

One important consideration is that mailslots broadcast messages using datagrams . 一个重要的考虑因素是邮槽使用数据报广播消息 A datagram is a small packet of information that the network sends along the wire. 数据报是网络沿线发送的一小包信息。 Like a radio or television broadcast, a datagram offers no confirmation of receipt; 像广播或电视广播一样, 数据报不提供接收确认。 there is no way to guarantee that a datagram has been received. 无法保证已收到数据报。 Just as mountains, large buildings, or interfering signals might cause a radio or television signal to get lost, there are things that can prevent a datagram from reaching a particular destination. 就像山区,大型建筑物或干扰信号可能导致无线电或电视信号丢失一样,有些事情可以阻止数据报到达特定的目的地。

That is why CreateFile() and WriteFile() do not fail when a remote mailslot does not exist. 这就是为什么当不存在远程邮槽时CreateFile()WriteFile()不会失败的原因。 They simply do not know one way or the other. 他们根本不知道一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM