Network Programming Learning Notes (4)

xiaoxiao2021-03-06  101

"Network Program" learning (4): Named pipe named pipe play its own role on data security accesses.

UNC: Universal Naming Convertion (Universal Name Rules). LPCTSTR type

Communication mode: (1) byte mode, the message is in the form of a continuous byte stream, flowing between the client and the server. Disadvantages, in a period of time, we will not know how many bytes are circulated from the pipeline. (2) Message mode, a complete data segment, ready to write.

To build a naming pipe or server application with Microsoft Visual C , you must join WinBase.h file in your own program file. And it is necessary to establish a connection with kernel32.lib. Use the Visual C linker to configure the kernel.lib link library.

Error Return: All Win32 API functions (except CreateFile and CreateNamedPipe) will return 0 values ​​under the premise of call failure. CreateFile and CreateNamedPipe return to INVALID_HANDL E_VALUE.

The exact meaning of the handle: the handle, is actually an integer. It identifies a resource, such as windows, bitmaps, and more. Just like finding someone, you must know the address of him (she), if you want to operate a resource, you must first get the handle. The handle is an identifier, which is used to identify objects or projects. It is like our names. Everyone will have one, and the names of different people are different, but there may be a name with you. From the data type, it is just a 16-bit unsigned integer. Applications almost always get a handle by calling a Windows function, then other Windows functions can use the handle to reference the corresponding object. A large number of handles can be used in Windows programming, such as Hinstance (EQ), HBitmap, HDC (Device Description Table Handle), Hicon (Icon Handle), etc., this is also a universal handle It is Handle, such as the following statement: Hinstance Hinstance; can be changed to: Handle Hinstance; the second sentence above is right.

Implementation functions of the pipe: 1) Use the A P i function C R e a t e n a m e d P i p E to create a naming pipe instance handle. 2) Use the A P i function C O N E C T n a m e d P I P E to listen to the client connection request on the named pipe instance. 3) Two A P i functions of R e a d f i l e and WR i t e f i L e, receive data from the client, or send data to the client. 4) Use the A P i function D i s c o N e c t n m e d p i p E to turn off the named pipe connection. 5) Use the A P i function C L O S e h a n d L E to close the named pipe instance handle.

The following is an example and parsing: // module name: client.cpp /// Purpose: ////// Purpose: ////// Purpose: ////// Purpose: ////// Purpose: //// / i a a a a t c c n n d Client Application. When This Application SuccessFully // Connects To a named Pipe, The Message "this is a test" IS // Written to the server.//////////////// 1. Wait for a named pipe instance to become available using // the waitnamedpipe () API function.// 2. Connect to the name pipe using the createfile () API // Function.//3. Send Data To or Receive Data From The Server Using /// The Writefile () And Readfile () API Functions.// 4. Close The named Pipe session Using the closehandle () API // Functions./// // compile: // cl-channel Client.cpp /// COMMAND line Options: // none // # include #include

#define pipe_name "./pipe//jim"

Void main (void) {

Handle PipeHandle; DWORD BYTESWRITEN

IF (WaitNamedPipe (PIPE_NAME, NMPWAIT_WAIT_FOREVER) == 0) {Printf ("WaitNamedPipe Faip with Error% D / N", getLastError (); Return;}

// Create the named pipe file handle if ((PipeHandle = CreateFile (PIPE_NAME, GENERIC_READ | GENERIC_WRITE, 0, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL)) == INVALID_HANDLE_VALUE) {printf ( "CreateFile failed with error % D / N ", getLastError ()); Return;}

IF (WriteFile (PipeHandle, "This Is A Test", 14, & Byteswritten, NULL) == 0) {Printf ("Writefile Faled with Error% D / N", getLastError ()); CloseHandle (PipeHandle); Return;}

Printf ("Wrote% D Bytes", Byteswritten; CloseHandle (PipeHandle);

// Module Name: Server.cpp //// Purpose: //// This program is a simple named pipe server that demonstrates // the API calls needed to successfully develop a basic named // pipe server application When this application receives. A // Client Connection, IT Reads the data from the pipe and // reports the receific screen/// You need FIPE STEPS to WRITE A Named Pipe Server: //// 1. Create a named Pipe Instance Handle Using the // CreateNamedPipe () API function.// 2. Listen for a client connection on a pipe instance using // the ConnectNamedPipe () API function.// 3. Receive from and send data to the client using the // ReadFile ( ) and WriteFile () API functions.// 4. Close down the named pipe connection using the // DisconnectNamedPipe () API function.// 5. Close the named pipe instance handle using the // CloseHandle () API function.// // compile: // cl-log server.cpp /// Command line Options: // none //

#include #include

Void main (void) {handle pipehandle; dword bytesread; char buffer [256];

if ((PipeHandle = CreateNamedPipe ( ".// Pipe // Jim", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 0, 0, 1000, NULL)) == INVALID_HANDLE_VALUE) {printf ( "CreateNamedPipe failed with error% d / n ", GetLastError ()); Return;}

Printf ("Server is now Running / N);

IF (connectnamedpipe == 0) {Printf ("ConnectNameDpipe Failed with Error% D / N", getLastError ()); CloseHandle (PipeHandle); Return;}

IF (readfile (PipeHandle, Bytesread, NULL) <= 0) {Printf ("ReadFile Faled with Error D / N", getLastError ()); CloseHandle (PipeHandle); Return;} Printf "%. * s / n", bytesread, buffer;

IF (DisconnectNameDpipe (PipeHandle) == 0) {Printf ("DisconnectNamedPipe Failed with Error% D / N", getLastError ()); Return;}

CloseHandle (PipeHandle);

These two programs don't have much. It is very simple.

转载请注明原文地址:https://www.9cbs.com/read-124237.html

New Post(0)