linux下http协议多线程实现

上传人:汽*** 文档编号:499095169 上传时间:2023-10-23 格式:DOC 页数:12 大小:68KB
返回 下载 相关 举报
linux下http协议多线程实现_第1页
第1页 / 共12页
linux下http协议多线程实现_第2页
第2页 / 共12页
linux下http协议多线程实现_第3页
第3页 / 共12页
linux下http协议多线程实现_第4页
第4页 / 共12页
linux下http协议多线程实现_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《linux下http协议多线程实现》由会员分享,可在线阅读,更多相关《linux下http协议多线程实现(12页珍藏版)》请在金锄头文库上搜索。

1、linux下http协议 多线程下载实现该程序只能在http协议下工作,等以后研究其他协议时再补充。- E ?! K+ Z9 y2 5 K2 s* r! a j( I, G. EJ- F( % M, P. (网络服务器用的是腾讯公司的linuxqq 希望他们能原谅我)3 b, W0 ?* k V% 9 p- 编程思路:2 x& s; ?8 b! : zq! r- V1、分析http协议 数据包。6 I, h- O. P2 N R9 R- G1 Z以下是一个请求报文与相应的回复报文的例子, j+ ?) l0 / i; y9 P, S, YE- h. j o1 2 e. GET /linuxqq/

2、linuxqq-v1.0.2-beta1.i386.rpm HTTP/1.1, ? q8 Z2 r; l( v9 RAccept: */*8 L0 G: I9 y, g n( TAccept-Language: en-us* R9 v7 q1 m: - p9 FUser-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)$ zK! 5 X FA: h* w) i8 jHost: :80H8 ; p- G) e) R/ w8 ) 9 HConnection: Keep-Alive# t2 k3 X/ ?& c- c5 C. Z.

3、F0 J2 X* _; P/ ) 9 HTTP/1.1 206 Partial Content: U* c4 w: u! w v! c, HContent-Length: 1009349) l4 q: j4 / EKContent-Range: bytes 0-1009348/5046743; q4 C5 V3 O$ c& G% U/ Server: qqdlsrv(1.84 for linux). q& r3 X* O d+ U* J7 iXConnection: Keep-alive3 F; bh6 S/ f, x2 w3 |Content-Disposition: attachment;

4、 filename=linuxqq-v1.0.2-beta1.i386.rpm% ?. C9 X# ?9 k. U0 y* i8 Z$ cAccept-Ranges: bytes# o t0 p3 q4 K1 c! Y6 jContent-Type: application/octet-stream9 K, y k& E) f6 H$ z$ 7 h0 Q; r. K: b9 m$ e1 ?$ H$ D, G; k; U2、取得文件大小,然后进行对文件的分块。) T1 * r1 I3 Z2 3、对文件分块下载, S% d: m1 J7 M, s. U4、合并文件,并改名。 ! y) 3 d% L

5、+ D5 B& A( w) L9 P8 p, wq$ v+ A, ; w6 B总结:这个分块再合并总感觉太过于啰嗦。6 A0 P3 L+ W E. r, v5 u是否还有另外一种方法,7 0 % p% p t& r1 Y# R; v( 如迅雷,在一开始就取得文件大小就在硬盘分配空间,6 DM0 % E+ C C5 m) c% E然后分块下载直接写入到文件的不同块?2 A i2 g6 ?9 G0 i是否可以用文件指针锁住不同的区域并写入? y5 s8 n5 L, a% z8 bF9 Y: |; | t# q: x; O8 9 M# * A& 9 u% P7 x: c, L! V3 v/*# w:

6、 B- m* p2 I# k6 S+ 3 + r* down.c2 d2 W8 H4 G3 C9 w*! g$ c, D0 E, w6 f, j7 P! w*Created on: Mar 27, 20093 S( x- L% j5 l9 o- u* Author: root: i& ! ?. J# B% h! J C* this program is to download file in http protocol with multthreading( v& d) e! q; : y# r! |8 K5 Wi*/0 . k. i& f- E. p0 o0 r9 U$ L6 Z#ifnde

7、f STRUCT_H_% h& 9 v2 q2 3 J/ b#define STRUCT_H_3 U3 f% w$ i: B2 Q( d% KRJ, * Y0 u! m1 j! ?8 G#endif /* STRUCT_H_ */5 t0 N4 H T* k& r& _3 E8 V#include ! y U9 o9 i F- p#include & h9 a, 3 z2 o: p8 I) x4 H! y5 K#include . 1 ?$ z6 n# W+ a#include & F. F3 q9 K* D G#include 2 z( o! + |, #include 6 P+ + g6

8、I7 k9 t4 z3 v#include K# s: p7 e) 9 C: N V#include & z4 z& z! K& F! z/create a struct to save the file part infomation8 r3 O/ f% zW0 8 O5 : w4 Tstruct filepart& P9 T+ r* N+ E4 X( Z5 M$ P1 r3 q0 3 E! ?2 o+ N( d: B; S% O: g5 Gint partnum;) - e# u6 G k+ k- y7 fchar pathname128;! p- q; f: h1 b, M0 P% 5

9、ilong int lenth;0 u8 w0 , Q2 nlong int datastar;$ $ L- x6 L$ T. |) % jlong int dataend;6 k6 el7 F! |; Y kstruct filepart *next;% m% K$ l! O Z5 Q6 % L;4 h% : M7 C8 ctypedef struct filepart FilePart;0 E! ) G, : 9 Pstruct hostent *host;9 A: L, Y* * g: Fchar filepath2048;/save the filepath on the server

