阿
阿伟~
Unregistered / Unconfirmed
GUEST, unregistred user!
写了个简单的例子,没用多线程处理,但始终存在2个问题:
发送端,发送代码:
procedure Tf_main.TransFile(filename: string);
var
Ftrans: file of Byte;
Flen: Integer;
BlockNum, RemainLen: Integer;
BlockBuf: array[0..BlockLen - 1] of Byte;
i: Integer;
SendLen: Integer;
begin
AssignFile(Ftrans, filename);
Reset(Ftrans);
Flen := FileSize(Ftrans);
BlockNum := Flen div BlockLen;
ProgressBar.Max := 1 + BlockNum;
RemainLen := Flen mod BlockLen;
StopTrans := False;
InTrans := True;
SendLen := 1;
for i := 0 to BlockNum - 1 do
begin
if (StopTrans) or (SendLen <= 0) then Break;
BlockRead(Ftrans, BlockBuf[0], BlockLen);
SendLen := send(Client, BlockBuf, BlockLen, 0);//问题1
ProgressBar.Position := i;
Application.ProcessMessages;
end;
if StopTrans then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '停止传输!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
if (SendLen <= 0) then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输异常终止!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
if RemainLen > 0 then //剩下不到4096长度的数据发送
begin
BlockRead(Ftrans, BlockBuf[0], RemainLen);
SendLen := send(Client, BlockBuf, RemainLen, 0);
if (SendLen <= 0) then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输异常终止!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
end;
ProgressBar.Position := ProgressBar.Max;
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输完成!', '提示', MB_OK);
ProgressBar.Position := 0;
end;
接收端代码:
procedure Tf_main.RecvFile(filename: string);
var
Ftrans: file of Byte;
Recelen, i: Integer;
BlockBuf: array[0..4096 * 10] of Byte;
RecvSocket: TSocket;
ra: SOCKADDR_IN;
ra_len: Integer;
Rece_Max: Integer;
begin
Rece_Max := 0;
Ra_len := sizeof(Ra);
//等待连接的客户端Socket
RecvSocket := accept(Server, @ra, @ra_len);
//创建一个保存的文件
AssignFile(Ftrans, filename);
ReWrite(Ftrans);
//设置状态变量
StopTrans := False;
InTrans := True;
//接收数据
Recelen := recv(RecvSocket, BlockBuf, BlockLen, 0);
while (Recelen > 0) and (not StopTrans) and (Rece_Max <> 15195) do
begin
i := 15195 - Rece_Max;
if i >= BlockLen then
i := BlockLen;
BlockWrite(Ftrans, BlockBuf[0], Recelen);
Application.ProcessMessages;
Recelen := recv(RecvSocket, BlockBuf, i, 0);//问题2
Rece_Max := Rece_Max + Recelen;
//当停止接收时,停止传输
if StopTrans then
begin
CloseFile(Ftrans);
closesocket(RecvSocket);
InTrans := False;
MessageBox(Handle, '停止传输!', '提示', MB_OK);
Exit;
end;
end;
//关闭文件,接收的Socket
CloseFile(Ftrans);
closesocket(RecvSocket);
InTrans := False;
if (Recelen = SOCKET_ERROR) then
MessageBox(Handle, '传输异常终止!', '提示', MB_OK)
else
MessageBox(Handle, '客户端已经关闭连接!文件可能已经传送完毕!', '提示', MB_OK);
end;
发送端有问题1,接收端有问题2
问题1:是当我在发送大容量的文件时,在发送端SEND处(也就是问题1的地方)按事先划好的文件块发到一定大小的时候会停在那,无法动弹,也没任何返回值包括错误的,而且我是规定文件块每次发送的大小为4K,但我发送较小的文件时却没这样的情况,我分别试发了16K和50K的文件,16K的没问题,50K的有问题,为什么会这样呢?
问题2:在接收文件时,我按4K一次的容量接收,在剩余接受数据大小小于4K时,用剩余实际大小接受,但为什么总在接受文件最后一块的时候,在RECV处(也就是问题2)处挂的那里,什么返回值也没有,就跟程序死了一样,但当我强行终止程序时,发现接受的文件却是对的而且可以打开,这又是为什么呢?
请各位大佬帮忙看看
发送端,发送代码:
procedure Tf_main.TransFile(filename: string);
var
Ftrans: file of Byte;
Flen: Integer;
BlockNum, RemainLen: Integer;
BlockBuf: array[0..BlockLen - 1] of Byte;
i: Integer;
SendLen: Integer;
begin
AssignFile(Ftrans, filename);
Reset(Ftrans);
Flen := FileSize(Ftrans);
BlockNum := Flen div BlockLen;
ProgressBar.Max := 1 + BlockNum;
RemainLen := Flen mod BlockLen;
StopTrans := False;
InTrans := True;
SendLen := 1;
for i := 0 to BlockNum - 1 do
begin
if (StopTrans) or (SendLen <= 0) then Break;
BlockRead(Ftrans, BlockBuf[0], BlockLen);
SendLen := send(Client, BlockBuf, BlockLen, 0);//问题1
ProgressBar.Position := i;
Application.ProcessMessages;
end;
if StopTrans then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '停止传输!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
if (SendLen <= 0) then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输异常终止!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
if RemainLen > 0 then //剩下不到4096长度的数据发送
begin
BlockRead(Ftrans, BlockBuf[0], RemainLen);
SendLen := send(Client, BlockBuf, RemainLen, 0);
if (SendLen <= 0) then
begin
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输异常终止!', '提示', MB_OK);
ProgressBar.Position := 0;
Exit;
end;
end;
ProgressBar.Position := ProgressBar.Max;
CloseFile(Ftrans);
InTrans := False;
StatusBar.SimpleText := '';
MessageBox(Handle, '传输完成!', '提示', MB_OK);
ProgressBar.Position := 0;
end;
接收端代码:
procedure Tf_main.RecvFile(filename: string);
var
Ftrans: file of Byte;
Recelen, i: Integer;
BlockBuf: array[0..4096 * 10] of Byte;
RecvSocket: TSocket;
ra: SOCKADDR_IN;
ra_len: Integer;
Rece_Max: Integer;
begin
Rece_Max := 0;
Ra_len := sizeof(Ra);
//等待连接的客户端Socket
RecvSocket := accept(Server, @ra, @ra_len);
//创建一个保存的文件
AssignFile(Ftrans, filename);
ReWrite(Ftrans);
//设置状态变量
StopTrans := False;
InTrans := True;
//接收数据
Recelen := recv(RecvSocket, BlockBuf, BlockLen, 0);
while (Recelen > 0) and (not StopTrans) and (Rece_Max <> 15195) do
begin
i := 15195 - Rece_Max;
if i >= BlockLen then
i := BlockLen;
BlockWrite(Ftrans, BlockBuf[0], Recelen);
Application.ProcessMessages;
Recelen := recv(RecvSocket, BlockBuf, i, 0);//问题2
Rece_Max := Rece_Max + Recelen;
//当停止接收时,停止传输
if StopTrans then
begin
CloseFile(Ftrans);
closesocket(RecvSocket);
InTrans := False;
MessageBox(Handle, '停止传输!', '提示', MB_OK);
Exit;
end;
end;
//关闭文件,接收的Socket
CloseFile(Ftrans);
closesocket(RecvSocket);
InTrans := False;
if (Recelen = SOCKET_ERROR) then
MessageBox(Handle, '传输异常终止!', '提示', MB_OK)
else
MessageBox(Handle, '客户端已经关闭连接!文件可能已经传送完毕!', '提示', MB_OK);
end;
发送端有问题1,接收端有问题2
问题1:是当我在发送大容量的文件时,在发送端SEND处(也就是问题1的地方)按事先划好的文件块发到一定大小的时候会停在那,无法动弹,也没任何返回值包括错误的,而且我是规定文件块每次发送的大小为4K,但我发送较小的文件时却没这样的情况,我分别试发了16K和50K的文件,16K的没问题,50K的有问题,为什么会这样呢?
问题2:在接收文件时,我按4K一次的容量接收,在剩余接受数据大小小于4K时,用剩余实际大小接受,但为什么总在接受文件最后一块的时候,在RECV处(也就是问题2)处挂的那里,什么返回值也没有,就跟程序死了一样,但当我强行终止程序时,发现接受的文件却是对的而且可以打开,这又是为什么呢?
请各位大佬帮忙看看