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


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

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

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


  列表2

  该程序每隔5秒写一次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 sender.
file://***************************************************************

// sms_send.c

// Usage: sms_send

#include
#include
#include

int main()
{
char toSendTxt[100], buffer[100];
DWORD bufferLen=100;
HANDLE hSMS_Slot;
BOOL Status;
DWORD NumBytesWritten;

/* Create the mailslot file handle for
sending messages */
hSMS_Slot=CreateFile("\\\\*\\mailslot\\sms",
GENERIC_WRITE, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);

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

/* form string to send */
GetComputerName(buffer, &bufferLen);
strcpy(toSendTxt, "Test string from ");
strcat(toSendTxt, buffer);

/* Repeatedly send message until program
is terminated */
while(1)
{
cout << "Sending..." << endl;
/* Write message to mailslot */
Status=WriteFile(hSMS_Slot,
toSendTxt, (DWORD) strlen(toSendTxt)+1,
&NumBytesWritten, (LPOVERLAPPED) NULL);

/* If error occurs when writing to mailslot,
terminate program */
if (!Status)
{
cerr << "ERROR: Unable to write to mailslot "
<< GetLastError() << endl;
CloseHandle(hSMS_Slot);
return (1);
}

/* Wait sending the message again */
Sleep(4800);
} /* while*/
}


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

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