Chuncked upload via socket

C

cyberco

Using: .Net Compact Framework 2, Windows Mobile 5 (Pocket PC)

I'm trying to upload a large binary file in chuncks over a socket (as a
multipart mime message). This is how I'm trying to accomplish this, but
it somehow doesn't work:

================================================
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPAddress ipa = Dns.GetHostEntry("www.blablabla.com").AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipa, 80);
s.Connect(ipe);

FileStream tmpFileStream = new FileStream(tmpUploadFileName,
FileMode.Open, FileAccess.Read);

StringBuilder postString = new StringBuilder("POST /servlet
HTTP/1.1\r\n");
postString.Append("Host: www.blablabla.com\r\n");
postString.Append("User-Agent: Windows Mobile 5 PPC\r\n");
postString.Append("Accept: */*;q=0.5\r\n");
postString.Append("Keep-Alive: 300\r\n");
postString.Append("Connection: keep-alive\r\n");
postString.Append("Content-Type: multipart/form-data;
boundary=----1234----\r\n");
postString.Append("Content-Length: " + tmpFileStream.Length + "\r\n");
postString.Append("\r\n");
postString.Append("\r\n");
s.Send(Encoding.UTF8.GetBytes(postString.ToString()));

int numRead = 1;
int chunkSize = 1024;
byte[] bytes = new byte[chunkSize];
while ((numRead = tmpFileStream.Read(bytes, 0, chunkSize)) > 0) {
try {
s.Send(bytes, numRead, SocketFlags.None);
} catch (Exception e) {
}
}

s.Shutdown(SocketShutdown.Send);
if (s.Connected) {
s.Close();
}

================================================

If I read in the complete file and upload it at once everything goes
fine, so it's likely not a problem with the format of the multipart
mime message.

I find the documentation on FileStream.Read() a bit vague, saying that
the 'offset' parameter is the offset in 'aray', which is the
destination array. It says 'offset: The byte offset in array at which
to begin reading. ' (where 'array' is the name of the destination array
parameter). Reading from the destination array?
 
J

Jon Skeet [C# MVP]

cyberco said:
Using: .Net Compact Framework 2, Windows Mobile 5 (Pocket PC)

I'm trying to upload a large binary file in chuncks over a socket (as a
multipart mime message). This is how I'm trying to accomplish this, but
it somehow doesn't work:

So, it doesn't do what you want it to do - but what *does* it do?
You're currently catching all exceptions and throwing them away - you
should *at least* log them, as that may well show you what's going
wrong.

Your call to FileStream.Read is fine (although you should use "using"
statements to close both the FileStream and the Socket).

Jon
 

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