Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 May : TServerSocket.onClientRead buffer under/overflow
| Subject: | TServerSocket.onClientRead buffer under/overflow |
| Posted by: | "David Allen" (dwall..@triosoftinc.com) |
| Date: | Tue, 9 May 2006 17:58:27 |
We have written an application that acts as a server to a single client. In
this case the single client is a legacy minicomputer with hundreds of users.
These users run an application that talks to a program that is the client
software on the legacy machine.
When I have the client write messages to the server it works correctly for
99.99% of records received. The .01% that are driving me crazy. The server
reads two types of messages one that is 22 characters long and one that is
89 characters long. In that .01% of the time the OnClientRead gets
triggered when either an incomplete records has been received in the buffer
or more than one record is in the buffer. For the 'more than one record in
the buffer' I have the program recognizing the buffer is larger than it
needed to be and deleting the first x number of characters and goes back and
processes the remainder. The code looks like the following:
procedure TfrmMain.ServerSocketClientRead(Sender: TObject; Socket:
TCustomWinSocket);
var
Request: string;
Response: string;
Action: String;
PosGET: Integer;
Log: TStrings;
LogType: Integer;
begin
LogActivity('Got Request');
Log := TStringList.Create;
LogType:= 0;
try
Request := Socket.ReceiveText;
while Length(Request) > 0 do begin
Log.Clear;
Action := '';
if Pos('GETZIP', Request) = 1 then begin // Process the ZIP
Verification
Response := ProcessGetZip(Copy(Request,1,22), LogZ);
Socket.SendText(Response);
Action := Copy(Request,1,22);
Delete(Request, 1, 22);
LogType:= 1;
end
else
if Pos('GETTAX', Request) = 1 then begin //Process the Tax
Calculation request
Response := ProcessGetTax(Copy(Request,1,89), LogT);
Socket.SendText(Response);
Action := Copy(Request,1,89);
Delete(Request, 1, 89);
LogType:= 2;
end
else begin //Record unknown
Response := 'Response to unknown request: ' + Request;
Action := Request;
PosGET := Pos('GET', Request);
Delete(Request, 1, PosGET - 1);
end;
FCSect.Enter;
try
if LogType = 0 then
LogRequest(Action, Response, Log);
if LogType = 1 then
LogRequest(Action, Response, LogZ);
if LogType = 2 then
LogRequest(Action, Response, LogT);
finally
FCSect.Leave;
end;
end;
finally
Log.Free;
end;
end;
There has to be a better way because once it starts the too little/too much
routine I start dropping reply's or sending the wrong reply such as the ZIP
Code is invalid or the tax is zero.
Thanks,
David Allen