Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

MF alternate to Stream.CopyTo


  • Please log in to reply
7 replies to this topic

#1 spaceyguy

spaceyguy

    Member

  • Members
  • PipPip
  • 10 posts

Posted 01 February 2011 - 11:03 PM

I was trying to duplicate the effect of using the Stream.CopyTo, so that I could redirect the output of a HttpWebResponse response stream to a FileStream. I created the following to as an attempt; it works on regular .Net but not on the netduino+.

FileStream fOut = File.Create(@"\SD\downed");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://example.com");//"http://feeds2.feedburner.com/the404/");
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream respStream = resp.GetResponseStream();
int read = respStream.ReadByte();
while (read != -1)
{
    read = respStream.ReadByte();
    fOut.WriteByte((byte)read);
}

respStream.Close();
fOut.Close();


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 01 February 2011 - 11:36 PM

Hi spaceyguy, What is the result if you try doing that with .NET MF? Chris

#3 spaceyguy

spaceyguy

    Member

  • Members
  • PipPip
  • 10 posts

Posted 01 February 2011 - 11:56 PM

I'm trying to download the requested page.

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 02 February 2011 - 12:00 AM

Sorry, let me ask that another way...what error are you getting when you run the code? Is a feature not implemented? Do you get an exception?

#5 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 02 February 2011 - 05:44 AM

Whatever other issues it may have, it also seems to have a bug, as it unconditionally throws away the first byte read without copying it. You probably want:

while(true) {
  int read = respStream.ReadByte();
  if(read == -1)
     break;
  fOut.WriteByte((byte)read);
}


#6 spaceyguy

spaceyguy

    Member

  • Members
  • PipPip
  • 10 posts

Posted 02 February 2011 - 07:12 PM

Thanks Corey, I knew there was a better design but I was having a mental block preventing me to find one.

Chris as for what actually happens, after 5 minutes I get the following:
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::recv [IP: 0000] ####
    #### System.Net.Sockets.Socket::Receive [IP: 0018] ####
    #### System.Net.Sockets.NetworkStream::Read [IP: 0062] ####
    #### System.Net.InputNetworkStreamWrapper::RefillInternalBuffer [IP: 0038] ####
    #### System.Net.InputNetworkStreamWrapper::ReadByte [IP: 000e] ####
    #### DownFile.Program::Main [IP: 0026] ####
    #### SocketException ErrorCode = 10060
    #### SocketException ErrorCode = 10060
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = 10060
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll



#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 02 February 2011 - 09:20 PM

spaceyguy, It looks like the final ReadByte() is waiting for a timeout of 5 minutes before throwing a timeout exception of sorts. Do you get all of your data before that call? Perhaps the ReadByte() call is improperly timing out instead of just returning EOF? Chris

#8 spaceyguy

spaceyguy

    Member

  • Members
  • PipPip
  • 10 posts

Posted 03 February 2011 - 12:30 AM

It only creates the file, nothing is output into it.
EDIT: When I substituted respStream with a FileStream it did work, if that means anything.
EDIT 2: I changed the page that it requested and it was successful.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.