我用实现文件的上传和下载,用indy的TIdTCPServer作为服务器,Tidtcpclient作为客户端。为什么同时只能有一个客户端上传文件,其它的客户端连接不上服务器呢?请各位多多指教。程序如下:procedure TForm1.ftpserverexecute(AThread: TIdPeerThread);
var
ReadFile: TMemoryStream;
SendFile: TFileStream;
RecevFileName,SendFileName,ClientMsg: string;
i:integer;
F:TextFile;
s:string;
begin
if fileExists('Mylog.txt')then
begin
AssignFile(F, 'Mylog.txt');
append(F);
end
else
begin
AssignFile(F, 'Mylog.txt');
rewrite(F);
end;
ClientMsg:=Athread.Connection.readln('',-2);
if trim(ClientMsg)='upload...' then //接收文件
begin
try
ReadFile := TMemoryStream.Create;//创建一个TMemoryStram对象
RecevFileName :=Athread.Connection.ReadLn;//从服务器端获得文件名
s:= Athread.Connection.ReadLn;
s:=datetimetostr(now())+' > Receving file '+RecevFileName+' from '+s;
Writeln(F, S);
lstMain.Items.Add(s);//显示到ListView中
RecevFileName:=CurDirectory+ExtractFileName(RecevFileName);
Athread.Connection.ReadStream(ReadFile, -1, True);//以流模式从客户器端获得文件内容,参数分别是ReadFile表示流对象,-1表示一直读取直到对方断开,True表示在NT操作系统下提高性能,对9x无效
s:='Total ' + IntToStr(ReadFile.Size) + ' Bytes recevied';
Writeln(F,s);
lstMain.items.Add(s);//显示总共接收到多少数据 ReadFile.Seek(0, soFromBeginning);//定位流指针到开始 ReadFile.SaveToFile(RecevFileName);//把流中的内容保存到文件中去
s:='save file to ' + RecevFileName;
Writeln(F,s);
writeln(F);
lstMain.Items.Add(s);//显示到ListView中
ReadFile.Free;//释放流对象
Athread.Connection.Disconnect;//断开连接
except
Athread.Connection.Disconnect;//断开连接
s:=datetimetostr(now())+': 文件传送失败!';
lstMain.items.Add(s);
writeln(F,s);
ReadFile.Free;//释放流对象
end;
end
else if trim(ClientMsg)='download...' then //发送文件
begin
try
SendFileName :=Athread.Connection.ReadLn;//从服务器端获得文件名
s:=Athread.Connection.ReadLn;
SendFileName:=CurDirectory+ExtractFileName(SendFileName);
s:=datetimetostr(now())+' > Send file ' + SendFileName+' to '+s;
Writeln(F,s);
lstMain.Items.Add(s);//显示到ListView中 SendFile := TFileStream.Create(SendFileName, fmOpenRead);//创建一个TFileStream,打开我们要发送的文件
Athread.Connection.WriteStream(SendFile);//使用流模式把文件发送到客户端
Athread.Connection.Disconnect;//断开连接
SendFile.Free;//释放流对象
writeln(F);
except
Athread.Connection.Disconnect;//断开连接
s:=datetimetostr(now())+': 文件接收失败!';
Writeln(F,s);
lstMain.items.Add(s);
ReadFile.Free;//释放流对象
end;
end
else if trim(ClientMsg)='delete...' then //删除服务器上的文件
begin
try
RecevFileName :=Athread.Connection.ReadLn;//从服务器端获得文件名
s:=Athread.Connection.ReadLn;
RecevFileName:=CurDirectory+ExtractFileName(RecevFileName);
DeleteFile(RecevFileName);
Athread.Connection.Disconnect;//断开连接
s:=datetimetostr(now())+' > 删除文件'+RecevFileName+' from '+s+'!';
Writeln(F,s);
lstMain.items.Add(s);
writeln(F);
except
Athread.Connection.Disconnect;//断开连接
s:=datetimetostr(now())+': 删除文件失败!';
Writeln(F,s);
lstMain.items.Add(s);
end;
end;
CloseFile(F);
end;
var
ReadFile: TMemoryStream;
SendFile: TFileStream;
RecevFileName,SendFileName,ClientMsg: string;
i:integer;
F:TextFile;
s:string;
begin
if fileExists('Mylog.txt')then
begin
AssignFile(F, 'Mylog.txt');
append(F);
end
else
begin
AssignFile(F, 'Mylog.txt');
rewrite(F);
end;
ClientMsg:=Athread.Connection.readln('',-2);
if trim(ClientMsg)='upload...' then //接收文件
begin
try
ReadFile := TMemoryStream.Create;//创建一个TMemoryStram对象
RecevFileName :=Athread.Connection.ReadLn;//从服务器端获得文件名
s:= Athread.Connection.ReadLn;
s:=datetimetostr(now())+' > Receving file '+RecevFileName+' from '+s;
Writeln(F, S);
lstMain.Items.Add(s);//显示到ListView中
RecevFileName:=CurDirectory+ExtractFileName(RecevFileName);
Athread.Connection.ReadStream(ReadFile, -1, True);//以流模式从客户器端获得文件内容,参数分别是ReadFile表示流对象,-1表示一直读取直到对方断开,True表示在NT操作系统下提高性能,对9x无效
s:='Total ' + IntToStr(ReadFile.Size) + ' Bytes recevied';
Writeln(F,s);
lstMain.items.Add(s);//显示总共接收到多少数据 ReadFile.Seek(0, soFromBeginning);//定位流指针到开始 ReadFile.SaveToFile(RecevFileName);//把流中的内容保存到文件中去
s:='save file to ' + RecevFileName;
Writeln(F,s);
writeln(F);
lstMain.Items.Add(s);//显示到ListView中
ReadFile.Free;//释放流对象
Athread.Connection.Disconnect;//断开连接
except
Athread.Connection.Disconnect;//断开连接
s:=datetimetostr(now())+': 文件传送失败!';
lstMain.items.Add(s);
writeln(F,s);
ReadFile.Free;//释放流对象
end;
end
else if trim(ClientMsg)='download...' then //发送文件
begin
try
SendFileName :=Athread.Connection.ReadLn;//从服务器端获得文件名
s:=Athread.Connection.ReadLn;
SendFileName:=CurDirectory+ExtractFileName(SendFileName);
s:=datetimetostr(now())+' > Send file ' + SendFileName+' to '+s;
Writeln(F,s);
lstMain.Items.Add(s);//显示到ListView中 SendFile := TFileStream.Create(SendFileName, fmOpenRead);//创建一个TFileStream,打开我们要发送的文件
Athread.Connection.WriteStream(SendFile);//使用流模式把文件发送到客户端
Athread.Connection.Disconnect;//断开连接
SendFile.Free;//释放流对象
writeln(F);
except
Athread.Connection.Disconnect;//断开连接
s:=datetimetostr(now())+': 文件接收失败!';
Writeln(F,s);
lstMain.items.Add(s);
ReadFile.Free;//释放流对象
end;
end
else if trim(ClientMsg)='delete...' then //删除服务器上的文件
begin
try
RecevFileName :=Athread.Connection.ReadLn;//从服务器端获得文件名
s:=Athread.Connection.ReadLn;
RecevFileName:=CurDirectory+ExtractFileName(RecevFileName);
DeleteFile(RecevFileName);
Athread.Connection.Disconnect;//断开连接
s:=datetimetostr(now())+' > 删除文件'+RecevFileName+' from '+s+'!';
Writeln(F,s);
lstMain.items.Add(s);
writeln(F);
except
Athread.Connection.Disconnect;//断开连接
s:=datetimetostr(now())+': 删除文件失败!';
Writeln(F,s);
lstMain.items.Add(s);
end;
end;
CloseFile(F);
end;
procedure TjhjlForm.uploadClick(Sender: TObject);
var
SendFileName,s:string;
SendFile: TFileStream;
today:string;
begin
if fjlb.Items.Count>2 then
begin
MessageDlg('最多只能上传三个附件',mtError,[mbOK],0);
exit;
end;today:=AnsiReplaceText(datetimetostr(now()),':','');
today:=AnsiReplaceText(today,'-','');
today:=AnsiReplaceText(today,' ','');upload.Enabled:=false;
open.Enabled:=false;
try
Tcpclient.Connect();
s:='upload...';
tcpclient.WriteLn(s);
SendFileName:=trim(filename.Text);
SendFile := TFileStream.Create(SendFileName, fmOpenRead);//创建一个TFileStream,打开我们要发送的文件
SendFileName:=today+ExtractFileName(SendFileName);
tcpclient.WriteLn(SendFileName);//把文件名发送给服务器
tcpclient.WriteLn(Mainform.yhm);//用户名
Tcpclient.WriteStream(SendFile);//使用流模式把文件发送
tcpclient.Disconnect;
SendFile.Free;
fjlb.Items.Add(SendFileName);
fjlb.ItemIndex:=0;
open.Enabled:=true;
del.Enabled:=true;
except
application.MessageBox('连接服务器失败!','提示',MB_OK);
end;