VC + + Multithreading-inter-process communication

Source: Internet
Author: User

1. Postal Slots

The mail slot is a one-way communication mechanism provided by Windows system, which can transmit data very small, usually around 400k.

Create a mail slot

// Specifies the name of the message slot, such as // ./mailslot/[path]name, the decimal point represents a server-based machine (cannot create a mail slot for a remote computer). DWORD nmaxmessagesize,// the maximum message length (in bytes) that can be written to the message slot, the client message is larger than the value the server does not accept the message, or 0 to receive any length of message. DWORD lreadtiemout,// wait mode and no wait mode, Mailslot_wait_forever waits indefinitely, 0 returns immediately, other values in milliseconds. lpsecurity_attributes lpsecurityattributes// access control permissions, generally set to null);

Instance

Service side

1#include <Windows.h>2#include <stdio.h>3 intMain ()4 {5HANDLE Mail;//define a mail slot handle6Mail=createmailslot ("\\\\.\\mailslot\\newslot",0, mailslot_wait_forever,null);//Create a mail slot7     if(Mail==invalid_handle_value)//determine the handle of a mail slot8     {9printf"failed to create mail slot! \ r \ n");TenCloseHandle (mail);//Close Handle One     } A     Else -     { -printf"mail slot created successfully! \ r \ n"); the         Chartext[ $]; -DWORD ReadText;//get the actual read value -         if(ReadFile (Mail,text, $, &readtext,null))//reading Data -         { + printf (text); -         } +         Else A         { atprintf"\ r \ n Read data failure!\r\n"); -         } -CloseHandle (mail);//Close Handle -Sleep ( +); -     } -  in     return 0; -}

Client

1#include <Windows.h>2#include <stdio.h>3 intMain ()4 {5HANDLE Mail2;//Mail slot handle6     Chartext[]="I'm going to send a message.";7 DWORD writetext;8Mail2=createfile ("\\\\.\\mailslot\\newslot", generic_write,file_share_read,null,open_existing,file_attribute_normal,null);//Open File/create file9     if(INVALID_HANDLE_VALUE==MAIL2)//deciding whether to open successfullyTen     { Oneprintf"Mail Slot open failed! \ r \ n"); A  -     } -     Else the     { -         if(WriteFile (Mail2,text,sizeof(text), &writetext,null))//Write Data -         { -Sleep ( -); +printf"Data write success \ r \ n"); -         } +         Else A         { atSleep ( -); -printf"Mail Slot Write failed! \ r \ n"); -         } -CloseHandle (MAIL2);//Close Mail Slots -     } -Sleep (10000); in     return 0; -}

VC + + Multithreading-inter-process communication

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.