10、P% E0 c% 8 t: w mchar hostname1024;/save the name of the server3 w# $ _. Y4 r# Z9 g: O( _char filename256;/save the name of the file# W# F/ b& E( # ochar localpath512; 3 |- h; r7 8 C- Mint port,part;+ B) N) V* # x8 C, T7 blong int FileLen;/the file lenth0 d, w+ B; B7 T! S- + d/ u j9 Nvoid getname( c

11、har * ulr); /解析url,并从中取得主机名,ip地址 、文件路径以及端口0 8 m/ m+ _* P5 yvoid filepart( long int datalen,int part ); /对文件进行分块6 C! V& a# W6 v3 lvoid *getfile( FilePart *pointer); /下载线程函数3 e Y# H, K# qo% T% q$ A7 dlong int getFileLen( struct hostent *host); /获取http包头,分析文件大小1 ( n/ K6 N4 U. b+ r; i5 7 , vvoid createt

12、hread( FilePart * head, int num );/创建线程函数 参数为链表头指针和分块数目: 4 A- l7 8 ?2 A* int mergefile( FilePart * head);0 o 2 u! v. a. 4 9 Hint main(void) A9 2 Q7 Y+ 4 F, B% 4 , g# W& Q* G, % h8 j4 r Jchar ulr128;4 Z v: t; W* j+ P eprintf(please input the website:n); Q H& v. t, x6 dscanf(%s,ulr);- _+ L( P1 H4 npri

13、ntf(the website is :%sn,ulr);2 / h% ! f. Fx uprintf(input the direct to save file:n);( X O! ?$ Z+ U/ ; Z jscanf(%s,localpath);* j; 2 r: z: Z8 I Dgetname( ulr ); /get the ipaddress of the host8 m$ Z: k7 S; J. z/ getfile( host );+ |2 l9 ) e8 - E. o0 m) ?s aFileLen = getFileLen( host ); /get the lenth

14、of the file9 S3 m) m6 D- PZ; S* N( + Ffilepart( FileLen, 3);8 0 Q- E* T, T6 k# ) |8 C2 I4 8 O M* a4 Tputs(!Hello World!); /* prints !Hello World! */6 , 0 T; k4 P1 c Sreturn EXIT_SUCCESS;, W7 b4 R4 F% X( E ( A- C N1 s7 |+ N8 H3 |( Z1 z6 w7 ?0 B* v7 k# q, f- . P+ Z: ; y/ K* Z# O! V. u% F. M( G1 Xlong int getFileLen( struct hostent *host)0 8 i9 * A; c

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号