|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Chuncked upload via socketI'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? cyberco wrote:
> Using: .Net Compact Framework 2, Windows Mobile 5 (Pocket PC) So, it doesn't do what you want it to do - but what *does* it do?> > 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: 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 > So, it doesn't do what you want it to do - but what *does* it do? I accidentilly left out the debug info in the catch clause. It showed> 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. me that not all data was sent accross the wire, the last bits were left out. This indicates that the 'length' parameter in the headers is incorrect, but when sending the complete file at once succeeds. So what could that mean? The binary file is a multipart mime message (so including the boundaries), maybe a multipart mime message should not be uploaded in chunks? > Your call to FileStream.Read is fine (although you should use "using" OK, but I'm not sure what you mean with using 'using'> statements to close both the FileStream and the Socket). |
|||||||||||||||||||||||