Call WINAPI Implementation of mail slot communication C/s example

Source: Internet
Author: User
Tags include mail printf

Just a test. Master ignores.

Service side:

Code by pnig0s1992//date:2012,3,19 #include <stdio.h> #include <Windows.h> VOID usemailslot (  
     
LPTSTR lpmailslotname);  
    int main (int argc,char **argv) {LPTSTR lpslotname = TEXT ("\\\\.\\mailslot\\first_slot");  
    Usemailslot (Lpslotname);  
return 0;  
    } VOID Usemailslot (LPTSTR lpmailslotname) {HANDLE hmailslot;  
    BOOL Bresult;  
    DWORD dwmessagesize;  
    DWORD Dwmessagecount;  
    DWORD dwhasreadbytes;  
    Hmailslot = Createmailslot (lpmailslotname,0,mailslot_wait_forever,null);  
        if (Hmailslot = = Invalid_handle_value) {printf ("\ncreate mailslot:%s failed.", Lpmailslotname);  
    Return  
    }else {printf ("\ncreate mailslot successfully.");  
    int icount = 0;  
    int index = 0;  
        while (1) {bresult = Getmailslotinfo (hmailslot,null,&dwmessagesize,&dwmessagecount,null); if (!bresult) {printf ("\ngeTmailslotinfo failed with error:%d ", GetLastError ());  
            CloseHandle (Hmailslot);  
        Return  
            } if (Dwmessagecount = = 0) {printf ("\nno.%d wait for message.", icount+1);  
            icount++;  
            Sleep (2000);  
        Continue 

while (dwmessagecount!= 0) {LPTSTR Lpmessagebuffer = (LPTSTR) HeapAlloc (getprocessheap  
            (), heap_zero_memory,dwmessagesize);  
                if (Lpmessagebuffer = = NULL) {printf ("\nheapalloc failed with error:%d", GetLastError ());  
                CloseHandle (Hmailslot);  
            Return  
            } bresult = ReadFile (hmailslot,lpmessagebuffer,dwmessagesize,&dwhasreadbytes,null);  
                if (!bresult) {printf ("\nreadfile failed with error:%d", GetLastError ());  
                HeapFree (GetProcessHeap (), 0,lpmessagebuffer); ClOsehandle (Hmailslot);  
            Return  
            printf ("\nreceive no.%d message from client.\ncontent:%s", Index+1,lpmessagebuffer);  
            index++;  
            HeapFree (GetProcessHeap (), 0,lpmessagebuffer);  
            Bresult = Getmailslotinfo (hmailslot,0,&dwmessagesize,&dwmessagecount,null);  
                if (!bresult) {printf ("\ngetmailslotinfo failed with error:%d", GetLastError ());  
            Return  
}} return; }

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.