本文以實(shí)例形式講述了Delphi下木馬的文件傳輸方法的實(shí)現(xiàn)過(guò)程,具體步驟如下:
服務(wù)器端代碼:
unit ServerFrm;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, ExtCtrls,WinSock;type TfrmMain = class(TForm) Panel1: TPanel; Label1: TLabel; edtPort: TEdit; Panel2: TPanel; stabar: TStatusBar; SaveDialog: TSaveDialog; btnListen: TButton; btnReceive: TButton; btnStop: TButton; btnExit: TButton; procedure FormCreate(Sender: TObject); procedure btnExitClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure btnListenClick(Sender: TObject); procedure btnReceiveClick(Sender: TObject); procedure btnStopClick(Sender: TObject); private { Private declarations } public { Public declarations } StopTrans:Boolean; //是否停止傳送開(kāi)關(guān) InTrans:Boolean; //表示正在接收文件 Server:TSocket; //定義服務(wù)器端的socket句柄 //自定義過(guò)程接收文件 procedure RecvFile(FileName:String); end;var frmMain: TfrmMain;const BlockLen=1024*4;implementation{$R *.dfm}procedure tfrmmain.RecvFile(FileName:String);var Ftrans:file of Byte; Recelen:Integer; Blockbuf:array[0..BlockLen-1] of Byte; RecvSocket:TSocket; ra:Sockaddr_in; ra_len:integer;begin ra_len:=sizeof(ra); Recvsocket:=accept(server,@ra,@ra_len); assignFile(Ftrans,filename); rewrite(ftrans); stoptrans:=false; intrans:=true; recelen:=recv(recvsocket,Blockbuf,BlockLen,0); while (recelen>0) and (not StopTrans) do begin BlockWrite(Ftrans,Blockbuf[0],BlockLen); application.ProcessMessages; recelen:=recv(recvsocket,Blockbuf,Blocklen,0); if stoptrans then begin CloseFile(Ftrans); CloseSocket(RecvSocket); InTrans:=False; MessageBox(Handle,'停止傳輸!','提示',MB_OK); EXIT; END; END; //關(guān)閉文件,接收的SOCKET CloseFile(Ftrans); Closesocket(recvsocket); InTrans:=False; if (Recelen=SOCKET_ERROR) then messagebox(handle,'傳輸異常終止!','提示',MB_OK) ELSE MESSAGEBOX(HANDLE,'客戶端已經(jīng)關(guān)閉連接1,文件可能已經(jīng)傳送完畢了!','提示',MB_OK);end; procedure TfrmMain.FormCreate(Sender: TObject);var aWSAData:TWSAData;begin if WSAStartup($0101,aWSAData)<>0 then raise Exception.Create('不能啟動(dòng)WinSock動(dòng)態(tài)鏈接庫(kù)'); messageBox(Handle,aWSAdata.szDescription ,'WinSock動(dòng)態(tài)鏈接庫(kù)版本',mb_ok);end;procedure TfrmMain.btnExitClick(Sender: TObject);begin Close;end;procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);begin if InTrans then if MessageBox(handle,'正在接收文件,停止嗎?','提示',MB_YESNO)=IDNO then abort; IF SERVER<>INVALID_SOCKET THEN CLOSESOCKET(SERVER); //釋放winsock動(dòng)態(tài)鏈接庫(kù)所創(chuàng)建的資源 if WSACleanup<>0 then messagebox(handle,'清除Winsock動(dòng)態(tài)鏈接庫(kù)錯(cuò)誤!','提示',MB_OK) ELSE messagebox(handle,'清除Winsock動(dòng)態(tài)鏈接庫(kù)成功!','提示',MB_OK);end;procedure TfrmMain.btnListenClick(Sender: TObject);var ca:SOCKADDR_IN;begin //創(chuàng)建服務(wù)器端SOCKET Server:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP); IF server=invalid_socket then begin stabar.SimpleText :='創(chuàng)建接收SOCKET錯(cuò)誤1'; exit; end; //綁定服務(wù)器端SOCKET ca.sin_family :=PF_INET; CA.sin_port :=htons(strtoint(trim(edtPort.Text ))); ca.sin_addr.S_addr :=INADDR_ANY; if bind(server,ca,sizeof(ca))=socket_error then begin stabar.SimpleText :='綁定socket錯(cuò)誤,請(qǐng)更改接收端口'; closeSocket(server); exit; end else stabar.SimpleText :='綁定接收端socket成功!'; //開(kāi)始監(jiān)聽(tīng) listen(server,5); btnlisten.Enabled :=False; btnstop.Enabled :=true;end;procedure TfrmMain.btnReceiveClick(Sender: TObject);begin if (server=INVALID_SOCKET) THEN BEGIN MESSAGEBOX(HANDLE,'還沒(méi)有進(jìn)行監(jiān)聽(tīng),請(qǐng)先進(jìn)行監(jiān)聽(tīng)!','提示',MB_OK); EXIT; END; IF SaveDialog.Execute THEN RECVFILE(SaveDialog.FileName ); end;procedure TfrmMain.btnStopClick(Sender: TObject);begin STOPTRANS:=TRUE; IF SERVER<>INVALID_SOCKET THEN cLOSESOCKET(SERVER); //此處需要說(shuō)明 server:=INVALID_SOCKET; bTNSTOP.Enabled :=fALSE; BTNlISTEN.Enabled :=TRUE;end;end.
客戶端代碼:
unit ClientFrm;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls,WinSock;type TfrmMain = class(TForm) opendfile: TOpenDialog; Label1: TLabel; edtIP: TEdit; Label2: TLabel; edtPort: TEdit; StatusBar: TStatusBar; btnConnect: TButton; btnSend: TButton; btnStop: TButton; btnExit: TButton; ProgressBar: TProgressBar; procedure FormCreate(Sender: TObject); procedure btnExitClick(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure btnConnectClick(Sender: TObject); procedure btnSendClick(Sender: TObject); procedure btnStopClick(Sender: TObject); private { Private declarations } Client:TSocket; public { Public declarations } StopTrans:Boolean; //是否停止發(fā)送的開(kāi)發(fā) InTrans:Boolean; //表示是否正在傳送文件 procedure TransFile(FileName:String); //傳遞文件的過(guò)程 end; const BlockLen=1024*4; //每次發(fā)送的最大數(shù)據(jù)量var frmMain: TfrmMain;implementation{$R *.dfm}procedure TfrmMain.TransFile(FileName:String); //傳遞文件的過(guò)程var Ftrans:file of Byte; Flen:integer; BlockNum,RemainLen:integer; BlockBuf:array[0..BlockLen-1] of Byte; i:integer; SendLen:Integer;begin assignFile(Ftrans,filename); reset(Ftrans); Flen:=FileSize(Ftrans); BlockNum:=Flen div BlockLen; progressBar.Max :=1+BlockNum; RemainLen:=Flen mod BlockLen; StopTrans:=False; InTrans:=True; SendLen:=1; for i:=0 to BlockNum-1 do begin if (StopTrans) or (SendLen<=0) then Break; BlockRead(Ftrans,Blockbuf[0],BlockLen); SendLen:=Send(Client,Blockbuf,BlockLen,0); ProgressBar.Position :=i; Application.processMessages; end; if StopTrans then begin CloseFile(Ftrans); InTrans:=False; StatusBar.SimpleText :=''; MessageBox(Handle,'停止傳輸!','提示',mb_ok); progressbar.Position :=0; exit; end; if (SendLen<=0) then begin CloseFile(Ftrans); InTrans:=False; StatusBar.SimpleText :=''; messagebox(handle,'傳出異常終止!','提示',MB_OK); progressBar.Position :=0; exit; end; if remainLen>0 then begin BlockRead(Ftrans,BlockBuf[0],RemainLen); SendLen:=send(client,BlockBuf,Remainlen,0); if (sendLen<=0) then begin closeFile(Ftrans); InTrans:=False; StatusBar.SimpleText :=''; messagebox(handle,'傳輸異常終止!','提示',mb_ok); progressBar.Position :=0; exit; end; end; progressBar.Position :=ProgressBar.Max ; CloseFile(Ftrans); InTrans:=False; StatusBar.SimpleText :=''; messagebox(handle,'傳輸完成!','提示',mb_ok); progressbar.Position :=0;end;procedure TfrmMain.FormCreate(Sender: TObject);var aWSAData:TWSAData;begin if WSAStartup($0101,aWSAData)<>0 then raise Exception.Create('不能啟動(dòng)WinSock動(dòng)態(tài)鏈接庫(kù)'); messageBox(Handle,aWSAdata.szDescription ,'WinSock動(dòng)態(tài)鏈接庫(kù)版本',mb_ok);end;procedure TfrmMain.btnExitClick(Sender: TObject);begin Close;end;procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);begin if InTrans then if MessageBox(handle,'正在傳輸文件,停止嗎?','提示',MB_YESNO)=IDNO then abort; //釋放winsock動(dòng)態(tài)鏈接庫(kù)所創(chuàng)建的資源 if WSACleanup<>0 then messagebox(handle,'清除Winsock動(dòng)態(tài)鏈接庫(kù)錯(cuò)誤!','提示',MB_OK) ELSE messagebox(handle,'清除Winsock動(dòng)態(tài)鏈接庫(kù)成功!','提示',MB_OK); CloseSocket(Client);end;procedure TfrmMain.btnConnectClick(Sender: TObject);var ca:SOCKADDR_IN; hostaddr:u_long;begin Client:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP); IF CLIENT=INVALID_SOCKET THEN BEGIN StatusBar.SimpleText :='為連接遠(yuǎn)程服務(wù)器端創(chuàng)建COSKET錯(cuò)誤!'; exit; end; ca.sin_family :=PF_INET; CA.sin_port :=HTONS(STRTOINT(TRIM(EDTpORT.Text ))); HOSTADDR:=INET_ADDR(PCHAR(TRIM(EDTIP.Text ))); //判斷IP是否合法 if (hostaddr= -1) then begin StatusBar.SimpleText :='主機(jī)IP地址:'+trim(edtip.Text )+'錯(cuò)誤'; exit; end else ca.sin_addr.S_addr :=hostaddr; //連接服務(wù)器 if connect(Client,ca,sizeof(ca))<>0 then begin StatusBar.SimpleText :='連接服務(wù)器端SOCKET錯(cuò)誤!'; exit; end else StatusBar.SimpleText :='連接遠(yuǎn)程SOCKET成功!';end;procedure TfrmMain.btnSendClick(Sender: TObject);begin if (opendfile.Execute ) and (FileExists(opendfile.FileName )) then transFile(opendfile.FileName );end;procedure TfrmMain.btnStopClick(Sender: TObject);begin Stoptrans:=True;end;end.
新聞熱點(diǎn)
疑難解答
圖片精選