Home All Groups Group Topic Archive Search About

Re: HowTo, Download first 1024 bytes of file ONLY?

Author
28 Mar 2006 9:36 PM
John Daragon
NutsAboutVB wrote:
> Basically, the title explains it.
>
> I want to be able to read the first n bytes (say, 1024 bytes) of any
> file (text or binary) off the internet in my vb.net application. I
> specifically do not want to download the whole file (which in most
> cases will be significantly large compared to the portion i do want).

Well, I'm no specialist (I started programming C# err... yesterday.

But, how about something like this horrible non-compilable code snippet
(most of which was stolen from a Microsoft C# socket example) :

      int port = PORT_NUMBER;
          string server = SERVER_NAME;

            Socket s = null;
            IPHostEntry hostEntry = null;
            hostEntry = Dns.GetHostEntry(server);

            foreach (IPAddress address in hostEntry.AddressList)
            {
                IPEndPoint ipe = new IPEndPoint(address, port);
                Socket tempSocket = new Socket(ipe.AddressFamily,        
                            SocketType.Stream, ProtocolType.Tcp);

                try
                {

                    tempSocket.Connect(ipe);

                }
                catch { }
                finally
                {
                }

                if (tempSocket.Connected)
                {
                    s = tempSocket;
                    break;
                }
                else
                {
                    // explain to the user why we can't service the request,
                    // and either try again or abend.

                    DialogResult dr = MessageBox.Show(

                                 "We don't appear to be able to connect
to the server. Is it running ?", "Socket Error",
                                    MessageBoxButtons.RetryCancel,
                                    MessageBoxIcon.Exclamation,
                                    MessageBoxDefaultButton.Button1);


                    continue;
                }

            }

            // Send request to the server.

            s.Send(Request, Request.Length, 0);

            int bytes = 0;
            i = 0;
            // The following will block until the data is transmitted.
            for (; ; )
            {

                // This returns a stream of bytes
                NumberOfBytes = s.Receive(buffer, buffer.Length, 0);
                string blah = Encoding.ASCII.GetString(buffer, 0,

                                          NumberOfBytes);

                // Add logic so you know when to stop
        //
         }

         s.Close();

> Ok, I've had my moment of publicly whining (this is what happens
> after 3-full days of performing errant google searches).

IFYP.  I've got this spiffy MSDN Universal thing, and I'd be dead in the
water without Google.

jd

Author
28 Mar 2006 9:58 PM
SStory
John,

This shouldn't be to hard, the webrequest/webresponse objects (or
derivitives) that allow downloading a file from the internet can do so in
chunks (such as 1024).

I am doing that for downloading updates for my app.

Show quote
"John Daragon" <j***@argv.co.uk> wrote in message
news:4429ac6b$0$23297$db0fefd9@news.zen.co.uk...
> NutsAboutVB wrote:
>> Basically, the title explains it.
>>
>> I want to be able to read the first n bytes (say, 1024 bytes) of any
>> file (text or binary) off the internet in my vb.net application. I
>> specifically do not want to download the whole file (which in most
>> cases will be significantly large compared to the portion i do want).
>
> Well, I'm no specialist (I started programming C# err... yesterday.
>
> But, how about something like this horrible non-compilable code snippet
> (most of which was stolen from a Microsoft C# socket example) :
>
>    int port = PORT_NUMBER;
>          string server = SERVER_NAME;
>
>            Socket s = null;
>            IPHostEntry hostEntry = null;
>            hostEntry = Dns.GetHostEntry(server);
>
>            foreach (IPAddress address in hostEntry.AddressList)
>            {
>                IPEndPoint ipe = new IPEndPoint(address, port);
>                Socket tempSocket = new Socket(ipe.AddressFamily,
>                            SocketType.Stream, ProtocolType.Tcp);
>
>                try
>                {
>
>                    tempSocket.Connect(ipe);
>
>                }
>                catch { }
>                finally
>                {
>                }
>
>                if (tempSocket.Connected)
>                {
>                    s = tempSocket;
>                    break;
>                }
>                else
>                {
>                    // explain to the user why we can't service the
> request,
>                    // and either try again or abend.
>
>                    DialogResult dr = MessageBox.Show(
>
>                                 "We don't appear to be able to connect
> to the server. Is it running ?", "Socket Error",
>                                    MessageBoxButtons.RetryCancel,
>                                    MessageBoxIcon.Exclamation,
>                                    MessageBoxDefaultButton.Button1);
>
>
>                    continue;
>                }
>
>            }
>
>            // Send request to the server.
>
>            s.Send(Request, Request.Length, 0);
>
>            int bytes = 0;
>            i = 0;
>            // The following will block until the data is transmitted.
>            for (; ; )
>            {
>
>                // This returns a stream of bytes
>                NumberOfBytes = s.Receive(buffer, buffer.Length, 0);
>                string blah = Encoding.ASCII.GetString(buffer, 0,
>
>                                          NumberOfBytes);
>
>                // Add logic so you know when to stop
> //
>      }
>
>      s.Close();
>
>> Ok, I've had my moment of publicly whining (this is what happens
>> after 3-full days of performing errant google searches).
>
> IFYP.  I've got this spiffy MSDN Universal thing, and I'd be dead in the
> water without Google.
>
> jd
Author
5 Apr 2006 2:16 PM
NutsAboutVB
Hello,

