您现在的位置是: 软件 > 开发者网络 > 程序方舟 > 系统编程 > 正文


-Win xp中的多种网络
-试验试验试验试验
-用Freehand实现位图矢量化
-网络电话面面观

Win32程序设计之网络通信
2001-05-31· ·QQ新人类编译··yesky

上一页  1 2 3 4 5 6 7  下一页


  结论

  命名管道通常用在客户/服务器系统,这时服务器使用一个多线程的方式来同时处理多个连接。

  列表1

  该程序创建一个mailslot服务器并且从中进行读取


//***************************************************************
// From the book "Win32 System Services: The Heart of Windows NT"
// by Marshall Brain
// Published by Prentice Hall
file://
// This code implements a simple mailslot server (receiver) that
// uses polling.
file://***************************************************************

// sms_recv.cpp

#include
#include

int main()
{
char toDisptxt[80];
HANDLE hSMS_Slot;
DWORD nextSize;
DWORD Msgs;
DWORD NumBytesRead;
BOOL Status;

/* Create a mailslot for receiving messages */
hSMS_Slot=CreateMailslot("\\\\.\\mailslot\\sms",
0, 0, (LPSECURITY_ATTRIBUTES) NULL);

/* Check and see if the mailslot was created */
if (hSMS_Slot == INVALID_HANDLE_VALUE)
{
cerr << "ERROR: Unable to create mailslot "
<< GetLastError() << endl;
return (1);
}

/* Repeatedly check for messages until the
program is terminated */
while(1)
{
Status=GetMailslotInfo(hSMS_Slot,
(LPDWORD) NULL, &nextSize, &Msgs,
(LPDWORD) NULL);
if (!Status)
{
cerr << "ERROR: Unable to get status. "
<< GetLastError() << endl;
CloseHandle(hSMS_Slot);
return (1);
}

/* If messages are available, then get them */
if (Msgs)
{

/* Read the message and check to see if
read was successful */
if (!ReadFile(hSMS_Slot, toDisptxt, nextSize,
&NumBytesRead, (LPOVERLAPPED) NULL))
{
cerr
<< "ERROR: Unable to read from mailslot "
<< GetLastError() << endl;
CloseHandle(hSMS_Slot);
return (1);
}

/* Display the Message */
cout << toDisptxt << endl;
}
else
/* Check for new messages twice a second */
Sleep(500);
} /* while */
}

上一页  1 2 3 4 5 6 7  下一页

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 C++BUILDER动态建立菜单及菜单事件
 Win32程序设计之系统安全
 Win32程序设计之服务
 WIN32程序设计之线程
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!