title: WinSock2 API tags: [WinSock, 网络编程, WinSock2.0 API, 动态加载, WinSock 异步函数] date: 2018-07-21 10:36:09 categories: Windows 网络编程 keywords: WinSock, 网络编程, WinSock2.0 API, 动态加载, WinSock 异步函数 --- WinSock中提供的5种网络模型已经可以做到很高效了,特别是完成端口,它的高效的原因在于它不仅另外开启了线程来处理完成通知而不是占用主程序的时间,同时也在于我们在完成端口中运用了大量异步IO处理函数。比如WSASend、WSARecv等等。为了高效的处理网络IO,WinSock提供了大量这样的异步函数。这篇博文主要探讨这些函数的用法和他们与传统的巴克利套接字相比更加高效的秘密
windows socket2定义了一种扩展机制,允许windows套接字服务提供者想应用程序设计者导出 先进的数据传输功能 1 GetAcceptExSockaddrs函数 粘贴从AcceptEx函数取得的数据,将本地和远程地址传递到sockaddr结构 2 TransmitFile函数 在一个已连接的套接字句柄上传输文件数据 3 TransmitPackets函数 与TransmitFile类似,不同之处是,既可以发送文件,也可以发送内存缓冲区中的数据 4 ConnextEx函数 为指定的套件诶子建立连
string filePath = Server.MapPath(“”);//路径
{ /* 微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite 下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。 代码如下: */ Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=z.zip"); string filename = Server.MapPath("DownLoad/aaa.zip"); Response.TransmitFile(filename); } //WriteFile实现下载 protected void Button2_Click(object sender, EventArgs e) { /* using System.IO; */ string fileName ="aaa.zip";//客户端保存的文件名 string filePath=Server.MapPath("DownLoad/aaa.zip");//路径 FileInfo fileInfo = new FileInfo(filePath); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(fileInfo.FullName); Response.Flush(); Response.End(); } //WriteFile分块下载 protected void Button3_Click(object sender, EventArgs e) { string fileName = "aaa.zip";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.zip");//路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true) { const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力 byte[] buffer = new byte[ChunkSize]; Response.Clear(); System.IO.FileStream iStream = System.IO.File.OpenRead(filePath); long dataLengthToRead = iStream.Length;//获取下载的文件总大小 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName)); while (dataLengthToRead > 0 && Response.IsClientConnected) { int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小 Response.OutputStream.Write(buffer, 0, lengthRead); Response.Flush(); dataLengthToRead = dataLengthToRead - lengthRead; } Response.Close(); } } //流方式下载 protected void Button4_Click(object sender, Eve
FileResult是一个基于文件的ActionResult,利用FileResult我们可以很容易地将从某个物理文件的内容响应给客户端。ASP.NET MVC定义了三个具体的FileResult,分
sendfile(2) is a UNIX system call which provides a “zero-copy” way of copying data from one file descriptor (a file) to another (a socket). Because this copying is done entirely within the kernel, sendfile(2) is more efficient than the combination of “file.read()” and “socket.send()”, which requires transferring data to and from user space. This copying of the data twice imposes some performance and resource penalties which sendfile(2) syscall avoids; it also results in a single system call (and thus only one context switch), rather than the series of read(2) / write(2) system calls (each system call requiring a context switch) used internally for the data copying. A more exhaustive explanation of how sendfile(2) works is available here, but long story short is that sending a file with sendfile() is usually twice as fast than using plain socket.send(). Typical applications which can benefit from using sendfile() are FTP and HTTP servers.
大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说【译】利用Asp.net MVC处理文件的上传下载,希望能够帮助大家进步!!!
很久没有发文了,一是忙上线的事。二是有些颓废了,身体虚弱了,感觉工作没有激情了,不太想写。好了,既然承诺了写下去,那么就坚持吧。
title: WinSock 重叠IO模型 tags: [WinSock 模型, 网络编程, 重叠IO模型] date: 2018-06-29 20:26:13 categories: Windows 网络编程 keywords: WinSock 模型, 网络编程, 重叠IO模型 --- 之前介绍的WSAAsyncSelect和WSAEvent模型解决了收发数据的时机问题,但是网卡这种设备相比于CPU和内存来说仍然是慢速设备,而调用send和recv进行数据收发操作仍然是同步的操作,即使我们能够在恰当的时机调用对应的函数进行收发操作,但是仍然需要快速的CPU等待慢速的网卡。这样仍然存在等待的问题,这篇博文介绍的重叠IO模型将解决这个等待的问题
关于IOCP网上到处都是资料,说的也很详细。我在这里就不再多说了,这只是本人在学习IOCP时的笔记,和配合AcceptEx写的一个极小的服务端程序。由于刚刚接触ICOP加上本人刚毕业不到一年,所以里面的理解或观点可能有误,还请大家多多批评!
----- By PiggyXP(小猪)
这篇文档我非常详细并且图文并茂的介绍了关于网络编程模型中完成端口的方方面面的信息,从API的用法到使用的步骤,从完成端口的实现机理到实际使用的注意事项,都有所涉及,并且为了让朋友们更直观的体会完成端口的用法,本文附带了有详尽注释的使用MFC编写的图形界面的示例代码。
领取专属 10元无门槛券
手把手带您无忧上云