ClientSocket(TClientSocket) 接收数据的问题,急!!!!特别求救,在线等待 (100分)

  • 主题发起人 主题发起人 frankchang
  • 开始时间 开始时间
F

frankchang

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序如下,在接收数据时会不定期的出现写地址冲突的错误(不是每次都会
有,但是比较频繁)并且每次报告的地址不同,请教各位高手
其中,nMsgLen,gBuffer,gulCount是全局变量

unit Socket_Module;

interface

uses
Windows, Messages, SysUtils, Classes, Variants, ScktComp, ExtCtrls, Dialogs, NMUDP,
Controls, Graphics;

type
TfrmSocket_Module = class(TDataModule)
Client_ccs: TClientSocket;

procedure Client_ccsRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
function BufferMsgProc() : boolean;

implementation

{$R *.dfm}
var
nMsgLen, gulCount : integer;
gBuffer : Array [0..1023] of char;

procedure TfrmSocket_Module.Client_ccsRead(Sender: TObject;
Socket: TCustomWinSocket);
var
lpBuf : TRecData;
nBufLen, nRevLen : integer;
i : integer;
send_heart : array[0..2] of char;
begin
nBufLen := 1024;
lpBuf := Clear_Array(0, 1023);
Send_Heart[0] := '.';
Send_Heart[1] := #13;
Send_Heart[2] := #10;
nRevLen := Client_ccs.Socket.ReceiveBuf(lpBuf, nBufLen);
if (nRevLen = 0) then
begin
ReceFlag := false;
exit;
end;
if ((1024 - gulCount) < nRevLen) then
begin
gBuffer := Clear_Array(0, 1023);
gulCount := 0;
end;
heartflag := true;
hearttime := now();
for i := gulCount to gulCount + nRevLen - 1 do
begin
gBuffer := lpBuf[i - gulCount];
end;
gulCount := gulCount + nRevLen;
while ( BufferMsgProc() ) do
begin
if( nMsgLen = 0 ) then //收到Server的心跳信息
begin
heartflag := true;
hearttime := now();
Client_ccs.Socket.SendBuf(Send_Heart, 3);
heartflag := false;
gBuffer := Clear_Array(0, 1023);
end
else
RecData_FromCCS(lpMsgData); //一个具体消息分析函数,更具消息头进行相应的处理
end;
end;

function TfrmSocket_Module.BufferMsgProc() : boolean;
var
pHead, pTail : TRecData;
offset, CurrentPoint, rmdlen : integer;
i : integer;
begin
if( gulCount <= 0 ) then
begin
gulCount := 0;
Result := FALSE;
exit;
end;
offset := 0;
pHead := gBuffer;
pTail := gBuffer;
while ( offset < gulCount ) do
begin
if ((pHead[offset] = '.') and (pHead[offset+1] = #13) and (pHead[offset+2] = #10)) then
begin
CurrentPoint := offset + 3;
rmdlen := gulCount - offset - 3;
gulCount := rmdlen;
lpMsgData := Clear_Array(0, 1023);
for i := 0 to offset do lpMsgData := gBuffer;
nMsgLen := offset;
if ( rmdlen > 0 ) then
begin
for i := CurrentPoint to CurrentPoint + rmdlen - 1 do gBuffer := pTail;
end;
gBuffer := Clear_Array(rmdlen, 1025 - rmdlen);
Result := TRUE;
exit;
end;
offset := offset + 1;
end;
//没有检测到./r/n,“./r/n”是自定义的消息结束符,用来分隔两部分消息
Result := FALSE;
end;
 
谢谢各位,问题已经解决了
原来是数组的控制溢出了
 
把分还给我
 
[red]^_^[/red]
 
一个利用WinSocket控件传送文件的Demo。
http://www.aidelphi.com/6to23/docu/transfile.zip
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
807
import
I
后退
顶部