Wednesday, May 23, 2007

Using FtpWebRequest & FtpWebResponse to Upload File

Uri FtpHost = new Uri("ftp://127.0.0.1/wrar351.exe"); //Have to include destination filename in Uri()

FtpWebRequest ftprq;

FtpWebResponse ftprp;

string FtpFileName=@"c:\wrar351.exe"; //upload a WinRar self-install file

 

ftprq = (FtpWebRequest)WebRequest.Create(FtpHost);

ftprq.EnableSsl = false;

ftprq.UseBinary = true;

ftprq.UsePassive = false;

ftprq.Method = WebRequestMethods.Ftp.UploadFile;

ftprq.Credentials = new NetworkCredential("username", "password");

 

Stream RequestStream = ftprq.GetRequestStream();

Console.WriteLine("Put file:" + FtpFileName);

using (FileStream fs = new FileStream(FtpFileName, FileMode.Open))

{

    byte[] buf = new byte[4096];

    int readcount = 0;

 

    while ((readcount = fs.Read(buf, 0, 4096)) != 0)

    {

        RequestStream.Write(buf, 0, readcount);

        Console.Write("*");

    }

}

RequestStream.Close();

ftprp = (FtpWebResponse)ftprq.GetResponse();

 

if (ftprp.StatusCode != FtpStatusCode.ClosingData) //upload complete status is Closing Data

{

    throw new IOException("Ftp upload Error: " + ftprp.StatusCode.ToString() + ftprp.StatusDescription);

}

Console.Write("\nFile Upload OK\n");

ftprp.Close();

return;

Friday, May 11, 2007

Write file >2G on AIX

To write file over 2G on AIX, you should check the following settings:

  1. Check /etc/security/limits and /etc/security/olimits. Make sure fsize is -1
  2. Issue an "lsfs -q" command, check if "bf" is enabled

Name            Nodename   Mount Pt               VFS   Size    Options    Auto Accounting

/dev/hd4        --         /                      jfs   786432  --         yes  no  
(lv size: 786432, fs size: 786432, frag size: 4096, nbpi: 2048, compress: no, bf: false, ag size: 8)

Thursday, May 10, 2007

Compute string's MD5 hash value

string s; //User Input string

string md5 = "61a60170273e74a5be90355ffe8e86ad"; //MD5ed hash string

System.Text.StringBuilder sb = new System.Text.StringBuilder();

System.Security.Cryptography.MD5CryptoServiceProvider hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();

byte[] hashed;

 

hashed = hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(s));

 

for (int i = 0; i < hashed.Length; i++)

{

    sb.Append(hashed[i].ToString("x2"));

}

 

if (s == md5)

{

    //...

}