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

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

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

  列表4

  这个简单的程序用来创建一个命名管道服务器。该服务器将会等待并接受一个连接,然后从中接收信息。

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

// ssnprecv.cpp

// Usage: ssnprecv

#include
#include

int main()
{
 char toDisptxt[80];
 HANDLE ssnpPipe;
 DWORD NumBytesRead;

 /* Create a named pipe for receiving messages */
 ssnpPipe=CreateNamedPipe("\\\\.\\pipe\\ssnp",PIPE_ACCESS_INBOUND,
PIPE_TYPE_MESSAGE | PIPE_WAIT,1, 0, 0, 150,(LPSECURITY_ATTRIBUTES) NULL);

 /* Check and see if the named pipe was created */
 if (ssnpPipe == INVALID_HANDLE_VALUE)
 {
  cerr << "ERROR: Unable to create a named pipe. " << endl;
  return (1);
 }

 /* Allow a client to connect to the name pipe,
 terminate if unsuccessful */
 cout << "Waiting for connection... " << endl;
 if(!ConnectNamedPipe(ssnpPipe, (LPOVERLAPPED) NULL))
 {
  cerr << "ERROR: Unable to connect a named pipe "<< GetLastError() << endl;
  CloseHandle(ssnpPipe);
  return (1);
 }

 /* Repeatedly check for messages until the program
is terminated */
 while(1)
 {
  /* Read the message and check to see if read
  was successful */
  if (!ReadFile(ssnpPipe, toDisptxt,sizeof(toDisptxt),&NumBytesRead, (LPOVERLAPPED) NULL))
  {
   cerr << "ERROR: Unable to read from named pipe " << GetLastError() << endl;
   CloseHandle(ssnpPipe);
   return (1);
  }

  /* Display the Message */
  cout << toDisptxt << endl;
 } /* while */
}


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