Yesky首页| 产品报价| 行情| 手机 | 数码 | 笔记本 | 台式机 | DIY硬件 | 外设 | 网络 | 数字家庭 | 评测 | 软件 | e时代 | 游戏 | 图片 | 壁纸 | 群乐 | 社区 | 博客 | 下载
您现在的位置: 天极网 > 开发频道 > Win32位程序设计初步之网络通信
全文

Win32位程序设计初步之网络通信

2004-11-09 00:05 作者: QQ新人类编译 出处: 天极网 责任编辑:方舟

  列表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*/
}


共7页。 9 7 1 2 3 4 5 6 7 8 :
文章阅读排行
周排行
月排行
欢迎订阅天极网RSS聚合资讯:http://www.yesky.com/index.xml