"网络编程"学习笔记(3)
2004-08-22 01:20
246 查看
学习笔记(3):
Interprocess Communication(单向间通讯):MailSlot(邮槽):
邮槽的命名规则:
name]//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函数……
Interprocess Communication(单向间通讯):MailSlot(邮槽):
邮槽的命名规则:
name]//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函数……
相关文章推荐
- "网络编程"学习笔记(4)
- "网络编程"学习笔记(5)
- "HOW TO: Create a Smooth Progress Bar in Visual C# .NET"的学习笔记
- "The Design and Evolution of C++" 阅读笔记
- "活在未来" VS “活在当下”(通向财富自由学习笔记六)
- HTML 学习笔记(一)<h1>、<hr />、<p>、<ol><li>、<ul><li>、<dl><dt><dd>、 、<a name="abc">、href="#abc"、mailto
- 通过例子学习正则表达式(二)--检查输入的"钱"
- <Head First Java>学习笔记--第一章:基本概念
- <C++学习笔记一>——多继承
- <<Python基础教程>>学习笔记 | 第04章 | 字典
- <Head First Java>学习笔记--第五章:编写程序
- 10 声明字符串"javascript学习笔记,String字符串相关!" ( 追加和替换这两种用法做不出)
- ASP.NET学习笔记(5)--&quot;返回上一页&quot;功能的实现
- "The Development of AppleScript"的笔记
- "The Trouble with Checked Exceptions A Conversation with Anders Hejlsberg, Part II" 阅读笔记
- "Scalable Multithreaded Programming with Thread Pools" 阅读笔记
- PHP学习笔记——PHP语言标记及指令分隔符"分号";
- 【Java学习笔记之二十九】Java中的"equals"和"=="的用法及区别
- <<高级Bash脚本编程>> 学习笔记
- C语言学习笔记——scanf("%c",&ch) 读取字符