如何使用内存映射文件在同一台计算机上的服务器和客户端之间进行通信?

如何使用内存映射文件在同一台计算机上的服务器和客户端之间进行通信?,第1张

概述如何使用内存映射文件在同一台计算机上的服务器和客户端之间进行通信?

我真的希望有人能帮我理解内存映射文件是如何工作的。 我在网上做了大量的研究,但是有时候要求帮助就容易多了。

我正在运行的服务器上创build文档创build系统。 我想创build一个服务,将模板文件按需提供给客户端程序(这本身就是对请求文件的实际客户端的服务)。

我希望模板文件留在内存中,以便服务器在每次用户请求文件时都不必访问硬盘驱动器。

我不能使用pipe道连接,因为有问题的文件太大。 我可以使用套接字式连接,或将文件分解成更小的部分,但这似乎不如内存映射文件,这似乎是为了解决这个确切的问题,而且似乎是最快的方法在两个进程之间共享大文件。

哪些软件库可以通过编程方式创buildphotomosaics?

有没有办法检测到“几乎”远程桌面断开连接(即短暂的高延迟时间)?

使用安装程序更新windows .net程序

简单的方法来获得在VB.NETregistry项的所有权?

file.copy以及作为stream打开的文件,并写入networking文件挂起到本地机器

直到我将文件名更改为@“Global ”+ filename(请参阅代码),我无法获得内存映射文件的工作。 这工作了一段时间,但后来停止工作 – 但我不知道我改变了什么。 我在这里得到了这个想法。 我认为无论“全球”预先做的事情是否与我的问题的解决scheme有关,因为这似乎表明windows内存被分割成与文件系统格式化类似的方式,而不同的进程可能具有不同的访问级别。

以下是该服务的代码:

