我最近在看Windows 网络编程的例子。看到邮槽的时候有这样一句话
Mailslot = CreateMailslot("\\\\.\\Mailslot\\MySlot",0,MAILSLOT_WAIT_FOREVER,NULL)
这是在本地机器上运行的,但是我想将他改成两台电脑之间进行通信。Mailslot = CreateMailslot("\\S\\Mailslot\\MySlot",0,MAILSLOT_WAIT_FOREVER,NULL)在S处,IP地址也试了,电脑名称也试了,可就是不成功呢?请问这是怎么回事?Server 程序.
#include <windows.h>
#include <stdio.h>void main()
{ HANDLE Mailslot;
char buffer[256];
DWORD NumberOfBytesRead; if((Mailslot = CreateMailslot("\\\\.\\Mailslot\\MySlot",0,MAILSLOT_WAIT_FOREVER,NULL))==INVALID_HANDLE_VALUE)
{ printf("Failed to create a mailslot %d\n",GetLastError()); return;
} while(ReadFile(Mailslot,buffer,256,&NumberOfBytesRead,NULL)!=0)
{
printf("%.*s\n",NumberOfBytesRead,buffer);
}
}Client 程序
#include <windows.h>
#include <stdio.h>void main(int argc,char *argv[])
{
HANDLE Mailslot;
DWORD ByteWritten;
CHAR ServerName[256]; if(argc<2)
{
printf("Usage:Client <Server name>\n");
return;
}
sprintf(ServerName,"\\192.168.1.114\\Mailslot\\Myslot",argv[1]);
if((Mailslot = CreateFile(ServerName,GENERIC_WRITE,
FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,
NULL))==INVALID_HANDLE_VALUE)
{ printf("CreateFile failed with error %d\n",GetLastError());
return;
} if(WriteFile(Mailslot,"This is a test",14,&ByteWritten,NULL)==0)
{ printf("WriteFile failed with error %d\n",GetLastError());
return;
} printf("Wrote %d bytes\n",ByteWritten);
CloseHandle(Mailslot);
}

解决方案 »

  1.   

    命名规则:\\server\Mailslot\[path]name   
    请将上述字串分为三段来看:\\server、\Mailslot  和\[path]name   。
    第一部分\\server  对应于服务器的名字,我们要在上面创建邮槽,并在在上面运行服务器程序。
    第二部分\Mailslot 是一个“硬编码”的固定字串,用于告诉系统这个文件名从属于MS FS。
    而第三部分\[path]name则允许应用程序独一无二地定义及标识一个邮槽名。其中,“path”代表路径,可指定多级目录。   
    举个例子来说,对一个邮槽进行标识时,下面这些形式的名字都是合法的(注意Mailslot不得变化,必须原文照输,亦即所谓的“硬编码”):   
    \\Oreo\Mailslot\Mymailslot\\Testserver\Mailslot\Cooldirectory\Funtest\Anothermailslot   
    \\.\Mailslot\Easymailslot\\*\Mailslot\Myslot   
    服务器字串部分可表示成一个小数点(.)、一个星号(*)、一个域名或者一个真正的服务器名字。所谓“域”,其实就是一系列工作站和服务器的组合,它们共用一个相同的组名。
      

  2.   

    A MailSlot is a type of interprocess communication that allows communication between processes both locally and over a network. The use of MailSlots is generally simpler than named pipes or sockets, but they are more limited.MailSlots function as a server-client interface. A server can create a MailSlot, and a client can write to it. Only the server can read the mailslot. A server-client interface could consist of two processes communicating locally or across a network. MailSlots do not operate over Wide Area Networks such as the internet, though. Also, MailSlots offer no confirmation that a message has been received unless it is programmed into an application. MailSlots are generally a good choice when one process must broadcast a message to multiple processes, or if a fast and very easy solution is needed.