Streaming files over http

S

Stig-Arne Basberg

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());
}
}//------------------------------------------------------------------------
 
D

David Browne

Stig-Arne Basberg said:
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
 
S

Stig-Arne Basberg

David Browne said:
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());
}
}
 
V

Vijaye Raji

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 said:
David Browne said:
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());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top