Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Apr : Re: Connection lost due to "connection reset by peer"-error

www.cryer.info
Managed Newsgroup Archive

Re: Connection lost due to "connection reset by peer"-error

Subject:Re: Connection lost due to "connection reset by peer"-error
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Thu, 27 Apr 2006 14:06:06

"Shiva" <shiva@gonzo.com> wrote in message
news:445115ff$1@newsgroups.borland.com...

> I didn't know this component and seems to me it's doing what I want (my
> knowledge of Indy is still quite basic). I'll try to rewrite the program
> using this component.

> I used a critical section to prevent simultaneous access to the logfile

You weren't in the code you showed earlier.

Also, ShowMessage() is not thead-safe.  Use the Win32 API MessageBox()
function instead (not the TApplication.MessageBox() function, which is also
not thread-safe).

> I stored the different TIdPeerThread connections in a TObjectList.

TIdTCPServer already does that for you.  You can access the list via the
server's Threads property.

> You decided to create a descendant class TMyNNTPThread. A better
> approach imo but I thought this wasn't possible because the original
object,
> created by OE, is a TIdPeerThread.

That is just the default class type when you don't specify your own.
TIdTCPServer has a ThreadClass property that specifies the actual class type
to use for its threads.  You have to set that property if you want to use a
custom descendant class.  If you look at the code I gave you earlier, I am
setting that property in the form's constructor before the server is
activated:

    constructor TForm1.Create(AOwner: TComponent);
    begin
        IdTCPServer1.ThreadClass := TMyNNTPThread; // <-- here
        IdTCPServer1.Active := true;
    end;

> So commands like
>
>      TMyNNTPThread(IdTCPClient1).Connect('somehost', 12345);
>
> can be executed safely and don't result in a access violation?

That is not safe, no.  You are trying to cast the TIdTCPClient itself to a
type that it is not.  You can only cast the TIdPeerThread pointers instead,
provided that you set the ThreadClass property.  If you do not, then the
pointers are actual TIdPeerThread instances, and the cast is invalid.

> Does the cast TMyNNTPThread(IdTCPClient1) reallocate memory?

No.

> How does this work?

I take it you have never worked with type casting before?  All it is doing
is telling the compiler to interpret a memory address as a different type
than it was declared as.


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive