"网络编程"学习笔记(3)

学习笔记(3):
Interprocess Communication(单向间通讯):MailSlot(邮槽):
邮槽的命名规则:
//ServerName/MailSlot/[path]name
第一部分/ / s e r v e r对应于服务器的名字,我们要在上面创建邮槽,并在在上面运行服务器程序。第二部分/ M a i l s l o t是一个"硬编码"的固定字串,用于告诉系统这个文件名从属于M S F S。而第三部分/ [ p a t h ] n a m e则允许应用程序独一无二地定义及标识一个邮槽名。其中,"p a t h"代表路径,可指定多级目录。
举个例子(注意M a i l s l o t不得变化,亦即所谓的"硬编码"):
//Oreo/Mailslot/Mymailslot
//Testserver/Mailslot/Cooldirectory/Funtest/Aothermailslot
//./Mailslot/Easymailslot
//*/Mailslot/Myslot
服务器字串部分可表示成一个小数点( .)、一个星号(*)、一个域名或者一个真正的服务
器名字。
疑问:由于邮槽要依赖Windows文件系统服务在网上来创建和传输数据,所以接口是"与协议无关"的。那windows文件系统服务又是怎么样那实现的呢?
无连接:就是到服务器的数据包发出后,不要求client端有一个收到数据的确认。
错误反应:所有Win32 API函数(C r e a t e F i l e和C r e a t e M a i l s l o t除外)在调用失败的情况下,都会返回0值。C r e a t e F i l e和C r e a t e M a i l s l o t这两个A P I却会返回I N VA L I D _ H A N D L E _ VA L U E(无效句柄值)
但是我们不能在远程创建mailslot的?"//*"这样的形式又是为什么会这样的呢?
CreateMailslot:
HANDLE CreateMailslot(
  LPCTSTR lpName,         // pointer to string for mailslot name
  DWORD nMaxMessageSize,  // maximum message size
DWORD lReadTimeout,     // milliseconds before read time-out
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
                          // pointer to security structure
);
lpName:的格式如下表。
nMaxMessageSize:指示消息的长度,若长度小了服务器不与理睬,若设为0表示任何长度。
IreadTimeout:具体的永久等待还是不等待。
lpSecurithAttributes:安全的问题。
//./mailslot/name Retrieves a client handle to a local mailslot.
//computername/mailslot/name Retrieves a client handle to a remote mailslot.
//domainname/mailslot/name Retrieves a client handle to all mailslots with the specified name in the specified domain.
//*/mailslot/name Retrieves a client handle to all mailslots with the specified name in the system's primary domain.
一个简单的例子:
client:
// Module Name: Client.cpp
//
// Purpose:
//     To demonstrate how to write a mailslot client application
//
// Compile:
//     cl -o Client Client.cpp
//
// Command Line Parameters/Options:
//     - Specifies what mailslot server to send data
//                     to
//dos:
// c:>/client
hongweijin
//
#include
#include
//
//必需在dos环境下调试,调试的语法结构为 : 如,c:>/Client
hongweijin
//这个是将hongweijing 发给服务器,当然前提是服务器是打开的。
//
void main(int argc, char *argv[])
{
 HANDLE Mailslot;   //定义一个邮槽
 DWORD BytesWritten;  //指向想要发送的最大字符数量,是ReadFile的第三个参数
 CHAR ServerName[256] ; //服务器名字(一般在调试的时候认为是机器名)

 // Accept a command line argument for the server to send
 // a message to
 if (argc < 2)
 {
  printf("Usage: client /n");
  return;
 }
 //
 //sprintf():将ServerName = // arg[1]/Mailslot/Myslot
 //
 sprintf(ServerName, "
%s//Mailslot//Myslot", argv[1]);

 if ((Mailslot = CreateFile(ServerName, GENERIC_WRITE,
  FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  NULL)) == INVALID_HANDLE_VALUE)
//必须注意OPEN_EXISTRING的存在的必要
// INVALID_HANDLE_VALUE :  (HANDLE)-1
 {
  printf("CreateFile failed with error %d/n", GetLastError());
  return;
 }

 if (WriteFile(Mailslot, "This is a test", 14, &BytesWritten,
  NULL) == 0)
 {
  printf("WriteFile failed with error %d/n", GetLastError());
  return;
 }

 printf("Wrote %d bytes/n", BytesWritten);

 CloseHandle(Mailslot); //最后我们需要关闭邮槽
}

Server:
// Module Name: Server1.cpp
//
// Purpose:
//     Demonstrates how to write a mailslot server application
//
// Compile:
//     cl -o Server1 Server1.cpp
//
// Command Line Options:
//     None
//

#include
#include

void main(void)
{
 HANDLE Mailslot;   //定义一个邮槽
 char buffer[256];    //发送过来的消息
 DWORD NumberOfBytesRead; //指定的字节数

 // Create the mailslot
 if ((Mailslot = CreateMailslot("
.//Mailslot//Myslot", 0,
  MAILSLOT_WAIT_FOREVER, NULL)) == INVALID_HANDLE_VALUE)
 {
  printf("Failed to create a mailslot %d/n", GetLastError());
  return;
 }

 // Read data from the mailslot forever!
 // If the function succeeds, the return value is nonzero.
 while(ReadFile(Mailslot, buffer, 256, &NumberOfBytesRead,
  NULL) != 0)
 {
  buffer[NumberOfBytesRead] = 0;  //当读入成功的时候对suffer缓存进行修改
  printf("%s/n", buffer);
 }
}

其他的API函数……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值