//司马华鹏
// 服务器端
//------------------------------SocketTrans事件---------------------------------
procedure TFrmMain.SocketTransAccept(Sender: TObject;Socket: TCustomWinSocket);
var
Item: TListItem;
Idx: Integer;
begin
Item := listView.FindCaption(1,Socket.RemoteAddress,False,True,True);
if Item = nil then
begin
with listView.Items.Add do
begin
Caption := Socket.RemoteAddress;
SubItems.Add('在线');
SubItems.Add('等待传输');
end;
end else
begin
with Item do
begin
SubItems[1]:='等待传输';
end;
end;
// 联接成功则删除
Idx := ListFileTrans.IndexOf(Socket.RemoteAddress);
if Idx >= 0 then ListFileTrans.Delete(Idx);
end;
//------------------------------------------------------------------------------
// 文件传输部分登陆成功
procedure TFrmMain.SocketTransClientConnect(Sender: TObject;Socket: TCustomWinSocket);
var
Item: TListItem;
begin
Item := listView.FindCaption(1,Socket.RemoteAddress,False,True,True);
if Item = nil then
begin
with listView.Items.Add do
begin
Caption := Socket.RemoteAddress;
SubItems.Add('在线');
SubItems.Add('等待传输');
end;
end else
begin
with Item do
begin
SubItems[1]:='等待传输';
end;
end;
end;
//------------------------------------------------------------------------------
procedure TFrmMain.SocketTransClientDisconnect(Sender: TObject;Socket: TCustomWinSocket);
var
Item:TListItem;
begin
//找到地址相同的机器,然后修改其传输状态
Item := listView.FindCaption(1,Socket.RemoteAddress,False,True,True);
if Item <> nil then
begin // 如果找到
with Item do
begin
SubItems[1] := '结束传输'; // 显示传输状态
end;
end;
end;
//------------------------------------------------------------------------------
procedure TFrmMain.SocketTransClientError(Sender: TObject;Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;var ErrorCode: Integer);
var
Item:TListItem;
begin
//找到地址相同的机器,然后修改其传输状态
Item := listView.FindCaption(1,Socket.RemoteAddress,False,True,True);
if Item <> nil then
begin // 如果找到
with Item do
begin
SubItems[1] := '传输结束,网络故障:' + fcTransNetErrString(ErrorCode);
end;
end;
ErrorCode := 0;
end;
{ TDbServerThread }
//------------------------------------------------------------------------------
// 文件传输执行线程
procedure TDbServerThread.ClientExecute;
var
Stream: TWinSocketStream;
Buffer, strIn: string;
nRead: Integer;
nFileSize:integer;
Item:TListItem;
begin
inherited FreeOnTerminate := TRUE; // 关闭后自动释放
Stream := TWinSocketStream.Create(ClientSocket, 6000); // 6000为超时限制时间
try
while not Terminated and ClientSocket.Connected do
begin // 初始化,读取消息
Buffer := '';
strIn := '';
SetLength(Buffer, 64);
repeat
nRead := Stream.Read(Buffer[1], 64);
if nRead = 0 then
begin
ClientSocket.Close;
Break;
end;
SetLength (Buffer, nRead);
StrIn := StrIn + Buffer;
until (Pos(#10#13'.'#10#13, Buffer) > 0);
if strIn = '' then // 没有请求
Continue // 继续循环
else begin // 处理请求
StrCommand := Copy (strIn, 1, Pos (#10#13'.'#10#13, strIn) -1);
Case StrCommand[1] of
fp_FileProperty:
begin {1} // 客户要求得到文件属性
Item:=frmMain.ListView.FindCaption(1,ClientSocket.RemoteAddress,False,True,True);
if Item<>nil then
begin
Item.SubItems[1]:='请求文件信息'; // 显示传输状态
end;
nFileSize:=fcGetFileSize(strSourceFile);
if bSendFile then
strFeedback:= fp_FileProperty + '0' + Inttostr(nFileSize) + '*' + strDestFile
else
strFeedback:= fp_FileProperty + '1' + Inttostr(nFileSize) + '*' + strDestFile;
Stream.Write(strFeedback[1], Length (strFeedback)); //应答
end;
fp_FileReady:
begin {2} // 客户准备完毕,可以发送
Item:=frmMain.ListView.FindCaption(1,ClientSocket.RemoteAddress,False,True,True);
if Item<>nil then
begin
Item.SubItems[1]:='正在接收...';
end;
ClientSocket.SendStream(TFileStream.Create(strSourceFile, fmOpenRead or fmShareDenyNone));
end;
fP_FileEnd:
begin {3} // 客户接收完毕,检测完成状态
Item:=frmMain.ListView.FindCaption(1,ClientSocket.RemoteAddress,False,True,True);
if Item<>nil then
begin
Item.SubItems[1]:='接收完毕';
end;
ClientSocket.Close;
Terminate;
end;
fP_FileABORT:
begin {4} // 客户要求中断传输
Item:=frmMain.ListView.FindCaption(1,ClientSocket.RemoteAddress,False,True,True);
if Item<>nil then
begin
Item.SubItems[1]:='传输因意外中断';
end;
ClientSocket.Close;
Terminate;
end;
end;
end;
end; // 在循环中获取消息
finally
Stream.Free;
end;
end;
//------------------------------------------------------------------------------
// 接收文件线程创建
procedure TFrmMain.SocketTransGetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
ClientSocket.SendText(fP_FileSend); //发送一个文件发送信号
SocketThread := TDbServerThread.Create(False, ClientSocket);
end;