求一段用socket多用户多文件下载源码!!急急^^^^^(50分)

  • 主题发起人 主题发起人 hs9206
  • 开始时间 开始时间
H

hs9206

Unregistered / Unconfirmed
GUEST, unregistred user!
要求:<br>clientsocket作客户端:用服务端硬盘上文件存放的位置如 :<br>d:/server/file/01.tw &nbsp; &nbsp; &nbsp; &nbsp;192.168.1.101<br>e:/server/file/13.tw &nbsp; &nbsp; &nbsp; &nbsp;192.168.1.100<br>d:/server/file/345.tw &nbsp; &nbsp; &nbsp; 192.168.1.100<br>e:/server/file/6435.tw &nbsp; &nbsp; &nbsp;192.168.1.10<br>和该文件服务端的IP 如上,clientsocket根据这两个参数从服务端下载<br>文件服务端的文件到本地,(下载后我可以将TW文件转换成可用的格式<br>这个不在问题讨论里)<br>seversocket做监听,可能在多用户同时请求下载文件时,响应,<br>并发文件到客户端……………<br><br>源码呀,不要只讲思路,因为我对socket的编程很不熟悉!!!!
 
const BLOCKSIZE = 1024 * 4;<br>const TRT_FILE = '001';<br>const TRT_NOP = '999';<br>//=============================<br><br>客户端<br>procedure TF_Main.ClientSocket1Read(Sender: TObject;<br> &nbsp;Socket: TCustomWinSocket);<br>var<br> &nbsp;li_actbyte: integer; //当前分块(此块)流的大小<br> &nbsp;lb_tmpbuff: array[0..BLOCKSIZE] of char; //流缓存空间<br>begin<br> &nbsp;li_actbyte := Socket.ReceiveLength;<br> &nbsp;Socket.ReceiveBuf(lb_tmpbuff, li_actbyte); //追加接收流<br> &nbsp;ist_picstream.Write(lb_tmpbuff, li_actbyte); //写入内存<br> &nbsp;StatusBar1.Panels[1].Text := '传输中...';<br> &nbsp;StatusBar1.Panels[2].Text := inttostr(ist_picstream.Size div 1024) + 'Kb已传输';<br> &nbsp;if li_actbyte &lt; BLOCKSIZE then //“流” 剩最后一块时或未开始接文件时 &nbsp; &nbsp;执行<br> &nbsp;begin<br> &nbsp; &nbsp;ist_picstream.Position := 0; //内存指向首位置<br> &nbsp; &nbsp;if is_transtype = TRT_FILE then //传的是文件 &nbsp;执行<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Save_localFIleName:= Save_FilePath + extractfilename(Save_FilePathName);<br> &nbsp; &nbsp; &nbsp;ist_picstream.SaveToFile(Save_localFIleName);<br> &nbsp; &nbsp; &nbsp;StatusBar1.Panels[3].Text := extractfilename(Save_FilePathName) + '保存成功';<br> &nbsp; &nbsp; &nbsp;StatusBar1.Panels[1].Text := '传输完毕';<br> &nbsp; &nbsp;// &nbsp;clientsocket1.Close; &nbsp; //================================<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;ist_picstream.Clear;<br> &nbsp; &nbsp;socket.SendText(TRT_NOP);<br> &nbsp; &nbsp;if Breadover = true then &nbsp; &nbsp;F_play.Show<br> &nbsp; &nbsp; &nbsp;else TwtoWav(Save_localFIleName,Save_FilePath,true) ;<br> &nbsp; &nbsp;//=======================================================<br> &nbsp; &nbsp;if listbox1.Count &gt; 0 then &nbsp;listbox1.Items.Delete(0);<br> &nbsp; &nbsp;listbox1.Refresh;<br> &nbsp; &nbsp;Breadover:=false;<br> &nbsp; &nbsp;if listbox1.Count &gt; 0 then &nbsp;TimerSend(sender);<br><br> &nbsp;end<br> &nbsp;else &nbsp;socket.SendText(is_cmdtext); //继续该文件//文件名<br>end;<br><br>服务端:<br><br>procedure TForm1.ServerSocket1ClientRead(Sender: TObject;<br> &nbsp;Socket: TCustomWinSocket);<br>var<br> &nbsp;ls_receive: string;<br> &nbsp;ls_info: string;<br> &nbsp;ls_cmd: string;<br> &nbsp;i: integer;<br>begin<br> &nbsp;ls_receive := Socket.ReceiveText;<br> &nbsp;ls_cmd := copy(ls_receive, 1, 3);<br> &nbsp;ls_info := copy(ls_receive, 4, length(ls_receive) - 3);<br> &nbsp;if ls_cmd = TRT_FILE then<br> &nbsp;begin<br> &nbsp; &nbsp;if FileExists(ls_info) then uf_sendfile(ls_info)<br> &nbsp; &nbsp;else begin<br> &nbsp; &nbsp; &nbsp;for I := 0 to (serversocket1.socket.ActiveConnections - 1) do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;serversocket1.Socket.Connections.SendText('004ERROR');<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>end;<br><br>procedure TForm1.uf_sendfile(as_filename: string);<br>var<br> &nbsp;li_actbyte: longint;<br> &nbsp;lb_tmpbuff: array[0..BLOCKSIZE - 1] of char;<br> &nbsp;I, j: integer;<br>begin<br> &nbsp;if ist_memstrem.Size = 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;ist_memstrem.LoadFromFile(as_filename);<br> &nbsp; &nbsp;il_leave := ist_memstrem.Size;<br> &nbsp; &nbsp;for j := 0 to (serversocket1.socket.ActiveConnections - 1) do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;serversocket1.Socket.Connections[j].SendText('003' + IntTOstr(il_leave));<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;if il_leave &gt; BLOCKSIZE then<br> &nbsp; &nbsp;li_actbyte := BLOCKSIZE<br> &nbsp;else li_actbyte := il_leave;<br> &nbsp;il_leave := il_leave - li_actbyte;<br> &nbsp;ist_memstrem.ReadBuffer(lb_tmpbuff, li_actbyte);<br> &nbsp;if il_leave = 0 then<br> &nbsp; &nbsp;ist_memstrem.Clear;<br> &nbsp;for I := 0 to (serversocket1.socket.ActiveConnections - 1) do<br> &nbsp;begin<br> &nbsp; &nbsp;serversocket1.Socket.Connections.SendBuf(lb_tmpbuff, li_actbyte);<br> &nbsp;end;<br>end;<br>我在本机上可以传完,可是在网络上只传第一个包,不传以后的,也就是都小于4K<br>文件在500k到10M 之间
 
////???????????????????????????????????????<br>请高手帮忙很急呀
 
没有人回答,请版本帮我收一下分,问题到此为止!谢谢<br>我通过其它途径完成了,相同的功能了!
 
不要用DELPHI的SOCKET控件,自己写,没有那么复杂,建立监听线程
 
后退
顶部