调用WinApi实现邮槽通信C/S实例

Just a test.高手无视.

服务端:

//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;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索printf
, return
, 槽函数
, heapfree
GetLastError()
邮槽通信、winapi directx实例、windows 邮槽、c 串口通信实例、java调用api接口实例,以便于您获取更多的相关知识。

时间: 2024-09-11 06:22:53

调用WinApi实现邮槽通信C/S实例的相关文章

进程通信系列-单向邮槽

      单向邮槽可以用于局域网的不可靠单向广播,代码长度一般,功能一般        缺点:不可靠,单向        优点:简单,不必关心协议 单向邮槽类: #include "stdafx.h" #include "mailslot.h" mailslot::mailslot(void) { hmail=NULL; } mailslot::~mailslot(void) { if(hmail) CloseHandle(hmail); } int mailsl

qt-Qt 怎么在自定义函数中调用MainWindow中的槽函数

问题描述 Qt 怎么在自定义函数中调用MainWindow中的槽函数 自定义的函数,没有继承MainWindow,写了emit 信号一直报错. 怎么在自定义函数中调用MainWindow中的槽函数. 解决方案 在线等.......急 解决方案二: 你自定义的信号已经和MainWindow中的槽函数connect了么

使用VB.net调用WinAPI方法介绍

本贴精华段:    Declare Auto Function MBox Lib "user32.dll" _       Alias "MessageBox" (ByVal hWnd As Integer, _       ByVal txt As String, ByVal caption As String, _       ByVal Typ As Integer) As Integer    '定义一些要调用参数    Const MB_ICONQUEST

java中利用反射调用另一类的private方法的简单实例_java

我们知道,Java应用程序不能访问持久化类的private方法,但Hibernate没有这个限制,它能够访问各种级别的方法,如private, default, protected, public. Hibernate是如何实现该功能的呢?答案是利用JAVA的反射机制,如下:  import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class ReflectDemo

调用WinApi查询内存信息

Pnig0s1992: 列出当前系统信息,当前系统内存状况. 枚举进程,列出每个进程所有内存块的页面类型,页面属性,保护属性等信息 //Code by Pnig0s1992 //Date:2012,3,15 #include <stdio.h> #include <Windows.h> #include <TlHelp32.h> VOID getMemoryInfo(SYSTEM_INFO si,HANDLE hProcess); int main(void) { SY

Delphi调用WinAPI: QueryPerformanceCounter

//声明:QueryPerformanceCounter( var lpPerformanceCount: TLargeInteger {获取定时器每秒的频率数; TLargeInteger = Int64} ): BOOL; {返回 False 表示调用失败, 或者是硬件不支持高性能定时器} // 一个有趣的示例: 判断你的鼠标点击速度; 我的最快记录是 151 毫米 var n1,n2,n,f: Int64; b: Boolean; procedure TForm1.FormMouseDow

Delphi调用WinAPI: MessageBeep

//声明: MessageBeep( uType: UINT {参数是个常数; 根据不同的常数发出不同的声音, 也就是调用了不同的 wav} ): BOOL; //参数 uType 可选值: MB_OK = 0; MB_ICONHAND = 16; MB_ICONQUESTION = 32; MB_ICONEXCLAMATION = 48; MB_ICONASTERISK = 64; //举例, 下面代码会发出错误警告 begin MessageBeep(16); end; //另外 Delph

调用WinApi实现超大文件分割

不多说了,10G+的Sql测试分割成功,每块大小不要超过1G就好了. //Code by Pnig0s1992 //Date:2012,3,18 #include <stdio.h> #include <Windows.h> #define MAXLENGTH 30 int main(int argc,char * argv[]) { LPTSTR lpFileName = "xxx.sql"; HANDLE hFile; struct FILEINFO { L

Delphi调用WinAPI 字符及字符串函数(15): CharNext、CharPrev

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Se