Z
zengmiao
Unregistered / Unconfirmed
GUEST, unregistred user!
//这是我写的一个文件传输的模型
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
FileName, RecvTemp: string;
RecvBuf: pointer;
ReceiveLength, uCmd: integer;
begin
ioctlsocket(ClientSocket.Handle, FIONREAD, ReceiveLength);
GetMem(RecvBuf, ReceiveLength);
Try
ClientSocket.ReceiveBuf(RecvBuf^, ReceiveLength);
RecvTemp := StrPas(PChar(RecvBuf));
If (Length(RecvTemp) >= 8) And (Copy(RecvTemp, 1,7) = 'CT $000') Then
uCmd := StrToInt(Copy(RecvTemp, 4, 5))
else
uCmd := $FFFF;
Case uCmd of
$0000: {File Property}
begin
//接收文件长度等信息
end;
$0001: {Query}
Try
FileName := ExtractFileName(TrimRight(RecvTemp));
RecvFile := TFileStream.Create('C:/Temp/' + FileName, fmCreate);
// ClientSocket.Sendln('CT $0003');
Except
// ClientSocket.Sendln('CT $0002');
end;
$0002: {Refuse};
//拒绝接收
$0003: {Accept}
begin
//接受请求
end;
$0004: ;{Next will be data}
// ClientSocket.Sendln('CT $0005');
$0005: {Data}
begin
end;
$0006: {Abort}
RecvFile.Free;
$0007: {Over}
begin
end;
$0008: {Chat text}
begin
end;
$0009: {end}
RecvFile.Free;
else
RecvFile.WriteBuffer(RecvBuf^, ReceiveLength);
end;
Finally
FreeMem(RecvBuf, ReceiveLength);
end;
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var
FileName, RecvTemp: string;
RecvBuf: pointer;
ReceiveLength, uCmd: integer;
begin
ioctlsocket(ClientSocket.Handle, FIONREAD, ReceiveLength);
GetMem(RecvBuf, ReceiveLength);
Try
ClientSocket.ReceiveBuf(RecvBuf^, ReceiveLength);
RecvTemp := StrPas(PChar(RecvBuf));
If (Length(RecvTemp) >= 8) And (Copy(RecvTemp, 1,7) = 'CT $000') Then
uCmd := StrToInt(Copy(RecvTemp, 4, 5))
else
uCmd := $FFFF;
Case uCmd of
$0000: {File Property}
begin
//接收文件长度等信息
end;
$0001: {Query}
Try
FileName := ExtractFileName(TrimRight(RecvTemp));
RecvFile := TFileStream.Create('C:/Temp/' + FileName, fmCreate);
// ClientSocket.Sendln('CT $0003');
Except
// ClientSocket.Sendln('CT $0002');
end;
$0002: {Refuse};
//拒绝接收
$0003: {Accept}
begin
//接受请求
end;
$0004: ;{Next will be data}
// ClientSocket.Sendln('CT $0005');
$0005: {Data}
begin
end;
$0006: {Abort}
RecvFile.Free;
$0007: {Over}
begin
end;
$0008: {Chat text}
begin
end;
$0009: {end}
RecvFile.Free;
else
RecvFile.WriteBuffer(RecvBuf^, ReceiveLength);
end;
Finally
FreeMem(RecvBuf, ReceiveLength);
end;
end;