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

Win32程序设计之网络通信

2001-05-28 12:04 作者: QQ新人类编译 出处: yesky 责任编辑:方舟

  列表5

  一个命名管道客户,可连接到列表4中的程序,并且发送信息给它

//***************************************************************
// From the book "Win32 System Services: The Heart of Windows NT"
// by Marshall Brain
// Published by Prentice Hall
file://
// Copyright 1994, by Prentice Hall.
file://
// This code implements a simple named pipe sender.
file://***************************************************************

// ssnpsend.cpp

// Usage: ssnpsend

#include
#include

int main()
{
char *toSendtxt="Test String";
HANDLE ssnpPipe;
DWORD NumBytesWritten;
char machineName[80];
char pipeName[80];

cout << "Enter name of server machine: ";
cin >> machineName;
wsprintf(pipeName, "\\\\%s\\pipe\\ssnp",
machineName);

/* Create the named pipe file handle for sending
messages */
ssnpPipe=CreateFile(pipeName,
GENERIC_WRITE, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);

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

/* Repeatedly send message until program is
terminated */
while(1)
{
cout << "Sending..." << endl;
/* Write message to the pipe */
if (!WriteFile(ssnpPipe,
toSendtxt, (DWORD) strlen(toSendtxt)+1,
&NumBytesWritten, (LPOVERLAPPED) NULL))
{
/* If error occurs when writing to named
pipe, terminate program */
cerr << "ERROR: Unable to write to named pipe "
<< GetLastError() << endl;
CloseHandle(ssnpPipe);
return (1);
}

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




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