PC Review


Reply
Thread Tools Rate Thread

Chuncked upload via socket

 
 
cyberco
Guest
Posts: n/a
 
      9th Jan 2007
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?

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      9th Jan 2007
cyberco wrote:
> 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

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Service y Chuncked Data Atari Microsoft ASP .NET 0 2nd Jun 2008 06:29 PM
Chuncked upload via socket cyberco Microsoft Dot NET Framework 2 10th Jan 2007 08:18 AM
Chuncked upload via socket cyberco Microsoft Dot NET Compact Framework 0 9th Jan 2007 12:08 PM
Chuncked upload via socket cyberco Microsoft Dot NET Compact Framework 0 9th Jan 2007 12:05 PM
Socket.BeginSendTo and Socket.BeginSendFrom on a single Socket instancefrom multiple threads Jonas Hei Microsoft C# .NET 2 22nd Jun 2005 12:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:52 PM.