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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • SERVICE_CONTROL_STOP not received
    ... the ServiceMainwill start a socket ... The socket server is wrapped in a class ... is "Wait" and the reason is "User Request") ... SERVICE_CONTRL_STOP or other control messages from SCM? ...
    (microsoft.public.win32.programmer.kernel)
  • Re: IPC looking for simple/best way to communicate
    ... want to go to a server/client connection. ... Server accepts connections on given port. ... request or a request with a removal. ... >> one process reads a socket it removes the message from the socket and ...
    (comp.lang.perl.misc)
  • IE6 bug: doesnt resend form-data when server resets connection
    ... When the server closes the connection on a socket where IE is currently ... sends the request on an existing socket and the server immediately replies ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: TCP reset caused by socket.py
    ... seems that all POST requests to Trac's standalone server have ... chance the server will issue a TCP RST. ... A RST when you close a socket is OK. ... handling each request on its own thread. ...
    (comp.lang.python)
  • [REVS] NTLM HTTP Authentication is Insecure By Design
    ... in front of a web server, and that proxy server shares a single TCP ... These are attacks that make use of non-RFC HTTP requests (HTTP Request ... the authentication is associated with the ...
    (Securiteam)