断点续传的原理
断点续传是大型文件数据传输的核心。本文将以多线程技术和Socket技术为依托,介绍大型文件断点续传的实现方法。
![](/Images/OutliningIndicators/None.gif)
基本实现思想
![](/Images/OutliningIndicators/None.gif)
多线程断点续传实现的基本思想就是在发送端(也称客户端)将要传输的文件分割为大小相当的多块,用多个线程,将这些块同时向目标服务器端发送;在服务器端的服务程序监听数据传输请求,每当接到新的请求,则创建一个新的线程,与客户端的发送线程对应,接收数据,记录数据传输进程
![](/Images/OutliningIndicators/None.gif)
图1是点对点文件断点续传第N块传输过程示意图。在传输发起端(客户端),将大型文件事先分割为大小相当的N块,同时创建N个传输线程,连接目标端服务器。当服务器端接收到每一个连接请求后,告知客户端可以传输文件。当客户端接收到可以传输文件的消息时,首先向服务器发送数据传输信息块(包括第几块、在块中的起始位置)请求,当服务器端接收到该请求后,向客户端发送数据传输信息,客户端然后传输数据传输信息块指定的数据给服务器端,服务器端更新数据传输信息块。
![](/Images/OutliningIndicators/None.gif)
(一)断点续传的原理
其实断点续传的原理很简单,就是在Http的请求上和一般的下载有所不同而已。
打个比方,浏览器请求服务器上的一个文时,所发出的请求如下:
假设服务器域名为wwww.sjtu.edu.cn,文件名为down.zip。
GET /down.zip HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-
excel, application/msword, application/vnd.ms-powerpoint, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Connection: Keep-Alive
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
服务器收到请求后,按要求寻找请求的文件,提取文件的信息,然后返回给浏览器,返回信息如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
200
Content-Length=106786028
Accept-Ranges=bytes
Date=Mon, 30 Apr 2001 12:56:11 GMT
ETag=W/"02ca57e173c11:95b"
Content-Type=application/octet-stream
Server=Microsoft-IIS/5.0
Last-Modified=Mon, 30 Apr 2001 12:56:11 GMT
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
所谓断点续传,也就是要从文件已经下载的地方开始继续下载。所以在客户端浏览器传给
Web服务器的时候要多加一条信息--从哪里开始。
下面是用自己编的一个"浏览器"来传递请求信息给Web服务器,要求从2000070字节开始。
GET /down.zip HTTP/1.0
User-Agent: NetFox
RANGE: bytes=2000070-
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
仔细看一下就会发现多了一行RANGE: bytes=2000070-
这一行的意思就是告诉服务器down.zip这个文件从2000070字节开始传,前面的字节不用传了。
服务器收到这个请求以后,返回的信息如下:
206
Content-Length=106786028
Content-Range=bytes 2000070-106786027/106786028
Date=Mon, 30 Apr 2001 12:55:20 GMT
ETag=W/"02ca57e173c11:95b"
Content-Type=application/octet-stream
Server=Microsoft-IIS/5.0
Last-Modified=Mon, 30 Apr 2001 12:55:20 GMT
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
和前面服务器返回的信息比较一下,就会发现增加了一行:
Content-Range=bytes 2000070-106786027/106786028
返回的代码也改为206了,而不再是200了。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
知道了以上原理,就可以进行断点续传的编程了。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
(二)Java实现断点续传的关键几点
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
(1)用什么方法实现提交RANGE: bytes=2000070-。
当然用最原始的Socket是肯定能完成的,不过那样太费事了,其实Java的net包中提供了这种功能。代码如下:
URL url = new URL("http://www.sjtu.edu.cn/down.zip");
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
();
//设置User-Agent
httpConnection.setRequestProperty("User-Agent","NetFox");
//设置断点续传的开始位置
httpConnection.setRequestProperty("RANGE","bytes=2000070");
//获得输入流
InputStream input = httpConnection.getInputStream();
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
从输入流中取出的字节流就是down.zip文件从2000070开始的字节流。
大家看,其实断点续传用Java实现起来还是很简单的吧。
接下来要做的事就是怎么保存获得的流到文件中去了。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
保存文件采用的方法。
我采用的是IO包中的RandAccessFile类。
操作相当简单,假设从2000070处开始保存文件,代码如下:
RandomAccess oSavedFile = new RandomAccessFile("down.zip","rw");
long nPos = 2000070;
//定位文件指针到nPos位置
oSavedFile.seek(nPos);
byte[] b = new byte[1024];
int nRead;
//从输入流中读入字节流,然后写到文件中
while((nRead=input.read(b,0,1024)) > 0)
{
oSavedFile.write(b,0,nRead);
}
![](/Images/OutliningIndicators/None.gif)
怎么样,也很简单吧。
接下来要做的就是整合成一个完整的程序了。包括一系列的线程控制等等。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
(三)断点续传内核的实现
主要用了6个类,包括一个测试类。
SiteFileFetch.java负责整个文件的抓取,控制内部线程(FileSplitterFetch类)。
FileSplitterFetch.java负责部分文件的抓取。
FileAccess.java负责文件的存储。
SiteInfoBean.java要抓取的文件的信息,如文件保存的目录,名字,抓取文件的URL等。
Utility.java工具类,放一些简单的方法。
TestMethod.java测试类。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
下面是源程序:
/*
**SiteFileFetch.java
*/
package NetFox;
import java.io.*;
import java.net.*;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public class SiteFileFetch extends Thread {
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
SiteInfoBean siteInfoBean = null; //文件信息Bean
long[] nStartPos; //开始位置
long[] nEndPos; //结束位置
FileSplitterFetch[] fileSplitterFetch; //子线程对象
long nFileLength; //文件长度
boolean bFirst = true; //是否第一次取文件
boolean bStop = false; //停止标志
File tmpFile; //文件下载的临时信息
DataOutputStream output; //输出到文件的输出流
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public SiteFileFetch(SiteInfoBean bean) throws IOException
{
siteInfoBean = bean;
//tmpFile = File.createTempFile ("zhong","1111",new File(bean.getSFilePath()));
tmpFile = new File(bean.getSFilePath()+File.separator + bean.getSFileName()+".info");
if(tmpFile.exists ())
{
bFirst = false;
read_nPos();
}
else
{
nStartPos = new long[bean.getNSplitter()];
nEndPos = new long[bean.getNSplitter()];
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public void run()
{
//获得文件长度
//分割文件
//实例FileSplitterFetch
//启动FileSplitterFetch线程
//等待子线程返回
try{
if(bFirst)
{
nFileLength = getFileSize();
if(nFileLength == -1)
{
System.err.println("File Length is not known!");
}
else if(nFileLength == -2)
{
System.err.println("File is not access!");
}
else
{
for(int i=0;i new FileSplitterFetch( siteInfoBean.getSSiteURL(),
siteInfoBean.getSFilePath() + File.separator + siteInfoBean.getSFileName(),
nStartPos[i],nEndPos[i],i);
Utility.log( " Thread " + i + " , nStartPos =" + nStartPos[i]+", nEndPos = " + nEndPos[i]);
fileSplitterFetch[i].start();
}
// fileSplitterFetch[nPos.length-1] = new FileSplitterFetch(siteInfoBean.getSSiteURL(),
siteInfoBean.getSFilePath() + File.separator + siteInfoBean.getSFileName() , nPos[nPos.length-1], nFileLength, nPos.length-1);
// Utility.log("Thread " + (nPos.length-1 ) + " , nStartPos = " + nPos[nPos.length-1] + ",
nEndPos = " + nFileLength);
// fileSplitterFetch[nPos.length-1].start();
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//等待子线程结束
//int count = 0;
//是否结束while循环
boolean breakWhile = false;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
while(!bStop)
{
write_nPos();
Utility.sleep(500);
breakWhile = true;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
for(int i=0;i4)
// siteStop();
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
System.err.println("文件下载结束!");
}
catch(Exception e){e.printStackTrace ();}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//获得文件长度
public long getFileSize()
{
int nFileLength = -1;
try{
URL url = new URL(siteInfoBean.getSSiteURL());
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection ();
httpConnection.setRequestProperty("User-Agent","NetFox");
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
int responseCode=httpConnection.getResponseCode();
if(responseCode>=400)
{
processErrorCode(responseCode);
return -2; //-2 represent access is error
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
String sHeader;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
for(int i=1;;i++)
{
//DataInputStream in = new DataInputStream(httpConnection.getInputStream ());
//Utility.log(in.readLine());
sHeader=httpConnection.getHeaderFieldKey(i);
if(sHeader!=null)
{
if(sHeader.equals("Content-Length"))
{
nFileLength = Integer.parseInt(httpConnection.getHeaderField(sHeader));
break;
}
}
else
break;
}
}
catch(IOException e){e.printStackTrace ();}
catch(Exception e){e.printStackTrace ();}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
Utility.log(nFileLength);
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
return nFileLength;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//保存下载信息(文件指针位置)
private void write_nPos()
{
try{
output = new DataOutputStream(new FileOutputStream(tmpFile));
output.writeInt(nStartPos.length);
for(int i=0;iDataInputStream(new FileInputStream(tmpFile));
int nCount = input.readInt();
nStartPos = new long[nCount];
nEndPos = new long[nCount];
for(int i=0;i 0 & & nStartPos < nEndPos && !bStop)
{
nStartPos += fileAccessI.write(b,0,nRead);
//if(nThreadID == 1)
// Utility.log("nStartPos = " + nStartPos + ", nEndPos = " + nEndPos);
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
Utility.log("Thread " + nThreadID + " is over!");
bDownOver = true;
//nPos = fileAccessI.write (b,0,nRead);
}
catch(Exception e){e.printStackTrace ();}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//打印回应的头信息
public void logResponseHead(HttpURLConnection con)
{
for(int i=1;;i++)
{
String header=con.getHeaderFieldKey(i);
if(header!=null)
//responseHeaders.put(header,httpConnection.getHeaderField(header));
Utility.log(header+" : "+con.getHeaderField(header));
else
break;
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public void splitterStop()
{
bStop = true;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
/*
**FileAccess.java
*/
package NetFox;
import java.io.*;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public class FileAccessI implements Serializable{
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
RandomAccessFile oSavedFile;
long nPos;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public FileAccessI() throws IOException
{
this("",0);
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public FileAccessI(String sName,long nPos) throws IOException
{
oSavedFile = new RandomAccessFile(sName,"rw");
this.nPos = nPos;
oSavedFile.seek(nPos);
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public synchronized int write(byte[] b,int nStart,int nLen)
{
int n = -1;
try{
oSavedFile.write(b,nStart,nLen);
n = nLen;
}
catch(IOException e)
{
e.printStackTrace ();
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
return n;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
/*
**SiteInfoBean.java
*/
package NetFox;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public class SiteInfoBean {
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
private String sSiteURL; //Site's URL
private String sFilePath; //Saved File's Path
private String sFileName; //Saved File's Name
private int nSplitter; //Count of Splited Downloading File
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public SiteInfoBean()
{
//default value of nSplitter is 5
this("","","",5);
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public SiteInfoBean(String sURL,String sPath,String sName,int nSpiltter)
{
sSiteURL= sURL;
sFilePath = sPath;
sFileName = sName;
this.nSplitter = nSpiltter;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public String getSSiteURL()
{
return sSiteURL;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public void setSSiteURL(String value)
{
sSiteURL = value;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public String getSFilePath()
{
return sFilePath;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public void setSFilePath(String value)
{
sFilePath = value;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public String getSFileName()
{
return sFileName;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public void setSFileName(String value)
{
sFileName = value;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public int getNSplitter()
{
return nSplitter;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public void setNSplitter(int nCount)
{
nSplitter = nCount;
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
/*
**Utility.java
*/
package NetFox;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public class Utility {
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public Utility()
{
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public static void sleep(int nSecond)
{
try{
Thread.sleep(nSecond);
}
catch(Exception e)
{
e.printStackTrace ();
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public static void log(String sMsg)
{
System.err.println(sMsg);
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public static void log(int sMsg)
{
System.err.println(sMsg);
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
/*
**TestMethod.java
*/
package NetFox;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public class TestMethod {
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public TestMethod()
{ ///xx/weblogic60b2_win.exe
try{
SiteInfoBean bean = new SiteInfoBean("http://localhost/xx/weblogic60b2_win.exe"," L: emp","weblogic60b2_win.exe",5);
//SiteInfoBean bean = new SiteInfoBean("http://localhost:8080/down.zip"," L: emp","weblogic60b2_win.exe",5);
SiteFileFetch fileFetch = new SiteFileFetch(bean);
fileFetch.start();
}
catch(Exception e){e.printStackTrace ();}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public static void main(String[] args)
{
new TestMethod();
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
一,最重要的一点,断点续传需要服务器的支持,这个是必要条件。
传统的FTP SERVER是不支持断点续传的,因为它不支持REST指令,传统的FTP指令(我是指服务器端指令)并不包括REST指令。
第二,客户端要知道使用REST等一系列指令来作断点续传。
看看断点续传的详细过程(FTP SERVER):
首先客户端使用REST指令来告诉FTP SERVER它需要从文件的某个点开始传,接着用STOR或者RETR命令开始传文件,大概的命令的流程如下:
TYPE I
200 Type set to I.
PASV
227 Entering Passive Mode (204,48,18,69,98,250)
REST 187392
350 Restarting at 187392. Send STORE or RETRIEVE to initiate transfer.
RETR /pub/audio/pci/maestro-3/win2k/1056.zip
150 Opening BINARY mode data connection for /pub/audio/pci/maestro-3/win2k/1056.zip (936098 bytes).
首先使用TYPE命令告诉FTP SERVER使用BINARY模式传送文件;
然后使用PASV命令告诉FTP SERVER使用被动打开模式来传送文件;
接着使用REST 187392指令告诉FTP SERVER要从文件的187392字节开始传送;
最后使用RETR指令来传送文件。
从上面可以看出,这个FTP SERVER支持REST指令,有的FTP SERVER(特别的老的)是不支持这个指令的,这时即使FTP CLIENT支持断点续传也一点用都没有!
支持断点的FTP SERVER:Serv-U FTP,还有一系列的新出现的FTP SERVER;
不支持断点的:IIS4以前版本所带的都不行,IIS5 有,不家可以测试一下,登录进FTP SERVER,然后输入REST 1000命令,看服务器是否认识,认识就是支持断点。
上面说的是FTP SERVER的断点,HTTP的断点续传是这样的:
在以前版本的HTTP SERVER也是不支持断点的,HTTP/1.1开始就支持了,具体如下:
在HTTP请求的头部信息里面,通常是这样的:
GET http://xxx.xxx.xxx.xxx/index.html HTTP/1.1
Host:www.163.net
Accept:*/*
上面是HTTP请求头的主要内容,是浏览器等客户端发给HTTP SERVER的信息。
在这个请求头里面,第一行叫做Request Line,GET叫做请求方法(通常得到一个HTML页面都是用GET,CGI等请求是用POST),http://bbs.netbuddy.org/index.html是URL,HTTP/1.1为版本号。
Host:bbs.netbuddy.org是HTTP服务器名字,这也是HTTP/1.1的新东东,以前做虚拟主机可是要一个主机名对应多个IP,现在好了![](https://www.cnblogs.com/Images/dot.gif)
呵呵,这个离题太远,不说了)
要做断点续传,浏览器等客户端需要在请求头里面发送
Range: bytes=1140736-
这样的请求,就是告诉HTTP SERVER,这个文件要从1140736字节开始传送。
最 后一点,大家看了上面的描述可能会有一个问题,那么多点传送怎么做呢?那就是多起几个线程,连接到服务器,用断点指令来传送文件,在传送的过程中,会检查 前面的(比如说第一个蚂蚁)得到的文件的部分是否超过了后面的(比如说第二个蚂蚁)的起点,相等就停前面的蚂蚁,最后再合并几个部分,就得到一个完整的文 件了
![](/Images/OutliningIndicators/None.gif)
----------------------------------------------
C版本实现方式:
![](/Images/OutliningIndicators/None.gif)
3 具体实现
![](/Images/OutliningIndicators/None.gif)
在实现过程中我使用了MFC的多线程和Windows的Socket,分客户端和服务器端实现。因为数据传输往往是对等的,所以需要将客户端和服务器端集成在一起,在客户端和服务器端分别实现后,这是件非常简单的工作,只需将它们集成在一起即可。下面将分别介绍客户端和服务器端的实现。
![](/Images/OutliningIndicators/None.gif)
3.1 关键数据结构
![](/Images/OutliningIndicators/None.gif)
文件信息数据结构 用于在服务器端和客户端之间传递文件第N块的属性信息,详细定义如下:
![](/Images/OutliningIndicators/None.gif)
structfileinfo
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
int fileno; //文件号
![](/Images/OutliningIndicators/None.gif)
int type; //消息类别
![](/Images/OutliningIndicators/None.gif)
long len; //文件(块)长度,在客户端向服务器端发送数据时,是文件长度;
![](/Images/OutliningIndicators/None.gif)
//在服务器端向客户端发送已传部分的信息时,是应该续传部分的长度;
![](/Images/OutliningIndicators/None.gif)
long seek; //开始位置,标识要传输数据在原文件中的起始位置
![](/Images/OutliningIndicators/None.gif)
char name[MAX_PATH_LEN];//文件名
![](/Images/OutliningIndicators/None.gif)
};
![](/Images/OutliningIndicators/None.gif)
发送进度记录结构 用户记录文件传输进程,详细定义如下:
![](/Images/OutliningIndicators/None.gif)
structSentInfo
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
long totle; //已经成功发送数据的长度;
![](/Images/OutliningIndicators/None.gif)
int block; //块标识;
![](/Images/OutliningIndicators/None.gif)
long filelen; //文件总长度;
![](/Images/OutliningIndicators/None.gif)
int threadno; //负责传输第N块数据的线程标识;
![](/Images/OutliningIndicators/None.gif)
CString name; //文件名称
![](/Images/OutliningIndicators/None.gif)
};
![](/Images/OutliningIndicators/None.gif)
客户端类 客户端文件发送实例封装,用户记录客户端文件发送过程中的属性信息、已发送大小、发送线程句柄、发送线程状态、发送统计信息等,具体定义是:
![](/Images/OutliningIndicators/None.gif)
classCClient:publicCObject
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
protected:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//Attributes
![](/Images/OutliningIndicators/None.gif)
public:
![](/Images/OutliningIndicators/None.gif)
CClient(CString ip);
![](/Images/OutliningIndicators/None.gif)
~CClient();
![](/Images/OutliningIndicators/None.gif)
SentInfo doinfo;
![](/Images/OutliningIndicators/None.gif)
long m_index; //块索引
![](/Images/OutliningIndicators/None.gif)
BOOL sendOk[BLOCK]; //块发送结束状态
![](/Images/OutliningIndicators/None.gif)
CString SendFileName;
![](/Images/OutliningIndicators/None.gif)
CString DestIp; //目标IP地址
![](/Images/OutliningIndicators/None.gif)
THREADSTATUS SendStatus;
![](/Images/OutliningIndicators/None.gif)
int GetBlockIndex(); //获取当前要传输文件块的序号,例如0,1,2…
![](/Images/OutliningIndicators/None.gif)
CCriticalSection m_gCS;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//Sending File Block Thread Handles
![](/Images/OutliningIndicators/None.gif)
HANDLE m_thread[BLOCK]; //Block Sending Thread Handles Array
![](/Images/OutliningIndicators/None.gif)
HANDLE m_ExitThread; //WaitToExitThread Handle
![](/Images/OutliningIndicators/None.gif)
HANDLE m_ProcessTrackThread;
![](/Images/OutliningIndicators/None.gif)
HANDLE m_hEventKill; //User canceled Event
![](/Images/OutliningIndicators/None.gif)
long m_IdxInListCtrl; //Index in ListView
![](/Images/OutliningIndicators/None.gif)
long m_lBeginTimeTick;
![](/Images/OutliningIndicators/None.gif)
long m_lBytesSent;
![](/Images/OutliningIndicators/None.gif)
// Operations
![](/Images/OutliningIndicators/None.gif)
public:
![](/Images/OutliningIndicators/None.gif)
};
![](/Images/OutliningIndicators/None.gif)
3.2 客户端
![](/Images/OutliningIndicators/None.gif)
发送线程 用于发送由lpparam参数指定的Client对象标识的文件块数据。具体实现是:
![](/Images/OutliningIndicators/None.gif)
//发送线程主函数
![](/Images/OutliningIndicators/None.gif)
//并行进行的线程,针对每个要发送的文件有BLOCK个该线程
![](/Images/OutliningIndicators/None.gif)
DWORDWINAPISendThread(LPVOIDlpparam)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
CClient* pClient=(CClient*)lpparam;
![](/Images/OutliningIndicators/None.gif)
int block_index = pClient->GetBlockIndex();
![](/Images/OutliningIndicators/None.gif)
//发送文件的第m_index块
![](/Images/OutliningIndicators/None.gif)
//发送线程
![](/Images/OutliningIndicators/None.gif)
sockaddr_in local;
![](/Images/OutliningIndicators/None.gif)
SOCKET m_socket;
![](/Images/OutliningIndicators/None.gif)
int rc=0;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
local.sin_family=AF_INET;
![](/Images/OutliningIndicators/None.gif)
local.sin_port=htons(pMainFrm->m_FilePort);
![](/Images/OutliningIndicators/None.gif)
local.sin_addr.S_un.S_addr=inet_addr(pClient->DestIp);
![](/Images/OutliningIndicators/None.gif)
m_socket=socket(AF_INET,SOCK_STREAM,0);
![](/Images/OutliningIndicators/None.gif)
int ret;
![](/Images/OutliningIndicators/None.gif)
char m_buf[MAX_BUFSIZE];
![](/Images/OutliningIndicators/None.gif)
fileinfo file_info;
![](/Images/OutliningIndicators/None.gif)
//Connect to Server
![](/Images/OutliningIndicators/None.gif)
ret = connect(m_socket,(LPSOCKADDR)&local,sizeof(local));
![](/Images/OutliningIndicators/None.gif)
while((ret == SOCKET_ERROR)&&(WaitForSingleObject(pClient->m_hEventKill, 0)
![](/Images/OutliningIndicators/None.gif)
==WAIT_TIMEOUT))
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
closesocket (m_socket);
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
file_info.len = pClient->doinfo.filelen ;
![](/Images/OutliningIndicators/None.gif)
if (file_info.len <= 0) //已经完成传输
![](/Images/OutliningIndicators/None.gif)
return 1;
![](/Images/OutliningIndicators/None.gif)
file_info.seek=0;
![](/Images/OutliningIndicators/None.gif)
file_info.type=1; //从服务器上获取文件的第index块已接收的大小
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
file_info.fileno = block_index;
![](/Images/OutliningIndicators/None.gif)
strcpy(file_info.name,pClient->doinfo.name);
![](/Images/OutliningIndicators/None.gif)
//发送第index块已发送大小请求
![](/Images/OutliningIndicators/None.gif)
sendn(m_socket,(char*)&file_info,PROTOCOL_HEADSIZE);
![](/Images/OutliningIndicators/None.gif)
long nRead = readn(m_socket,m_buf,PROTOCOL_HEADSIZE);
![](/Images/OutliningIndicators/None.gif)
if(nRead <= 0)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
closesocket (m_socket);
![](/Images/OutliningIndicators/None.gif)
return -1;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
fileinfo * pfile_info = (fileinfo*)m_buf;
![](/Images/OutliningIndicators/None.gif)
if (pfile_info->len == 0)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
closesocket (m_socket);
![](/Images/OutliningIndicators/None.gif)
//block_index块是续传,且发送完毕
![](/Images/OutliningIndicators/None.gif)
pClient->good[block_index] = TRUE;
![](/Images/OutliningIndicators/None.gif)
return 1;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
CFile SourceFile;
![](/Images/OutliningIndicators/None.gif)
SourceFile.Open(pClient->SendFileName,
![](/Images/OutliningIndicators/None.gif)
CFile::modeRead|CFile::typeBinary| CFile::shareDenyNone);
![](/Images/OutliningIndicators/None.gif)
//Seek right position of sending file
![](/Images/OutliningIndicators/None.gif)
long apos=SourceFile.Seek(pfile_info->seek,CFile::begin);
![](/Images/OutliningIndicators/None.gif)
int len1;
![](/Images/OutliningIndicators/None.gif)
len1=pfile_info->len;
![](/Images/OutliningIndicators/None.gif)
//取得已经发送的长度
![](/Images/OutliningIndicators/None.gif)
if (block_index < BLOCK - 1)
![](/Images/OutliningIndicators/None.gif)
pClient->doinfo.totle += pClient->doinfo.filelen / BLOCK - pfile_info->len;
![](/Images/OutliningIndicators/None.gif)
else
![](/Images/OutliningIndicators/None.gif)
pClient->doinfo.totle += pClient->doinfo.filelen –
![](/Images/OutliningIndicators/None.gif)
pClient->doinfo.filelen/BLOCK* (BLOCK - 1) - pfile_info->len;
![](/Images/OutliningIndicators/None.gif)
int nBegin = GetTickCount();
![](/Images/OutliningIndicators/None.gif)
int SendTotal = 0;
![](/Images/OutliningIndicators/None.gif)
while((len1 > 0) && (WaitForSingleObject(pClient->m_hEventKill, 0) == WAIT_TIMEOUT))
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
nRead = SourceFile.Read(m_buf, (len1 < pMainFrm->m_CurrentBlockSize) ?
![](/Images/OutliningIndicators/None.gif)
len1:pMainFrm->m_CurrentBlockSize);
![](/Images/OutliningIndicators/None.gif)
int send_count=sendn(m_socket,m_buf,nRead);
![](/Images/OutliningIndicators/None.gif)
float nLimit;
![](/Images/OutliningIndicators/None.gif)
if(send_count<=0)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
closesocket (m_socket);
![](/Images/OutliningIndicators/None.gif)
SourceFile.Close();
![](/Images/OutliningIndicators/None.gif)
return 1;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
len1 = len1 - send_count;
![](/Images/OutliningIndicators/None.gif)
//进度信息
![](/Images/OutliningIndicators/None.gif)
pClient->doinfo.totle += send_count;
![](/Images/OutliningIndicators/None.gif)
SendTotal += send_count;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
SourceFile.Close();
![](/Images/OutliningIndicators/None.gif)
shutdown(m_socket,2);
![](/Images/OutliningIndicators/None.gif)
closesocket (m_socket);
![](/Images/OutliningIndicators/None.gif)
if(len1 <= 0)pClient->good[block_index] = TRUE;
![](/Images/OutliningIndicators/None.gif)
return 1;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
创建发送线程 对应要发送的文件,创建BLOCK个线程,负责发送各自的数据块。
![](/Images/OutliningIndicators/None.gif)
for(inti=0;i
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
//创建线程,状态为停止状态
![](/Images/OutliningIndicators/None.gif)
pClient->m_thread[i] = ::CreateThread(NULL,0,SendThread,(LPVOID)pClient,
![](/Images/OutliningIndicators/None.gif)
CREATE_SUSPENDED,&dwthread);
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
::SetThreadPriority(pClient->m_thread[i],THREAD_PRIORITY_HIGHEST);
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
3.3 服务器端
![](/Images/OutliningIndicators/None.gif)
监听函数 负责监听文件传输请求,在接受请求后,创建数据块接收线程,具体实现如下:
![](/Images/OutliningIndicators/None.gif)
//监听函数
![](/Images/OutliningIndicators/None.gif)
DWORDWINAPIlistenthread(LPVOIDlpparam)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
SOCKET pSocket=(SOCKET)lpparam;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//最多监听MAX_LISTENCOUNT个客户端连接
![](/Images/OutliningIndicators/None.gif)
int rc=listen(pSocket,MAX_LISTENCOUNT);
![](/Images/OutliningIndicators/None.gif)
if (rc<0)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
//Error
![](/Images/OutliningIndicators/None.gif)
return 0;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
//Listening all the incoming connections
![](/Images/OutliningIndicators/None.gif)
while(1)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
SOCKET aSocket;
![](/Images/OutliningIndicators/None.gif)
//Listening and Block
![](/Images/OutliningIndicators/None.gif)
aSocket=accept(pSocket,NULL,NULL);
![](/Images/OutliningIndicators/None.gif)
DWORD dwthread;
![](/Images/OutliningIndicators/None.gif)
// Create Server Data Receiving Thread "Serverthread"
![](/Images/OutliningIndicators/None.gif)
// And Transfers data
![](/Images/OutliningIndicators/None.gif)
::CreateThread(NULL,0, Serverthread,(LPVOID)aSocket,0,&dwthread);
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
return 0;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
接收文件数据 负责接收由参数seek和len以及block_index标识的数据块,实现如下:
![](/Images/OutliningIndicators/None.gif)
//接收文件数据
![](/Images/OutliningIndicators/None.gif)
voidRecvFile(SOCKETso,CStringPeerName,CString DestFileName,long seek,
![](/Images/OutliningIndicators/None.gif)
longlen,intblock_index,CFileInfoRecving*pfileinfo)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
CFile DestFile;
![](/Images/OutliningIndicators/None.gif)
long fileseek = seek;
![](/Images/OutliningIndicators/None.gif)
long filelen = len;
![](/Images/OutliningIndicators/None.gif)
CMainFrame* pWnd = (CMainFrame*) AfxGetMainWnd();
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
if (!DestFile.Open(pWnd->m_DestPath + "\\IP" + PeerName + "-" + DestFileName,
![](/Images/OutliningIndicators/None.gif)
CFile::modeWrite|CFile::typeBinary| CFile::shareDenyNone))
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
DWORD error = GetLastError();
![](/Images/OutliningIndicators/None.gif)
return;
![](/Images/OutliningIndicators/None.gif)
};
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
long apos = DestFile.Seek(fileseek,CFile::begin);
![](/Images/OutliningIndicators/None.gif)
char m_buf[MAX_BUFSIZE];
![](/Images/OutliningIndicators/None.gif)
CString m_temp;
![](/Images/OutliningIndicators/None.gif)
m_temp.Format("%s\\Temp\\IP%s-%s.%d",pWnd->m_appPath,PeerName,
![](/Images/OutliningIndicators/None.gif)
DestFileName,block_index);
![](/Images/OutliningIndicators/None.gif)
CFile BlockFile;
![](/Images/OutliningIndicators/None.gif)
BlockFile.Open(m_temp, CFile::modeWrite | CFile::typeBinary);
![](/Images/OutliningIndicators/None.gif)
while((filelen>0) && (WaitForSingleObject(pWnd->m_hEventKill, 0) == WAIT_TIMEOUT))
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
int nRead = readn(so, m_buf, (filelen > MAX_BUFSIZE) ? MAX_BUFSIZE : filelen);
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//Exited
![](/Images/OutliningIndicators/None.gif)
if (nRead == 0)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
CString msg;
![](/Images/OutliningIndicators/None.gif)
msg.Format("[SOCKET %d] Receiving file %s break(Recv 0).",so,DestFileName);
![](/Images/OutliningIndicators/None.gif)
pWnd->SendMessage(WM_NEWMSG,(LPARAM)msg.GetBuffer(0),3);
![](/Images/OutliningIndicators/None.gif)
msg.ReleaseBuffer();
![](/Images/OutliningIndicators/None.gif)
break;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
if(nRead<0)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
CString msg;
![](/Images/OutliningIndicators/None.gif)
msg.Format("[SOCKET %d] Receiving file %s break.",so,DestFileName);
![](/Images/OutliningIndicators/None.gif)
pWnd->SendMessage(WM_NEWMSG,(LPARAM)msg.GetBuffer(0),3);
![](/Images/OutliningIndicators/None.gif)
msg.ReleaseBuffer();
![](/Images/OutliningIndicators/None.gif)
break;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
DestFile.Write(m_buf,nRead);
![](/Images/OutliningIndicators/None.gif)
filelen -= nRead;
![](/Images/OutliningIndicators/None.gif)
fileseek += nRead;
![](/Images/OutliningIndicators/None.gif)
BlockFile.Seek(0,CFile::begin);
![](/Images/OutliningIndicators/None.gif)
BlockFile.Write(&fileseek,sizeof(long));
![](/Images/OutliningIndicators/None.gif)
BlockFile.Write(&filelen,sizeof(long));
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_seek = fileseek;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_len = filelen;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
closesocket (so);
![](/Images/OutliningIndicators/None.gif)
//Current Block Received OK
![](/Images/OutliningIndicators/None.gif)
BlockFile.Close();
![](/Images/OutliningIndicators/None.gif)
DestFile.Close();
![](/Images/OutliningIndicators/None.gif)
if (filelen <= 0)
![](/Images/OutliningIndicators/None.gif)
pfileinfo->canDel = true;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
初始化文件数据块信息 负责初始化文件传输时数据块的大小、开始位置等信息。具体实现是:
![](/Images/OutliningIndicators/None.gif)
//初始化文件数据块信息
![](/Images/OutliningIndicators/None.gif)
CFileInfoRecving*GetRecvedFileBlock(CStringPeerName,CStringFileName,long& filelen,
![](/Images/OutliningIndicators/None.gif)
long&fileseek,intblock_index)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
CString m_temp;
![](/Images/OutliningIndicators/None.gif)
CMainFrame* pWnd = (CMainFrame*) AfxGetMainWnd();
![](/Images/OutliningIndicators/None.gif)
m_temp.Format("%s\\temp\\IP%s-%s.%d",pWnd->m_appPath,PeerName,FileName,block_index);
![](/Images/OutliningIndicators/None.gif)
FILE* fp=NULL;
![](/Images/OutliningIndicators/None.gif)
CFile Blockfile;
![](/Images/OutliningIndicators/None.gif)
CFile RecvFile;
![](/Images/OutliningIndicators/None.gif)
long seek = fileseek;
![](/Images/OutliningIndicators/None.gif)
long flen = filelen;
![](/Images/OutliningIndicators/None.gif)
CString DestFile = pWnd->m_DestPath + "\\IP" + PeerName + "-" + FileName;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
bool DestFileNoExist = false;
![](/Images/OutliningIndicators/None.gif)
if((fp=fopen(DestFile,"r"))==NULL)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
RecvFile.Open(DestFile,
![](/Images/OutliningIndicators/None.gif)
CFile::modeCreate|CFile::modeWrite|CFile::typeBinary| CFile::shareDenyNone);
![](/Images/OutliningIndicators/None.gif)
RecvFile.Close();
![](/Images/OutliningIndicators/None.gif)
DestFileNoExist = true;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
if (fp) fclose(fp);
![](/Images/OutliningIndicators/None.gif)
CFileInfoRecving* pfileinfo = NULL;
![](/Images/OutliningIndicators/None.gif)
//如果目标文件不存在或者长度记录文件不存在,重新传
![](/Images/OutliningIndicators/None.gif)
if( ((fp=fopen(m_temp,"r"))==NULL) || (DestFileNoExist))
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
//第一次接收
![](/Images/OutliningIndicators/None.gif)
fileseek = block_index*(flen/BLOCK);
![](/Images/OutliningIndicators/None.gif)
filelen = flen/BLOCK;
![](/Images/OutliningIndicators/None.gif)
if (block_index == (BLOCK - 1))
![](/Images/OutliningIndicators/None.gif)
filelen = flen - fileseek;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//Add To Recving File List
![](/Images/OutliningIndicators/None.gif)
pfileinfo = new CFileInfoRecving();
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_sFileName = FileName;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_sPeerName = PeerName;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_nBlockIndex = block_index;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_sFileSize.Format("%d",flen);
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
seek = fileseek;
![](/Images/OutliningIndicators/None.gif)
flen = filelen;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_seek = seek;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_len = flen;
![](/Images/OutliningIndicators/None.gif)
pWnd->m_RecvingFileList.AddTail(pfileinfo);
![](/Images/OutliningIndicators/None.gif)
Blockfile.Open(m_temp,
![](/Images/OutliningIndicators/None.gif)
CFile::modeCreate|CFile::modeWrite| CFile::typeBinary | CFile::shareDenyNone);
![](/Images/OutliningIndicators/None.gif)
Blockfile.Write(&seek,sizeof(long));
![](/Images/OutliningIndicators/None.gif)
Blockfile.Write(&flen,sizeof(long));
![](/Images/OutliningIndicators/None.gif)
Blockfile.Close();
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
else
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
POSITION pos = pWnd->m_RecvingFileList.GetHeadPosition();
![](/Images/OutliningIndicators/None.gif)
while (pos)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
pfileinfo = pWnd->m_RecvingFileList.GetAt(pos);
![](/Images/OutliningIndicators/None.gif)
if (pfileinfo)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
if ((pfileinfo->m_sFileName == FileName) &&
![](/Images/OutliningIndicators/None.gif)
(pfileinfo->m_sPeerName == PeerName) &&
![](/Images/OutliningIndicators/None.gif)
(pfileinfo->m_nBlockIndex == block_index))
![](/Images/OutliningIndicators/None.gif)
break;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
pWnd->m_RecvingFileList.GetNext(pos);
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
Blockfile.Open(m_temp,CFile::modeRead | CFile::typeBinary | CFile::shareDenyNone);
![](/Images/OutliningIndicators/None.gif)
Blockfile.Read(&fileseek,sizeof(long));
![](/Images/OutliningIndicators/None.gif)
Blockfile.Read(&filelen,sizeof(long));
![](/Images/OutliningIndicators/None.gif)
if ((pfileinfo == NULL) || (!pos))
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
pfileinfo = new CFileInfoRecving();
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_sFileName = FileName;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_sPeerName = PeerName;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_nBlockIndex = block_index;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_sFileSize.Format("%d",flen);
![](/Images/OutliningIndicators/None.gif)
//Add To Recving File List
![](/Images/OutliningIndicators/None.gif)
//pWnd->m_RecvingFileListCS.Lock();
![](/Images/OutliningIndicators/None.gif)
pWnd->m_RecvingFileList.AddTail(pfileinfo);
![](/Images/OutliningIndicators/None.gif)
//pWnd->m_RecvingFileListCS.Unlock();
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
Blockfile.Close();
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_seek = fileseek;
![](/Images/OutliningIndicators/None.gif)
pfileinfo->m_len = filelen;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
if (fp) fclose(fp);
![](/Images/OutliningIndicators/None.gif)
return pfileinfo;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
数据接收线程 负责从客户端接收数据信息:
![](/Images/OutliningIndicators/None.gif)
//数据接收线程
![](/Images/OutliningIndicators/None.gif)
DWORDWINAPIServerthread(LPVOIDlpparam)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
fileinfo* pFileInfo;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//
![](/Images/OutliningIndicators/None.gif)
char* m_buf = new char[PROTOCOL_HEADSIZE];
![](/Images/OutliningIndicators/None.gif)
SOCKET pthis=(SOCKET)lpparam;
![](/Images/OutliningIndicators/None.gif)
int nRead = readn(pthis,m_buf,PROTOCOL_HEADSIZE);
![](/Images/OutliningIndicators/None.gif)
if( nRead < 0 )
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
closesocket (pthis);
![](/Images/OutliningIndicators/None.gif)
return -1;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
pFileInfo = (fileinfo*)m_buf;
![](/Images/OutliningIndicators/None.gif)
CString MsgStr;
![](/Images/OutliningIndicators/None.gif)
int addrlen;
![](/Images/OutliningIndicators/None.gif)
CString FileID;
![](/Images/OutliningIndicators/None.gif)
sockaddr_in* pSocket;
![](/Images/OutliningIndicators/None.gif)
CFileInfoRecving* pfileinfo;
![](/Images/OutliningIndicators/None.gif)
switch(pFileInfo->type)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
case 1:
![](/Images/OutliningIndicators/None.gif)
//获取文件块的已接收大小
![](/Images/OutliningIndicators/None.gif)
pfileinfo =
![](/Images/OutliningIndicators/None.gif)
GetRecvedFileBlock(FileID,pFileInfo->name,pFileInfo->len,pFileInfo->seek,pFileInfo->fileno);
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
nRead = sendn(pthis,(char*)pFileInfo,PROTOCOL_HEADSIZE);
![](/Images/OutliningIndicators/None.gif)
if(nRead < 0)
![](/Images/OutliningIndicators/None.gif)
{
![](/Images/OutliningIndicators/None.gif)
closesocket (pthis);
![](/Images/OutliningIndicators/None.gif)
return -1;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//开始接收文件
![](/Images/OutliningIndicators/None.gif)
RecvFile(pthis,FileID,pFileInfo->name,pFileInfo->seek,
![](/Images/OutliningIndicators/None.gif)
pFileInfo->len,pFileInfo->fileno,pfileinfo);
![](/Images/OutliningIndicators/None.gif)
closesocket(pthis);
![](/Images/OutliningIndicators/None.gif)
break;
![](/Images/OutliningIndicators/None.gif)
default:
![](/Images/OutliningIndicators/None.gif)
break;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
delete [] m_buf;
![](/Images/OutliningIndicators/None.gif)
return 0;
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
4 小结
![](/Images/OutliningIndicators/None.gif)
本文简要介绍了点对点文件断点续传的多线程实现方法,有一定的使用性。限于水平,文中不当之处,敬请批评指正。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
原文地址:https://www.cnblogs.com/bluespot/p/1122588.html