Re: HowTo, Download first 1024 bytes of file ONLY?
- From: John Daragon <john@xxxxxxxxxx>
- Date: Tue, 28 Mar 2006 22:36:37 +0100
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
.
- Follow-Ups:
- Re: HowTo, Download first 1024 bytes of file ONLY?
- From: SStory
- Re: HowTo, Download first 1024 bytes of file ONLY?
- References:
- HowTo, Download first 1024 bytes of file ONLY?
- From: NutsAboutVB
- HowTo, Download first 1024 bytes of file ONLY?
- Prev by Date: How to import Imports ROOT.CIMV2.Win32
- Next by Date: Re: HowTo, Download first 1024 bytes of file ONLY?
- Previous by thread: HowTo, Download first 1024 bytes of file ONLY?
- Next by thread: Re: HowTo, Download first 1024 bytes of file ONLY?
- Index(es):