谁来帮我弄一下,最好简短一点的,好的一定会加分的!

[复制链接]
查看11 | 回复0 | 2007-12-31 21:08:46 | 显示全部楼层 |阅读模式
一个简单的FTP程序,阻塞模式。DELPHI版。//客户端program ftpc;{$APPTYPE CONSOLE}usesWindows,Winsock,Classes,SysUtils;constrecv_port = 2000;send_port = 3000;max_filesize = 32*1024;typefiledata = record
ffname : array[0..29]of char;
ffdata : array[0..max_filesize-1] of char;
len : integer;end;varsock : TSOCKET;sa : SOCKADDR_IN;dp : filedata;f : TFileStream;wsd : TWSAData;s : string;i : integer;begin{ TODO -oUser -cConsole Main : Insert code here }if WSAStartup(makeword(2,2),wsd)0 thenbegin
writeln('sock initialize error.');
exit;end;sa.sin_family:=AF_INET;sa.sin_addr.S_addr:=inet_addr('127.0.0.1');sa.sin_port:=htons(recv_port);sock := socket(af_inet,sock_stream,0);if sock=socket_error thenbegin
writeln('create socket error.');
WSACleanup;
exit;end;while connect(sock,sa,sizeof(sa))=socket_error dowrite('connecting...'+#13);writeln;writeln('connected OK.');while true dobegin
writeln('input your file to send:(''exit'' to quit)');
readln(s);
if s='exit' then
begin
strpcopy(dp.ffdata,s);
strpcopy(dp.ffname,s);
dp.len:=12;
i :=send(sock,dp,12,0);
if i0 then begin messagebox(0,'fail to initialize winsocks.','information',mb_ok); exit; end; sock :=socket(af_inet,sock_stream,0); if sock = socket_error then begin messagebox(0,'fail to create socket.','information',mb_ok); WSACleanup; exit; end; sa.sin_family := af_inet; sa.sin_addr.S_addr:=htonl(inaddr_any); sa.sin_port:=htons(recv_port); if bind(sock,sa,sizeof(TSockAddrIn)) = socket_error then begin messagebox(0,'fail to bind socket.','information',mb_ok); exit; end; addrlen := sizeof(TSockAddrIn); if listen(sock,5) < 0 then begin messagebox(0,'fail to listen.','information',mb_ok); exit; end; //messagebox(0,'listening...','information',mb_ok); writeln('Listening...'); while true do begin
sock1 := accept(sock,@ca,@addrlen);
while true do
begin
zeromemory(@dp.ffname,30);
zeromemory(@dp.ffdata,max_filesize);
dp.len := 0;
if recv(sock1,dp,sizeof(dp),0) <= 0 then break;
s:=dp.ffname;
if s='exit' then
begin
WSACleanup;
exit;
end;
writeln('received file:'+dp.ffname+'length:'+inttostr(dp.len));
f := TFileStream.Create(dp.ffname,fmOpenWrite);
f.Write(dp.ffdata,dp.len);
f.Write('check ok',8);
f.Free;
end; end;end.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行