如果这个传输程序在互联网上运行,在局域网内测试通过,但在互联网上和网络状况有关,应该如何保证互联网上传输准确呢,测试发现有丢包现象,应该是和缓冲区有关,
客户端的发送事件
procedure TfrmClientMain.cSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
cmd: string;
strm, mstream: tmemorystream;
msgHead:TMsgHead;
bufsize: integer;
begin
bufsize := 4096;
mstream:=Tmemorystream.Create;
mstream.SetSize(socket.receivelength);
mstream.Position:=0;
socket.ReceiveBuf(mstream.Memory^,socket.receivelength);
mstream.Read(msghead, sizeof(TmsgHead)) ;
mstream.Position := 0;
if msgHead.MsgType = SENDBLOCK then
begin
strm := tmemorystream.Create;
if size > bufsize then
begin
strm.CopyFrom(strmsend, bufsize);
end
else
begin
strm.copyfrom(strmsend, size);
end;
size := size - bufsize;
strm.Position := 0;
socket.SendStream(strm);
end
else if msghead.MsgType = test1 then
begin
mstream.Read(tt, sizeof(test));
b := true;
end;
服务器的read事件:
procedure TfrmServerMain.svrSockClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
len: integer;
buffer: array[0..4096] of byte;
i: integer;
j: integer;
strm: tmemorystream;
msgHead:TMsgHead;
mstream, msSend:Tmemorystream;
tc :TClient;
tsf :TSendFile;
tsb :TSendBlock;
t: test;
begin
tc := TClient(self.SocketExists(Socket));
if not tc.bFlag then
begin
mstream:=Tmemorystream.Create;
mstream.SetSize(socket.receivelength);
mstream.Position:=0;
socket.ReceiveBuf(mstream.Memory^,socket.receivelength);
mstream.Read(msghead, sizeof(TmsgHead)) ;
case msghead.MsgType of
CHAT:
// mstream.Read() break;
Exit;
SENDFILE:
begin
mstream.Position := 0;
mstream.Read(tsf, sizeof(TSendFile));
tc.strmRec := TFileStream.Create('c:/' + tsf.fileName, fmCreate);
tc.filesize := tsf.filesize;
tc.RFileName := tsf.fileName;
tc.bFlag := true;
tsb.msgHead.MsgType := SENDBLOCK;
tsb.msg := '';
Socket.SendBuf(tsb, sizeof(tsb));
end;
test1:
begin
// mstream.Read(t, sizeof(t));
t.msgHead.MsgType := test1;
t.o.e := 22221;
t.o.f := 'bbb';
t.o.g := 'cccc';
socket.SendBuf(t,sizeof(t));
end;
end;
mstream.Free;
end
else
begin
len := socket.ReceiveLength;
socket.ReceiveBuf(buffer, len);
tc.strmRec.Seek(tc.strmRec.Size, sofrombeginning);
strm := tmemorystream.Create;
strm.Write(buffer, len);
strm.Position := 0;
tc.strmRec.CopyFrom(strm, len);
strm.Free;
meMessage.Lines.Add(IntToStr(tc.strmRec.Size) + '=======' + IntToStr(tc.filesize));
if tc.strmRec.size = tc.filesize then
begin
tc.strmRec.Free;
tc.bFlag := false;
// socket.SendText('end ' + FeedBack);
// socket.Close;
end
else
begin
tsb.msgHead.MsgType := SENDBLOCK;
tsb.msg := '';
Socket.SendBuf(tsb, sizeof(tsb));
end;
end;
end;
我现在的这个程序是在这个基础上改的(这个只是传输的一个小框架)这个程序在互联网上没问题,同样网络环境另一个程序(一些业务操作,数据打包竺)有问题,大家有这方面的经验,请指教,谢谢!!!