Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Mar : Re: about idTcpServer
| Subject: | Re: about idTcpServer |
| Posted by: | "darnis" (darn..@21cn.com) |
| Date: | Thu, 30 Mar 2006 10:09:49 |
Thank you, Gambit.
I fixed my problem.
The IOHandler' ReadFromSource is protected member,
I extend from TidIOHandler at current unit:
type
TidIOHandleEx = class(TIdIOHandler);
and then,
iRec := TidIOHandleEx(AContext.Connection.IOHandler).ReadFromSource(True,
1000, False);
now, it's ok.
Thand you.
darnis
> Get rid of your exception handling altogether, or at leasr re-throw the
> exception if caught. You are blocking the server from receiving any
> notifications of the socket being disconnected. Remember that Indy relies
> heavily on exceptions. It uses exceptions internally for a lot of its own
> internal notifications.
>
>> if AContext.Connection.Socket.Readable(1000) then
>
> Get rid of that as well. There is no reason to have that in the code you
> showed.
>
>> iRec := AContext.Connection.Socket.Binding.Receive(TBytes(strTmp));
>
> You should NEVER be calling Receive() directly. You must use the various
> reading methods of the IOHandler only. Worse, you are trying to force the
> compiler to accept a String as a TBytes, which is completely wrong anyway.
>
> Use this code instead:
>
> uses
> IdException, IdSys;
>
> procedure TfrmProxyMain.tcpServerExecute(AContext: TIdContext);
> var
> strTmp, str1: string;
> iRec: integer;
> oData: OleVariant;
> ra: TlbResponseAsk;
> p: TlbParameterMana;
> begin
> try
> if DEBUG then _('DA');
> iRec := AContext.Connection.IOHandler.ReadFromSource(True,
> 1000,
> False);
> if iRec <= 0 then begin
> if cm.qData(Pointer(AContext)) then AContext.Connection.Tag
> := Integer(cm.CurrentHost);
> Exit;
> end;
> strTmp := AContext.Connection.Socket.ReadString(iRec);
> if DEBUG then _(Sys.Format('DL:%d, [%s]', [iRec, strTmp]));
> DP.UnPacketData(strTmp, oData);
> FTCPMsg.XML := oData;
> if FTCPMsg.IsEmpty then begin
> if DEBUG then _(Sys.Format('EP:[%s]', [strTmp]));
> Exit;
> end;
> p := TlbParameterMana.Create;
> try
> p.Add('host', AContext.Connection.Socket.Binding.PeerIP);
> p.Add('port', AContext.Connection.Socket.Binding.PeerPort);
> p.Add('local', ltInner);
> p.Add('thread', Integer(Pointer(AContext)));
> p.Add('packet', strTmp);
> FTcpDispatcher[Byte(FTcpMsg.PacketType)](FTcpMsg, p);
> finally
> Sys.FreeAndNil(p);
> end;
> except
> on e: Exception do
> begin
> PutMessage(Sys.Format('%s %s', [str1, e.Message]));
> If e is EIdException then Raise;
> end;
> end;
> end;
>
>
> Gambit