public partial class TemplatedistributorService : ServiceBase { protected ServiceHost host; public static Dictionary<string,System.IO.MemoryMappedfiles.MemoryMappedfile> memfiles; public static Dictionary<string,int> fileSizes; public TemplatedistributorService() { InitializeComponent(); } protected overrIDe voID OnStart(string[] args) { memfiles = new Dictionary<string,System.IO.MemoryMappedfiles.MemoryMappedfile>(); fileSizes = new Dictionary<string,int>(); foreach (var path in args) { string filename = @"Global" + System.IO.Path.GetfilenameWithoutExtension(path); var sz = new System.IO.fileInfo(path).Length; //all files must be less than 2GB for easIEr programming. I don't anticipate //this being a problem,and if it does become one,we can up this to the max system file size (4 GB) if (sz <= system.int32.MaxValue) { using (System.IO.MemoryMappedfiles.MemoryMappedfile mmf = System.IO.MemoryMappedfiles.MemoryMappedfile.CreateNew(filename,sz,System.IO.MemoryMappedfiles.MemoryMappedfileAccess.ReaDWrite)) { fileSizes[filename] = System.Convert.ToInt32(sz); using (var stream = mmf.CreateVIEwStream()) { using (var writer = new System.IO.BinaryWriter(stream)) { byte[] fileBytes = System.IO.file.ReadAllBytes(path); writer.Write(fileBytes); } } memfiles[filename] = mmf; } } } host = new ServiceHost(typeof(Templatedistributor)); host.AddServiceEndpoint(typeof(TemplatedistributorInterface),new NetnamedPipeBinding(),"net.pipe://localhost/PipedTemplateAccess"); host.open(); } protected overrIDe voID OnStop() { host.Close(); } } [ServiceContract] public interface TemplatedistributorInterface { [OperationContract] int Addfile(string path,string filename); [OperationContract] int fileSize(string filename); } public class Templatedistributor : TemplatedistributorInterface { public int Addfile(string path,string filename) { //prepending the filename with 'Global' is really important. Not sure why,but you can't access the memory mapped file without it //https://stackoverflow.com/questions/11301978/memory-mapped-file-and-win-service-cannot-find-file-created-by-server //string filename = @"Global" + System.IO.Path.GetfilenameWithoutExtension(path); if (TemplatedistributorService.memfiles.ContainsKey(filename)) { if (TemplatedistributorService.memfiles[filename] != null) { return 0; } else { return -1; } } var sz = new System.IO.fileInfo(path).Length; if (sz > system.int32.MaxValue) { return -1; } using (System.IO.MemoryMappedfiles.MemoryMappedfile mmf = System.IO.MemoryMappedfiles.MemoryMappedfile.CreateNew(filename,System.IO.MemoryMappedfiles.MemoryMappedfileAccess.ReaDWrite)) { TemplatedistributorService.fileSizes[filename] = System.Convert.ToInt32(sz); using (var stream = mmf.CreateVIEwStream()) { using (var writer = new System.IO.BinaryWriter(stream)) { byte[] fileBytes = System.IO.file.ReadAllBytes(path); writer.Write(fileBytes); } } TemplatedistributorService.memfiles[filename] = mmf; } return 1; } public int fileSize(string filename) { if (TemplatedistributorService.fileSizes.ContainsKey(filename)) { return TemplatedistributorService.fileSizes[filename]; } else { return 0; } } }

这里是客户端代码(当我尝试打开内存映射文件时抛出:“在System.Core.dll中发生types'System.IO.fileNotFoundException'的未处理的exception):

[ServiceContract] public interface TemplatedistributorInterface { [OperationContract] int Addfile(string path,string filename); [OperationContract] int fileSize(string filename); } class Program { static voID Main(string[] args) { TestDocService(); } static voID TestDocService() { var path = @"C:Usersjack.geigerdocumentsBuschFTP.docx"; ChannelFactory<TemplatedistributorInterface> streamPipeFactory = new ChannelFactory<TemplatedistributorInterface>( new NetnamedPipeBinding(),new EndpointAddress( "net.pipe://localhost/PipedTemplateAccess")); TemplatedistributorInterface streamPipeProxy = streamPipeFactory.CreateChannel(); string filename = @"GlobalRestricted" + System.IO.Path.GetfilenameWithoutExtension(path); var f = streamPipeProxy.Addfile(path,filename); f = streamPipeProxy.Addfile(path,filename); //prepending the filename with 'Global' is really important. Not sure why,but you can't access the memory mapped file without it //https://stackoverflow.com/questions/11301978/memory-mapped-file-and-win-service-cannot-find-file-created-by-server //mmf is being created in service,but I can't find it here for some reason using (System.IO.MemoryMappedfiles.MemoryMappedfile mmf = System.IO.MemoryMappedfiles.MemoryMappedfile.OpenExisting( filename,System.IO.MemoryMappedfiles.MemoryMappedfileRights.Read,System.IO.Handleinheritability.None)) { using (var stream = mmf.CreateVIEwStream(0,System.IO.MemoryMappedfiles.MemoryMappedfileAccess.Read)) { using (System.IO.BinaryReader binReader = new System.IO.BinaryReader(stream)) { var fileSize = streamPipeProxy.fileSize(filename); var bts = binReader.ReadBytes(streamPipeProxy.fileSize(filename)); using (System.IO.MemoryStream memStr = new System.IO.MemoryStream()) { memStr.Read(bts,bts.Length); memStr.Seek(0,System.IO.SeekOrigin.Begin); using (Wordprocessingdocument template = Wordprocessingdocument.Create(memStr,WordprocessingdocumentType.document)) { template.MaindocumentPart.document.RemoveAllChildren(); } } } } } }

Path.Combine绝对与相对pathstring

自我安装的WInService

如何更改命令提示符/控制台窗口/ DOS框中的制表符?

根据屏幕分辨率调整窗体控件的大小

单声道,asp.net c#和MVC如何和教程

问题是我在“使用”子句中创建了我的内存映射文件。 这是删除内存映射文件。 非常感谢您的帮助@RbMm! 你帮我解决这个问题,如果没有你的帮助,我不认为我有这么多的解决办法。 这是我们的聊天记录:

http://chat.stackoverflow.com/rooms/132839/discussion-between-jack-geiger-and-rbmm

总结

以上是内存溢出为你收集整理的如何使用内存映射文件在同一台计算机上的服务器和客户端之间进行通信?全部内容,希望文章能够帮你解决如何使用内存映射文件在同一台计算机上的服务器和客户端之间进行通信?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1276965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存