🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: ClientSocket; Downloading a file from internet

#3816
From: Remy Lebeau (TeamB)
Date: Sat, 30 Apr 2005 01:12
70 lines
3238 bytes
"Param" <param_nirvana@yahoo.com> wrote in message
news:4272c790@newsgroups.borland.com...

>     ClientSocket1->Address = "195.190.132.10";

Use the Host property instead:

    ClientSocket1->Host = "www.national-lottery.co.uk";

>     ClientSocket1->Active = true;
>
>     if( ClientSocket1->Active )

You did not specify whether you are using the socket in blocking or
non-blocking mode.  If non-blocking, then that condition will almost always
be false since the socket does not actually connect right away.  You should
move the downloading code into the socket's OnConnect event instead, ie:

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        if( ClientSocket1->Active )
            ClientSocket1->Active = false;

        ClientSocket1->Port = 80;
        ClientSocket1->Address = "195.190.132.10";

        try {
            ClientSocket1->Active = true;
        }
        catch(const Exception &) {
            ListBox1->AddItem("Exception", this);
        }
    }

    void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
    {
        ListBox1->AddItem("Connected", this);

        // you must specify the leading backslash when specifying the file
        // to download, and you must use CRLF, not LF alone, for all
        // line breaks...

        try
        {
            Socket->SendText("GET /player/files/EuroMillions.csv
HTTP/1.0\r\n\r\n");
            //...
        }
        catch(const Exception &) {
            ListBox1->AddItem("Exception", this);
        }
    }


Now, with that said, the actual responses from the server need to be handled
in the socket's OnRead event.

However, do note that using TClientSocket in non-blocking mode, if you are
indeed doing so, will make the processing quite complicated to code for.
Even in blocking mode, HTTP in general is a complex protocol to implement
manually.  You would be MUCH better off not using TClientSocket at all.
There are plenty of third-party HTTP components available that do all of the
hard work for you.  Indy (http://www.indyproject.org) and ICS
(http://www.overbyte.be) are amongst the popuplar choices that include HTTP
support (amongst other things).


Gambit
Technical Details
Message-ID: <4272dade$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.moat.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.nuthinbutnews.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <4272c790@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,100e2486fa6e2fb2
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: <4272c790@newsgroups.borland.com>
Subject: Re: ClientSocket; Downloading a file from internet
Date: Fri, 29 Apr 2005 18:12:21 -0700
Lines: 72
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: <4272dade$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1114823390 68.69.17.41 (29 Apr 2005 18:09:50 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.moat.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.nuthinbutnews.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail