S
shenfeng_126
Unregistered / Unconfirmed
GUEST, unregistred user!
client端:负责发送文件,每10分钟自动到一个目录下就查,如果有文件就发送
server端:接收文件
client和server都是多进程的
我现在的程序虽然能传些文件了,但还有一些bug,比如文件传送完后关闭client程序的时候,会报句柄无效等错误,请兄弟们帮我看一下到底哪里有写错了
或者请哪位兄弟帮我优化一下代码,谢谢!!!!!!!11
我把主要的一些代码贴出来
client端代码:
procedure TClientThread.Execute;
begin
frmFilesRV.ClientSocket1.Open;
if FindFirst(JD.SENDDIR + '*.*', faAnyFile, sr) = 0 then
begin
repeat
if (sr.Attr and faDirectory)=0 then
begin
MyClientSockets := TWinsocketStream.Create(frmFilesRV.ClientSocket1.Socket,-1);
try
MyReadFile := TFileStream.Create(JD.SENDDIR + sr.Name,fmOpenRead);
//向server发送请求
.....................
while MyClientSockets.WaitForData(1000) do//等待server响应
begin
MyClientSockets.ReadBuffer(Res_Buf.MsgLen.MsgLen, 4);//读前4位
MyClientSockets.ReadBuffer( Res_Buf.Res_Msg, StrToInt(Trim(Res_Buf.MsgLen.MsgLen)) );
if server响应成功 then //向server请求成功
begin
file_len := MyReadFile.Read(FielBuffer, 1000);//每次读1000个字节
if (file_len <> 0) then //如果读取文件结束就发送结束报文
begin
//向server发送数据
.....................
end
else
begin
//向server发送文件结束报文
.................
MyReadFile.Free; //释放
end;
end
else server响应成功 then //server响应失败
begin
MyReadFile.Free;
end;
end;
except
MyReadFile.Free;
end;
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
server端程序:
procedure TServerThread.Execute;
var
........
begin
while not Terminated and ClientSocket.Connected do
Begin
try
SocketStream := TWinSocketStream.Create(ClientSocket, -1);
try
while SocketStream.WaitForData(1000) do
begin
SocketStream.ReadBuffer(Req_Buf.MsgLen.MsgLen, 4);
SocketStream.ReadBuffer(Req_Buf.Req_Msg, StrToInt(Trim(Req_Buf.MsgLen.MsgLen)) );
if Trim(Req_Buf.Req_Msg.cmd) = '0' then //0:文件名
begin
//接收client来的请求,server响应成功
..........
end
else if Trim(Req_Buf.Req_Msg.cmd) = '1' then //1:数据
begin
//接收client发来的数据,并响应成功
..........
end
else if Trim(Req_Buf.Req_Msg.cmd) = '2' then //2:文件结束
begin
// client发来文件结束请求报文,server响应成功
................
F.Free;
end;
end;
finally
end;
except
//发送响应错误报文
.................
Terminate;
end;
end;
end;
server端:接收文件
client和server都是多进程的
我现在的程序虽然能传些文件了,但还有一些bug,比如文件传送完后关闭client程序的时候,会报句柄无效等错误,请兄弟们帮我看一下到底哪里有写错了
或者请哪位兄弟帮我优化一下代码,谢谢!!!!!!!11
我把主要的一些代码贴出来
client端代码:
procedure TClientThread.Execute;
begin
frmFilesRV.ClientSocket1.Open;
if FindFirst(JD.SENDDIR + '*.*', faAnyFile, sr) = 0 then
begin
repeat
if (sr.Attr and faDirectory)=0 then
begin
MyClientSockets := TWinsocketStream.Create(frmFilesRV.ClientSocket1.Socket,-1);
try
MyReadFile := TFileStream.Create(JD.SENDDIR + sr.Name,fmOpenRead);
//向server发送请求
.....................
while MyClientSockets.WaitForData(1000) do//等待server响应
begin
MyClientSockets.ReadBuffer(Res_Buf.MsgLen.MsgLen, 4);//读前4位
MyClientSockets.ReadBuffer( Res_Buf.Res_Msg, StrToInt(Trim(Res_Buf.MsgLen.MsgLen)) );
if server响应成功 then //向server请求成功
begin
file_len := MyReadFile.Read(FielBuffer, 1000);//每次读1000个字节
if (file_len <> 0) then //如果读取文件结束就发送结束报文
begin
//向server发送数据
.....................
end
else
begin
//向server发送文件结束报文
.................
MyReadFile.Free; //释放
end;
end
else server响应成功 then //server响应失败
begin
MyReadFile.Free;
end;
end;
except
MyReadFile.Free;
end;
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
server端程序:
procedure TServerThread.Execute;
var
........
begin
while not Terminated and ClientSocket.Connected do
Begin
try
SocketStream := TWinSocketStream.Create(ClientSocket, -1);
try
while SocketStream.WaitForData(1000) do
begin
SocketStream.ReadBuffer(Req_Buf.MsgLen.MsgLen, 4);
SocketStream.ReadBuffer(Req_Buf.Req_Msg, StrToInt(Trim(Req_Buf.MsgLen.MsgLen)) );
if Trim(Req_Buf.Req_Msg.cmd) = '0' then //0:文件名
begin
//接收client来的请求,server响应成功
..........
end
else if Trim(Req_Buf.Req_Msg.cmd) = '1' then //1:数据
begin
//接收client发来的数据,并响应成功
..........
end
else if Trim(Req_Buf.Req_Msg.cmd) = '2' then //2:文件结束
begin
// client发来文件结束请求报文,server响应成功
................
F.Free;
end;
end;
finally
end;
except
//发送响应错误报文
.................
Terminate;
end;
end;
end;