procedure TForm2.IdTCPServerExecute(AThread: TIdPeerThread);
var
FileName, sCommand: string;
FStream: TFileStream;
begin
sCommand := AThread.Connection.ReadLn();
FLogEntry := sCommand + ' 来自于主机 '
+ AThread.Connection.Socket.Binding.PeerIP;
AThread.Synchronize(AddLogEntry);
if AnsiStartsText('SEND ', sCommand) then
begin
FileName := EdtData.Text + RightStr(sCommand, Length(sCommand)-5);
if FileExists(FileName) then
DeleteFile(FileName);
try
FStream := TFileStream.Create(FileName, FmCreate);
AThread.Connection.ReadStream(FStream, 2048, true);
AThread.Connection.WriteLn('200: 数据接收胜利!');
FLogEntry := '上传的文件已保存 ' + sCommand;
AThread.Synchronize(AddLogEntry);
Finally
FStream.Free;
end;
end
else if SameText(sCommand, 'QUIT') then
begin
FCommand := 'QUIT';
FLogEntry := '断开同主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接!';
AThread.Synchronize(AddLogEntry);
AThread.Connection.Disconnect;
end
else
begin
FCommand := 'ERROR';
AThread.Connection.WriteLn('500: 无法识别的命令!');
FLogEntry := '无法识别命令:' + sCommand;
AThread.Synchronize(AddLogEntry);
end;//endif
end;
var
FileName, sCommand: string;
FStream: TFileStream;
begin
sCommand := AThread.Connection.ReadLn();
FLogEntry := sCommand + ' 来自于主机 '
+ AThread.Connection.Socket.Binding.PeerIP;
AThread.Synchronize(AddLogEntry);
if AnsiStartsText('SEND ', sCommand) then
begin
FileName := EdtData.Text + RightStr(sCommand, Length(sCommand)-5);
if FileExists(FileName) then
DeleteFile(FileName);
try
FStream := TFileStream.Create(FileName, FmCreate);
AThread.Connection.ReadStream(FStream, 2048, true);
AThread.Connection.WriteLn('200: 数据接收胜利!');
FLogEntry := '上传的文件已保存 ' + sCommand;
AThread.Synchronize(AddLogEntry);
Finally
FStream.Free;
end;
end
else if SameText(sCommand, 'QUIT') then
begin
FCommand := 'QUIT';
FLogEntry := '断开同主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接!';
AThread.Synchronize(AddLogEntry);
AThread.Connection.Disconnect;
end
else
begin
FCommand := 'ERROR';
AThread.Connection.WriteLn('500: 无法识别的命令!');
FLogEntry := '无法识别命令:' + sCommand;
AThread.Synchronize(AddLogEntry);
end;//endif
end;
procedure TForm2.IdTCPServerExecute(AThread: TIdPeerThread);
var
FileName, sCommand: string;
FStream: TFileStream;
size: integer;
begin
sCommand := AThread.Connection.ReadLn();
FLogEntry := sCommand + ' 来自于主机 '
+ AThread.Connection.Socket.Binding.PeerIP;
AThread.Synchronize(AddLogEntry);
if AnsiStartsText('SEND ', sCommand) then
begin
FCommand := 'BEGIN';
FileName := EdtData.Text + RightStr(sCommand, Length(sCommand)-5);
if FileExists(FileName) then
DeleteFile(FileName);
try
size := AThread.Connection.ReadInteger;
FStream := TFileStream.Create(FileName, FmCreate);
AThread.Connection.ReadStream(FStream, size);
AThread.Connection.WriteLn('200: 数据接收胜利!');
FLogEntry := '上传的文件已保存 ' + sCommand;
AThread.Synchronize(AddLogEntry);
Finally
FStream.Free;
end;
end
else
if SameText(sCommand, 'QUIT') then
begin
FCommand := 'QUIT';
FLogEntry := '断开同主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接!';
AThread.Synchronize(AddLogEntry);
AThread.Connection.Disconnect;
end
else
begin
FCommand := 'ERROR';
AThread.Connection.WriteLn('500: 无法识别的命令!');
FLogEntry := '无法识别命令:' + sCommand;
AThread.Synchronize(AddLogEntry);
end;//endif
end;
procedure TForm1.BtnSendClick(Sender: TObject);
var
FileName: string;
FStream:TFileStream;
begin
FileName := EdtData.Text;
if IdTcpClient.Connected then
begin
LbLog.Items.Add('正在发送图片 ' + FileName);
IdTcpClient.WriteLn('SEND ' + ExtractFileName(FileName));
if FileExists(Filename) then
begin
try
FStream := TFileStream.Create(FileName, FmOpenRead);
FStream.Position := 0;
FStream.Seek(0, 0);
IdTcpClient.WriteInteger(FStream.Size);
IdTcpClient.WriteStream(FStream, True, False);
LbLog.Items.Add(IdTcpClient.ReadLn());
finally
FStream.Free;
end;//end try
end else MessageBox(Handle,'文件不存在!','Error',MB_OK);
end else begin
MessageBox(Handle,'没有连接服务器','Error',MB_Ok);
LbLog.Items.Add('发送文件失败!');
IdTCPClient.Disconnect();
LbLog.Items.Add('同主机 ' + EdtHost.Text + ' 的连接已断开!');
BtnConnect.Enabled := True;
BtnSend.Enabled := False;
BtnDisconnect.Enabled := False;
end;
end;