I was hoping to not hear the words 'socket programming', since it does
remind me of the tedious hit and miss days of socket hackery back in
VB6 times - wasn't my fav. thing to work with, but I am sure sockets in
..net 2.0 is much more user friendly. Thank you for the code.

Regarding webrequest/webresponse, are you sure about this? Since when i
did look into the documentation i didn't read anything about this.

Excuse the delay in my response (it was Formula 1 weekend in melbourne
australia, and as you probably know, getting back into 'programmer
mode' does take some time).

Show quote
:)
Author
5 Apr 2006 8:08 PM
SStory
I don't have the code with me at this location, but yes I am sure that I am
getting it in 1024 chunks and then doing as I wish with it.

webrequest/response I think are abstract classes--could be wrong....seems
there was an httpwebresponse object or request object that I was actually
using.

A quick search produced this:
http://www.codeguru.com/Csharp/Csharp/cs_network/internetweb/article.php/c7005/

Maybe it will help you.

I am not certain but I think you get the stream and read it little by little
and just by having that stream, doesn't mean it has all been downloaded, so
I assume you could get 1024 bytes from it and then abort some how.

HTH,
Shane
Show quote
"SStory" <nospam@nospam.com> wrote in message
news:uHqKQKrUGHA.4740@TK2MSFTNGP14.phx.gbl...
> John,
>
> This shouldn't be to hard, the webrequest/webresponse objects (or
> derivitives) that allow downloading a file from the internet can do so in
> chunks (such as 1024).
>
> I am doing that for downloading updates for my app.
>
> "John Daragon" <j***@argv.co.uk> wrote in message
> news:4429ac6b$0$23297$db0fefd9@news.zen.co.uk...
>> NutsAboutVB wrote:
>>> Basically, the title explains it.
>>>
>>> I want to be able to read the first n bytes (say, 1024 bytes) of any
>>> file (text or binary) off the internet in my vb.net application. I
>>> specifically do not want to download the whole file (which in most
>>> cases will be significantly large compared to the portion i do want).
>>
>> Well, I'm no specialist (I started programming C# err... yesterday.
>>
>> But, how about something like this horrible non-compilable code snippet
>> (most of which was stolen from a Microsoft C# socket example) :
>>
>>    int port = PORT_NUMBER;
>>          string server = SERVER_NAME;
>>
>>            Socket s = null;
>>            IPHostEntry hostEntry = null;
>>            hostEntry = Dns.GetHostEntry(server);
>>
>>            foreach (IPAddress address in hostEntry.AddressList)
>>            {
>>                IPEndPoint ipe = new IPEndPoint(address, port);
>>                Socket tempSocket = new Socket(ipe.AddressFamily,
>>                            SocketType.Stream, ProtocolType.Tcp);
>>
>>                try
>>                {
>>
>>                    tempSocket.Connect(ipe);
>>
>>                }
>>                catch { }
>>                finally
>>                {
>>                }
>>
>>                if (tempSocket.Connected)
>>                {
>>                    s = tempSocket;
>>                    break;
>>                }
>>                else
>>                {
>>                    // explain to the user why we can't service the
>> request,
>>                    // and either try again or abend.
>>
>>                    DialogResult dr = MessageBox.Show(
>>
>>                                 "We don't appear to be able to connect
>> to the server. Is it running ?", "Socket Error",
>>                                    MessageBoxButtons.RetryCancel,
>>                                    MessageBoxIcon.Exclamation,
>>                                    MessageBoxDefaultButton.Button1);
>>
>>
>>                    continue;
>>                }
>>
>>            }
>>
>>            // Send request to the server.
>>
>>            s.Send(Request, Request.Length, 0);
>>
>>            int bytes = 0;
>>            i = 0;
>>            // The following will block until the data is transmitted.
>>            for (; ; )
>>            {
>>
>>                // This returns a stream of bytes
>>                NumberOfBytes = s.Receive(buffer, buffer.Length, 0);
>>                string blah = Encoding.ASCII.GetString(buffer, 0,
>>
>>                                          NumberOfBytes);
>>
>>                // Add logic so you know when to stop
>> //
>>      }
>>
>>      s.Close();
>>
>>> Ok, I've had my moment of publicly whining (this is what happens
>>> after 3-full days of performing errant google searches).
>>
>> IFYP.  I've got this spiffy MSDN Universal thing, and I'd be dead in the
>> water without Google.
>>
>> jd
>
>

AddThis Social Bookmark Button