🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #2976

Re: Modify the timeout for open() method

#2976
From: Remy Lebeau (TeamB)
Date: Fri, 02 Dec 2005 19:12
85 lines
3209 bytes
"enzo" <enzo.arlati@aesys.it> wrote in message
news:438ffe59@newsgroups.borland.com...

> I create and open the socket in blocking mode from inside a thread.

You need another thread in order to abort a blocking Open().

> I need to make a fast check before call the open

There is no way to do that.  The only way to know if a socket is listening
on another machine is to connect to it.

> or mybe shorten the timeout if I call open on a pc not ready to listen.

Like I told you before, there is no connect timeout available.  You have to
implement your own.

> what do you mean for a separate thread to disconnect the socket

I mean exactly what I say.  The only way to abort a blocking Open() is to
disconnect the socket from the context of a different thread than the one
that is calling Open().  For example (untested):

    class TConnectThread : public TThread
    {
    private:
        TClientSocket *FSocket;
    protected:
        virtual void __fastcall Execute();
    public:
        __fastcall TConnectThread(TClientSocket *ASocket);
    };

    __fastcall TConnectThread::TConnectThread(TClientSocket *ASocket)
        : TThread(true)
    {
        FSocket = ASocket;
        Resume();
    }

    void __fastcall TConnectThread::Execute()
    {
        try
        {
            FSocket->Open();
        }
        catch(const Exception &) {}
    }


    __fastcall TSocketThread::TSocketThread()
        : TThread(true)
    {
        clientSck = new TClientSocket( NULL );
        clientSck->ClientType = ctBlocking;
        Resume();
    }

    __fastcall TSocketThread::~TSocketThread()
    {
        delete clientSck;
    }

    void __fastcall TSocketThread::Execute()
    {
        //...
        clientSck->Address = ipTarge;
        clientSck->Port = portTarget;

        TConnectThread *CT = new TConnectThread(clientSck);
        if( WaitForSingleObject((HANDLE)CT->Handle, 1000) != WAIT_OBJECT_0 )
        {
            clientSck->Close();
            CT->WaitFor();
        }
        delete CT;

        if( !clientSck->Active ) return;

        // ...
    }


Gambit
Technical Details
Message-ID: <43909cb3$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <438ed040@newsgroups.borland.com> <438f4204$1@newsgroups.borland.com> <438ffe59@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,234cf6f94ba290e1
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <438ed040@newsgroups.borland.com> <438f4204$1@newsgroups.borland.com> <438ffe59@newsgroups.borland.com>
Subject: Re: Modify the timeout for open() method
Date: Fri, 2 Dec 2005 11:12:01 -0800
Lines: 87
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <43909cb3$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1133550771 68.69.17.41 (2 Dec 2005 11:12:51 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail