Learning Notes (3): Interprocess Communication: Mailslot: Naming Rules for Posts: // ServerName / Mailslot / [PATH] Name The first section / / server corresponds to the server's name, we want Create a postlock above and run the server program on it. The second part / m a i l s L O t is a "hardcoded" fixed string for telling the system that this file name is from M s F S. The third part / [p A t h] n a m e allows the application to define and identify a postlock name unique. Among them, "P A T H" represents the path to specify a multi-level directory. For example (note that M ailslot may not change, "also so-called" hard coded "): //oreo/testserver/mailslot/CoolDirectory/funtest/aothermailslot//mailslot/easymailslot/*/mailslot / MySLOT server string portion can represent a decimal point (.), An asterisk (*), a domain name or a real server name. Doubt: Since the postlock relies on the Windows file system service to create and transfer data online, the interface is "unrelated to the protocol". How is the Windows file system service really achieved? No connection: It is to send it to the server's packet, and does not require a client to receive a confirmation of the data. Error Reaction: All Win32 API functions (except C R E A T E F I L E and C R E A TE E M A I L S L O) The 0 value will be returned in the case where the call failed. C R E A T E F I L E and C R E A T E M A I L S L O T Returns I N VA L I D_H (Invalid Handle Value) But we can't create mailslot remotely? Why is this form of "// *"? CreateMailslot: HANDLE CreateMailslot (LPCTSTR lpName, // pointer to string for mailslot name DWORD nMaxMessageSize, // maximum message sizeDWORD lReadTimeout, // milliseconds before read time-out LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security structure); lpName: The format of the table below . NMAXMESSAGESIZE: Indicates the length of the message, if the length is small, the server is not ignored, if set to 0, indicate any length. IReadTimeout: The specific permanent wait is still not waiting. LPSecurithattributes: Security problem.
//./mailslot/name Retrieves a client handle to a local mailslot.//computername/mailslot/name Retrieves a client handle to a remote mailslot.//domainname/mailslot/name Retrieves a client handle to all mailslots with the specified name in the specified domain.//*/mailslot/name Retrieves a client handle to all mailslots with the specified name in the system's primary domain with a simple example:. client: // Module name: Client.cpp //// Purpose: // to demonstrate how to write a mailslot client application / / // compile: // cl - client client.cpp /// Command line parameters / options: // - specifies What mailslot Server to send Data
// TO
// DOS:
// c:> / client
Hongweijin // # include
#include
//
/ / Must debug in the DOS environment, the syntax structure of debugging is:, C:> / Client
Hongweijin // This is sent to the server to the server, and of course the server is open. // void main (int Argc, char * argv []) {handle mailslot; // Defines a postlower dword byteswritten; // Point to the maximum number of characters you want to send, is the third parameter of ReadFile Char ServerName [256] ; // server name (generally considered a machine name when debugging)
// accept a command line argument for the server to send // a message to if (argc <2) {printf ("usage: client
/ N ");
Return;
}
//
// sprintf (): Will ServerName = // arg [1] / mailslot / myslot
//
Sprintf (ServerName, "
% s // mailslot // myslot ", Argv [1]);
if ((Mailslot = CreateFile (ServerName, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) // // INVALID_HANDLE_VALUE necessary must be taken of the presence of OPEN_EXISTRING: (HANDLE) -1 {printf ( "CreateFile failed WITH ERROR% D / N ", getLastError ()); Return;}
IF (Writefile (Mailslot, "this is a test", 14, & byteswritten, null) == 0) {Printf ("WriteFile Faled with Error% D / N", getLastError ()); Return;} Printf ("WROTE% D Bytes / N ", byteswritten;
CloseHandle (Mailslot); // We need to close the postlock}
Server: // Module Name: Server1.cpp //// Purpose: // DemonStrates How To Write a mailslot server application / / // compile: // cl-log1 server1.cpp //// Command Line Options: / / None //
#include
#include
Void main (void) {handle mailslot; // Define a postlock char buffer [256]; // Sended message DWORD NUMBEROFBYTESREAD; / / Specified bytes
// Create the mailslot if ((Mailslot = CreateMailslot ( ".// Mailslot // Myslot", 0, MAILSLOT_WAIT_FOREVER, NULL)) == INVALID_HANDLE_VALUE) {printf ( "Failed to create a mailslot% d / n", GetLastError ( Return;}
!. // Read data from the mailslot forever // If the function succeeds, the return value is nonzero while (! ReadFile (Mailslot, buffer, 256, & NumberOfBytesRead, NULL) = 0) {buffer [NumberOfBytesRead] = 0; // Modify Printf ("% s / n", buffer) when reading successfully;}}
Other API functions ...