本人要做一个服务器端一个客户端,由于要求信息传递的适时性所以两个程序要通过内存映射传递消息,要求服务器向内存中写文件,客户端只要发现内存中有内容或内容改变就进行读取,不许延迟(用进程实现,不要定时器),最好有源码,有实例就更好了.

解决方案 »

  1.   

    Creating Named Shared Memory
    To share data, multiple processes can use memory-mapped files that are backed by the system paging file.First ProcessThe first process creates the file mapping object by calling the CreateFileMapping function with INVALID_HANDLE_VALUE and a name for the object. By using the PAGE_READWRITE flag, the process has read/write permission to the memory through any file views that are created. Then the process uses the file mapping object handle that CreateFileMapping returns in a call to MapViewOfFile to create a view of the file in the process address space. The MapViewOfFile function returns a pointer to the file view.When the process does not need access to the file mapping object, it should call the CloseHandle function. When all handles are closed, the system can free the section of the paging file that the object uses.
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>#define BUF_SIZE 256
    TCHAR szName[]=TEXT("MyFileMappingObject");
    TCHAR szMsg[]=TEXT("Message from first process");void main()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;   hMapFile = CreateFileMapping(
                     INVALID_HANDLE_VALUE,    // use paging file
                     NULL,                    // default security 
                     PAGE_READWRITE,          // read/write access
                     0,                       // max. object size 
                     BUF_SIZE,                // buffer size  
                     szName);                 // name of mapping object
     
       if (hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE) 
       { 
          printf("Could not create file mapping object (%d).\n", 
                 GetLastError());
          return;
       }
       pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object
                            FILE_MAP_ALL_ACCESS, // read/write permission
                            0,                   
                            0,                   
                            BUF_SIZE);           
     
       if (pBuf == NULL) 
       { 
          printf("Could not map view of file (%d).\n", 
                 GetLastError()); 
          return;
       }   
       CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg));
       getch();   UnmapViewOfFile(pBuf);   CloseHandle(hMapFile);
    }
    Second ProcessA second process can access the same data by calling the OpenFileMapping function with the same name as the first process. Then it can use the MapViewOfFile function to obtain a pointer to the file view. 
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>#define BUF_SIZE 256
    TCHAR szName[]=TEXT("MyFileMappingObject");void main()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;   hMapFile = OpenFileMapping(
                       FILE_MAP_ALL_ACCESS,   // read/write access
                       FALSE,                 // do not inherit the name
                       szName);               // name of mapping object 
     
       if (hMapFile == NULL) 
       { 
          printf("Could not open file mapping object (%d).\n", 
                 GetLastError());
          return;
       } 
     
       pBuf = MapViewOfFile(hMapFile,    // handle to mapping object
                   FILE_MAP_ALL_ACCESS,  // read/write permission
                   0,                    
                   0,                    
                   BUF_SIZE);                   
     
       if (pBuf == NULL) 
       { 
          printf("Could not map view of file (%d).\n", 
                 GetLastError()); 
          return;
       }   MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);   UnmapViewOfFile(pBuf);   CloseHandle(hMapFile);
    }
      

  2.   

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/creating_named_shared_memory.asp
      

  3.   

    用pipe或者mailslot,memory-mapping file都可以。看你传递的信息量了。
    至于后一个要求(只要内存中一有数据就进行某种操作),可以额外创建一个event,服务器把信息写进去之后,用event通知客户端,客户端可以一直阻塞等待此event。
      

  4.   

    再请问我用空的for循环能不能实现"只要内存中一有数据就进行某种操作",并且没有延迟
      

  5.   

    另外我觉得DentistryDoctor建议使用内存映射文件的方法可行
    只是我没有看懂。
    最近正在学习这方面的东西。
    俺师傅让的!!
      

  6.   

    顺便学习一下: 
       我从没看过书,所以自己用的一些方法也不知道名字,那么请问我所说的空的for循环就是阻塞等待发放吗?
      

  7.   

    阻塞等待和额外创建一个event哪种方法更能适时一些?
      

  8.   

    请问谁能说说设置阻塞的具体思路,我是这样想的,开个线程,在里面做个for(;;)有内容就处理,是不是你们所说的阻塞等待呢?
      

  9.   

    请问谁能说说设置阻塞的具体思路,我是这样想的,开个线程,在里面做个for(;;)有内容就处理,是不是你们所说的阻塞等待呢?