Hi.
I want to make a program to download files from a remote host.
So what i do is to send throw sockets all the binary information + a string used as end "mystyring".
To check if i have to continue writing the buffer to the file i first convert all to string, so if i find "mystring" it will finish, if not, i reconvert all to bytes and i copy data.
The problem im having is that, .txt files are writed well, but binaris not, it always show error at opening. I ve tried copying a pdf, and when i open it show all the pages, but in blank :S. Also if i check the size of the original file is always some bytes bigger than the copy.
[CODE]FileStream fs = new FileStream("C:\\Users\\Mauri\\Desktop\\" + item.Name, FileMode.Append, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
bool continua = true;
while (continua)
{
recibido = getSocket(numsock).socketRecv(); // This return the data converted to string.
words = recibido.Split('|'); //We split "|mystring" to find the end of file.
foreach (string s in words)
{
if ((String.Compare(s, "mystring") != 0))
writer.Write(StringToBytes(s)); // Write converted data.
else
continua = false; // if we find "mystring", end the action.
}
}
writer.Close();
fs.Close();
// Convert string to bytes
public byte[] StringToBytes(String cadena)
{
System.Text.ASCIIEncoding codificador = new System.Text.ASCIIEncoding();
return codificador.GetBytes(cadena);
}
[/CODE]
The strange thing is that .txt files are writed correctly.
Thanks
MaurizioPosted Jun 13, 2011, 12:58 PM
Thanks
Sam HobbsPosted Jun 12, 2011, 10:27 PM
So I suggest that you read the data as a byte array and then convert that to a string just for the purpose of searching for the string. Use the string only for searching for "|mystring". The string would be useless for use as binary data because about half the data (all bytes with a value in the range 128-255) will be incorrect due to the conversion from binary to string. You can use the offset of "|mystring" in the string to determine the location in the byte array.
A much better solution would be to send a count of the number of bytes (the size of the file) at the beginning of every file instead of putting "|mystring" at the end. Nearly thirty years ago I wrote a program (in COBOL) to send files from an unclassified environment to a classified environment using magnetic tape. At the time I was a contractor and they were so impressed that I was able to do it so quickly that they hired me as a permanent employee at a rate higher than I first asked for. I think that sending a file size preceding every file will work better.
Sam HobbsPosted Jun 12, 2011, 9:56 PM
MaurizioPosted Jun 12, 2011, 8:58 PM
I send "mystring" throw c++ function send.
len = send(sock,"|mystring",(int)strlen("|mystring"),0);
Thanks
Sam HobbsPosted Jun 12, 2011, 8:26 PM
MaurizioPosted Jun 12, 2011, 7:52 PM
When the program makes the first archive, it is make ok, rar, zip, pdf, any extension works well, but when i try to download a second file, it is corrupted.
Any idea what is going wrong ?
MaurizioPosted Jun 12, 2011, 7:35 PM
Sam HobbsPosted Jun 12, 2011, 7:24 PM
MaurizioPosted Jun 12, 2011, 5:03 PM
I can't use webclient class, beacuse im not downloading the data from an http or ftp server, is for a connection between 2 computers, like a remote access program.
Thank you.
Sam HobbsPosted Jun 12, 2011, 3:03 PM
Can you use the WebClient class to download the files? If not then why not; if you can explain what you need to do then hopefully someone can help you with it.