Windows Named Pipes

Source: Internet
Author: User
Tags readfile

A named pipe is a network that completes inter-process communication, shielding the underlying network protocol details.

When a named pipe is used as a network programming scheme, it actually establishes a C/s communication system and transmits the data reliably. The difference between a named pipe server and a client is that the server is the only process that has permission to create a named pipe, and only that it can accept a connection request from a pipe client. The client can only establish a connection with a ready-made named pipe server. Named pipes provide two basic communication modes, byte mode, and message mode. In byte mode, the data flows between the client and the server in the form of a contiguous stream of bytes. In the message mode, the client and server are sent and received through a series of discontinuous data units, each time a message is issued on the pipeline, it must be read as a complete message.

#include "stdafx.h" #include <windows.h> #include <stdio.h>int _tmain (int argc, _tchar* argv[]) {//Accept all security descriptions ( That is, to minimize the connection permissions of the pipe). Security_attributes sa; Security_descriptor SD; if (InitializeSecurityDescriptor (&AMP;SD, security_descriptor_revision)) {//Add a NULL disc. ACL to the security descriptor.  if (SetSecurityDescriptorDacl (&AMP;SD, TRUE, (PACL) NULL, FALSE)) {sa.nlength = sizeof (SA);  Sa.lpsecuritydescriptor =&sd;  Sa.binherithandle = TRUE; Create a named pipe in Windows \ stands for Zhuan ' Yi two \ \ represents a \ HANDLE hnamedpipe = Createnamedpipea ("\\\\.\\pipe\\testname", Pipe_access_ DUPLEX |    File_flag_overlapped, Pipe_type_byte, 1, 1024x768, 1024,0, &AMP;SA);    Check whether to create successful if (Hnamedpipe = = Invalid_handle_value) {printf ("Create Named pipe failed!\n");    } else Window {printf ("Create Named pipe success!\n");    }//Asynchronous IO Structure OVERLAPPED op;    ZeroMemory (&op, sizeof (OVERLAPPED));    Create an event kernel object op.hevent = CreateEvent (null, TRUE, FALSE, NULL); Waiting for a client to connect    BOOL B = Connectnamedpipe (Hnamedpipe, &op);     When a client is connected, the event becomes signaled state if (WaitForSingleObject (op.hevent, INFINITE) = = 0) {printf ("Client connect success!\n");    } else {printf ("Client connect failed!\n");    }//After the connection is successful, communicate, read and write Char buff[100];    sprintf_s (Buff, +, "test message from server!");    DWORD Cbwrite;    WriteFile (hnamedpipe, Buff, strlen (buff), &cbwrite, NULL);    ZeroMemory (buff, 100);    ReadFile (hnamedpipe, Buff, &cbwrite, NULL);    After the communication is complete, disconnect disconnectnamedpipe (hnamedpipe);   Close the pipe CloseHandle (hnamedpipe); }} system ("Pause"); return 0;}

  

#include "stdafx.h" #include <windows.h>  #include <stdio.h>  int _tmain (int argc, _tchar* argv[]) {/ /check if the named pipe exists      BOOL B = Waitnamedpipea ("\\\\.\\pipe\\testname", nmpwait_wait_forever);      Open pipe      HANDLE hfile = Createfilea ("\\\\.\\pipe\\testname",          generic_read | Generic_write,          0, NULL, open_existing, file_attribute_normal, null);      Check if the connection is successful      if (!b | | hfile = = INVALID_HANDLE_VALUE)      {          printf ("Connect failed!\n");      }      else      {          printf ("Connect success!\n");      }      For communication      Char  buf[100];      ZeroMemory (buf,);      DWORD Dwread;      ReadFile (hfile, buf, &dwread, NULL);      printf (BUF);      WriteFile (hfile, "test message for client!", strlen ("test message for client!"), &dwread, NULL);      Close the pipe      CloseHandle (hfile);      System ("pause"); return 0;}

  

Windows Named Pipes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.