By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,302 Members | 1,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,302 IT Pros & Developers. It's quick & easy.

Streaming files over http

P: n/a

I am working on a project where I want to send at file from a Win CE device
to a receiving PC using the HTTP protocol.
For testing purpose I made a simple test app on my PC (code below).

The problem is that only the first 8760 bytes of the file is transfered. I
have tried several times and the received file is always 8760 bytes.
I have tested with several files (10k -> 200k).

My receiving application works very well with other application. I have
transfered files over 1MB. So there must be something with my send
application.
I have checked that the size in the header message is received correctly at
the receiving end but the actual stream is to short. The size of the stream
buffer on the sending side is also correct. So it seems like there is some
problems with the "low level" transfer. Is there some setting I may have
missed?

Does someone have any ideas?
Thanks in advance for any comments


public void SendFile()
{
try
{
HttpWebRequest request=(HttpWebRequest)
WebRequest.Create("http://192.168.1.10:16000/Send/test.xml);

request.ContentType="text/xml";
request.Method = "POST";
request.Timeout = 10000;
FileStream fs = new FileStream("D:\\Temp\\test.xml", FileMode.Open,
FileAccess.Read);
request.ContentLength = fs.Length;
fs.Close();
request.AllowWriteStreamBuffering = true;

request.BeginGetRequestStream(new AsyncCallback(ReadCallback),
request);

allDone.WaitOne();

HttpWebResponse httpWResp = (HttpWebResponse)request.GetResponse();
httpWResp.Close();
}
catch (System.Net.WebException escWeb)
{
if (escWeb.Response != null)
escWeb.Response.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}//------------------------------------------------------------------------
private static void ReadCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request =
(HttpWebRequest)asynchronousResult.AsyncState;

request.AllowWriteStreamBuffering = true;

Stream postStream = request.EndGetRequestStream(asynchronousResult);

FileStream fs = new FileStream("D:\\temp\\test.xml", FileMode.Open,
FileAccess.Read, FileShare.None);
byte[] buf = new byte[fs.Length];
fs.Read(buf, 0, (int)fs.Length);

postStream.Write(buf, 0, buf.Length);

postStream.Close();
fs.Close();
allDone.Set();
}
catch (System.Net.WebException escWeb)
{
if (escWeb.Response != null)
escWeb.Response.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}//------------------------------------------------------------------------

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Stig-Arne Basberg" <sab(a)isystem.no> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...

I am working on a project where I want to send at file from a Win CE device to a receiving PC using the HTTP protocol.
For testing purpose I made a simple test app on my PC (code below).

The problem is that only the first 8760 bytes of the file is transfered. I
have tried several times and the received file is always 8760 bytes.
I have tested with several files (10k -> 200k).

My receiving application works very well with other application. I have
transfered files over 1MB. So there must be something with my send
application.
I have checked that the size in the header message is received correctly at the receiving end but the actual stream is to short. The size of the stream buffer on the sending side is also correct. So it seems like there is some
problems with the "low level" transfer. Is there some setting I may have
missed?


Why are you doing an async receive? You are turning right around and
waiting for it to complete, so you're just making two threads do the work of
one.

Use simple blocking IO, and I bet you'll find the problem right away.

David
Nov 16 '05 #2

P: n/a

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:eW**************@tk2msftngp13.phx.gbl...

"Stig-Arne Basberg" <sab(a)isystem.no> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...

I am working on a project where I want to send at file from a Win CE device
to a receiving PC using the HTTP protocol.
For testing purpose I made a simple test app on my PC (code below).

The problem is that only the first 8760 bytes of the file is transfered. I have tried several times and the received file is always 8760 bytes.
I have tested with several files (10k -> 200k).

My receiving application works very well with other application. I have
transfered files over 1MB. So there must be something with my send
application.
I have checked that the size in the header message is received correctly

at
the receiving end but the actual stream is to short. The size of the

stream
buffer on the sending side is also correct. So it seems like there is some problems with the "low level" transfer. Is there some setting I may have
missed?


Why are you doing an async receive? You are turning right around and
waiting for it to complete, so you're just making two threads do the work

of one.

Use simple blocking IO, and I bet you'll find the problem right away.

David


Thanks for the replay.

I am quit new to the .net framework and I don't have to much experience
working with sockets. So I am not sure I understand what you mean.

I made an other simple test (code below). But with the same result. Only
8760 bytes transfered.
public void SendFile()
{
try
{
HttpWebRequest request=(HttpWebRequest)
WebRequest.Create("http://192.168.1.10/Send/test.xml);

request.ContentType="text/xml";
request.Method = "POST";
request.Timeout = 10000;

FileStream fs = new FileStream("D:\\Temp\\test.xml", FileMode.Open,
FileAccess.Read);
request.ContentLength = fs.Length;

byte[] buf = new byte[fs.Length];
fs.Read(buf, 0, (int)fs.Length);

Stream st = request.GetRequestStream();
st.Write(buf, 0, buf.Length);
st.Flush();
st.Close();
fs.Close();
}
catch (System.Net.WebException escWeb)
{
if (escWeb.Response != null)
escWeb.Response.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}

Nov 16 '05 #3

P: n/a
A Read operation on the stream is not guaranteed to return the entire length
even if you pass in a buffer big enough. You need to check the return value
that gives the number of bytes read and if there more to be read, read
again.

Typically, you'll have all stream read operations in a while loop that keeps
checking if you have read all that you wanted to read.

HTH

-vJ

"Stig-Arne Basberg" <sab(a)isystem.no> wrote in message
news:eQ*************@tk2msftngp13.phx.gbl...

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:eW**************@tk2msftngp13.phx.gbl...

"Stig-Arne Basberg" <sab(a)isystem.no> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
>
> I am working on a project where I want to send at file from a Win CE

device
> to a receiving PC using the HTTP protocol.
> For testing purpose I made a simple test app on my PC (code below).
>
> The problem is that only the first 8760 bytes of the file is
> transfered. I > have tried several times and the received file is always 8760 bytes.
> I have tested with several files (10k -> 200k).
>
> My receiving application works very well with other application. I have
> transfered files over 1MB. So there must be something with my send
> application.
>
>
> I have checked that the size in the header message is received
> correctly

at
> the receiving end but the actual stream is to short. The size of the

stream
> buffer on the sending side is also correct. So it seems like there is some > problems with the "low level" transfer. Is there some setting I may
> have
> missed?
>


Why are you doing an async receive? You are turning right around and
waiting for it to complete, so you're just making two threads do the work

of
one.

Use simple blocking IO, and I bet you'll find the problem right away.

David


Thanks for the replay.

I am quit new to the .net framework and I don't have to much experience
working with sockets. So I am not sure I understand what you mean.

I made an other simple test (code below). But with the same result. Only
8760 bytes transfered.
public void SendFile()
{
try
{
HttpWebRequest request=(HttpWebRequest)
WebRequest.Create("http://192.168.1.10/Send/test.xml);

request.ContentType="text/xml";
request.Method = "POST";
request.Timeout = 10000;

FileStream fs = new FileStream("D:\\Temp\\test.xml", FileMode.Open,
FileAccess.Read);
request.ContentLength = fs.Length;

byte[] buf = new byte[fs.Length];
fs.Read(buf, 0, (int)fs.Length);

Stream st = request.GetRequestStream();
st.Write(buf, 0, buf.Length);
st.Flush();
st.Close();
fs.Close();
}
catch (System.Net.WebException escWeb)
{
if (escWeb.Response != null)
escWeb.Response.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.