⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 管道通信.txt

📁 操作系统知识
💻 TXT
字号:
#include <windows.h>
#include <tchar.h>
#ifndef UNICODE
  #include <stdio.h>
#endif

#define BUFFERSIZE 256
const int cTimeout=INFINITE;

const TCHAR g_szpipe[]=_T("\\\\.\\pipe\\Quote");
const TCHAR g_szUnknown[]=T("Unknown request");
const TCHAR g_rgszQuotes[][350]={
  {T("We few,we happy few,we band of brothers;\n")
  _T("For he today that sheds his blood with me\n")
  _T("Shall be my brother be he ne'er so vile,\n")
  _T("This day shall gentle his condition.\n")
  _T("And gentlemen in England,now abed,\n")
  _T("Shall think themselves accursed they were not here;\n")
  _T("And hold their manhoods cheap whiles any speaks\n")
  _T("That fought with us upon Saint Chrispin's day.\n")},

  {_T("There are more things in heaven and earth,Horatio,\n")
  _T("Than are dreamt of in your philosophy.\n")},

  {_T("Now is the winter of our discontent\n")
  _T("Made glorious summer by this sun of York;\n")}
};

int ErrorHandling(LPCTSTR lpszTitle);
DWORD PipeFunc(LPARAM lparam);

int_tmain()
{

  SECURITY_ATTRIBUTES sa;
  SECURITY_DESCRIPTOR* psd;
  psd=(SECURITY_DESCRIPTOR*)LocalAlloc(LPTR,
                            SECURITY_DESCRIPTOR_MIN_LENGTH);
  InitializeSecurityDescriptor(psd,SECURITY_DESCRIPTOR_REVISION);
  SetSecurityDescriptorDacl(psd,TRUE,NULL,FALSE);
  sa.nLength=sizeof(sa);
  sa.lpSecurityDescriptor=psd;
  sa.blnheritHandle=TRUE;

  while(1)
  {
    BOOL fConnected;
    HANDLE hPipe=CreateNamedPipe(g_szPipe,
                                 PIPE_ACCESS_DUPLEX,
                                 PIPE_TYPE_MESSAGE|
                                 PIPE_READMODE_MESSAGE|
                                 PIPE_WAIT,
                                 PIPE_UNLIMITED_INSTANCES,
                                 BUFFERSIZE, 
                                 BUFFERSIZE,
                                 cTimeout,
                                 &sa);
    if(hPipe==INVALID_HANDLE_VALUE)
      return ErrorHandling(_T("CreatePipe Failed"));

    _tprintf(_T("Main thread waiting for a cliedt\n"));
    fConnected=ConnectNamedPipe(hPipe,NULL);
    if(fConnected||GetLastError()==ERROR_PIPE_CONNECTED)
    {

      HANDLE hThread;
      DWORD dwThreadlD;
      hThread=CreateThread(NULL,
                           0,
                           (LPTHREAD_START_ROUTINE)PipeFunc,
                           (LPVOID)hPipe,
                           0,
                           &dwThradlD);
      _tprintf(_T("Thread 0x%02X connected to a client\n"),
               dwThreadlD);
      if(hThread==INVALID_HANDLE_VALUE)
        ErrorHandling(_T("CreateThread Failed"));
    }
    else
    {
      CloseHandle(hPipe);
        return ErrorHandling(_T("Connect Failed"));
    }
  }
  LocalFree(psd);
  return 0;
}

DWORD PipeFunc(LPARAM Iparam)
{
  HANDLE hPipe=(HANDLE)Iparam;
  static int ndx=0;
  TCHAR szBuffer[BUFFERSIZE];
  DWORD dwRead,dwWritten;
  Bool fWrite;
  while(1)
  {
    LPCTSTR pszWrite;
    DWORD cbWrite;
    BOOL fRead=ReadFile(hPipe,
                        szBuffer,
                        BUFFERSIZE,
                        &dwRead,
                        NULL);
    if(fRead==FALSE||dwRead==0)break;
    if(_tcsicmp(_T("Quit"),szBuffer)==0)
    {
      DWORD dwThreadLD=GetCurrentThreadld();
      _tprintf(_T("Thread 0x%02X closing connection\n"),
        dwThreadLD);
      break;
    }
    else if(_tcsicmp(_T("Quote"),szBuffer)==0)
    {
      pszWrite=g_rgszQuotes[ndx];
      if(++ndx==3)
      ndx=0;
    }
    else
      pszWrite=g_szUnknown;

    cbWrite=(lstrlen(pszWrite)*sizeof(TCHAR))+sizeof(TCHAR);
    fWrite=WriteFile(hPipe,
                     pszWrite,
                     cbWrite,
                     &dwWritten,
                     NULL);
    if(fWrite==FALSE||dwWritten==0)break;
  }
  FlushFileBuffers(hPipe);
  DisconnectNamedPipe(hPipe);
  CloseHandle(hPipe);

  return NO_ERROR;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -