A long socket-write

It seemed natural to code the sending of data to an SSL socket thus:

socket.write (EncodeBase64 (imagedata)) socket.flush ()

where imagedata is a memoryblock filled by a binary read from a file.

However, the file may be sizeable and when today I chanced to do that with a 7Mbyte file, I had a spinning pizza for 63 seconds, during which time my app was entirely unresponsive, even though the code above is in a thread.

In order to get the thread to yield, should I be looking at encoding the data as one statement, then splitting that on the line-ends, and then in a loop, either writing the lines one at a time to the socket or in groups of 100 or so?

May I ask why you’re using EncodeBase64? If the server/protocol don’t require it then you probably don’t need it. Sockets are perfectly capable of handling raw binary data.

This will work for text (or base64 encoded binary). But there isn’t any obvious reason to split it into lines or base64 encode it, so I wouldn’t do it that way. Instead, I would write X number of bytes directly from the file on each iteration of the loop:

Dim bs As BinaryStream = BinaryStream.Open(myFolderItem) // file to upload Do Until bs.EOF socket.Write(bs.Read(1024 * 16)) // 16KB is a reasonable size App.YieldToNextThread Loop bs.Close

PS
Threads will automatically yield at a loop boundary if another thread with a higher priority is waiting or if the current thread’s time slice has elapsed. So it’s not strictly necessary (or advisable) to call App.YieldToNextThread on each iteration of the loop, particularly if you give the socket thread a lower priority than the main thread.

The app is an email client, and it’s an email attachment I’m sending, so all data sent through the socket must be US-ASCII. Hence the EncodeBase64.

You could also think of a daemonized console app, next to you GUI, taking the task to send the data out.