用ServerSocket和ClientSocket组件进行文件传输的问题,大哥大姐帮忙,只有10分了

星幻子 2005-03-09 12:26:35
var
Receiving: Boolean; //是否在接收文件
Buff: Point;
RealCount: Integer; //当前发送位置
Const
DataSize = 4096;//每次发送的字节数
发送的代码
IF Socket.ReceiveText = 'Ready To Receive Next' then
begin
GetMem(Buff,DataSize);
RealCount := SendFileStream.Read(Buff^,DataSize);
ClientSocket_SendFile.Socket.Sendbuf(Buff^,RealCount);
end;

接收代码
IF Not Receiving then
ReceiveFileStream := TFileStream.Create('C:\'+ReceiveFileName,fmCreate and fmOpenWrite);

Receiving := True;
Getmem(Buff,DataSize);
RealCount := Socket.ReceiveBuf(Buff^,DataSize);
ReceiveFileStream.WriteBuffer(Buff^,RealCount);
FreeMem(Buff);
ServerSocket_SendFile.Socket.Connections[0].SendText('Ready To Receive Next');

运行时出现这样问题:在接收到第一个数据后,反馈给发送方让发送方继续发送第二个数据
可是第儿个数据发送过来后,执行 ReceiveFileStream.WriteBuffer(Buff^,RealCount);语句程序
就会出错,提示是:Access violation at address 0041B4E5 in moudle 'xxx.exe'.Read of
address B883665F.
试过好多方法就是不行,请各位赐教啊,只有10分了,请各位高台贵手

...全文
166 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jilu_sun 2005-04-26
MARK
回复
quansui 2005-03-13
up
回复
xjlqlqlq 2005-03-12
iLength:=Socket.ReceiveLength;
GetMem(bufRecv,iLength);
FreeMem(Buff); //把这句去掉

var
iLength:integer;
接收代码
IF Not Receiving then
ReceiveFileStream := TFileStream.Create('C:\'+ReceiveFileName,fmCreate and fmOpenWrite);

Receiving := True;
iLength:=Socket.ReceiveLength;
GetMem(bufRecv,iLength);
try
RealCount := Socket.ReceiveBuf(Buff^,iLength);
ReceiveFileStream.WriteBuffer(Buff^,iLength);
finally
FreeMem(Buff);
end;
ServerSocket_SendFile.Socket.Connections[0].SendText('Ready To Receive Next');
回复
星幻子 2005-03-09
每次出错后接收到的文件有4K,说明第一次发送的数据接收成功了,为什么第二次就报错?
回复
jadeluo 2005-03-09
接收代码存在着一个比较严重的问题:如果RealCount < DataSize, 则就会出现发送方只发了一个DataSize的数据包, 而接收方却回复了多个'Ready To Receive Next'。

至于你所说的现象是否与上面的问题有关,目前还不太清楚。
回复
星幻子 2005-03-09
救命啊
回复
vinsonshen 2005-03-09
up
回复
发动态
发帖子
网络通信/分布式开发
创建于2007-08-02

1565

社区成员

Delphi 网络通信/分布式开发
申请成为版主
社区公告
暂无公告