Wednesday, March 28, 2012

Set ulimit parameters on ubuntu


Set ulimit parameters on ubuntu

By default the number of open files  pro user in Ubuntu 8.04   is 1024. In my case this number was  too small so I have to increase it.This is done with the  ulimit command:
$ulimit -a   # see all the kernel parameters
$ulimit -n   #see the number of open files
$ulimit -n 9000  #  set the number open files to 9000
The problem with this way is that the ulimit parameter is only set currently  for this command terminla and user.If you open a new tab and type again ulimit -a you will see that the number of open files is 1024.This means that after a reboot you’ll need to set the parameter again.
First, in order to set this options automatically  you have to edit theetc/security/limits.conf file.
$sudo gedit /etc/security/limits.conf    #open the file in gedit
The # means that this part is commented.The wildcard * means  for all users.We need to set the nofile option meaning maximum number of open files.If you want to change the number of files of user, you should add this line in the limits.conf:
user  soft  nofile 9000
user  hard  nofile 65000
If  you want to set the nofile only for superuser you just write root instead of user.
root soft  nofile 9000
root hard  nofile 65000
Second you have to add a line in the /etc/pam.d/common-session file:
$ sudo gedit /etc/pam.d/common-session #open the file in gedit
Then add the line:
session required pam_limits.so
Now after rebooting you can see in the terminal with ulimit -a the change.
The option with wildcard *didn’t work for me , because I used root accout to run my programms and wildcard option doesn’t affect the superuser.
Remark: Using the same steps you should be able to set and change other parameters ( core file size, max user processes, stack size ….) from the ulimit options.
References:

Saturday, March 17, 2012

Android Connect to FTP server example


Connect to FTP server example

This is the android coding example showing how to connect to FTP server and some basic file operations such as downloading, uploading, deleting, renaming file, and creating new directory.
You must import the following FTP client library, download it here:
import org.apache.commons.net.ftp.*;

Now, declare a public FTP client object.
public FTPClient mFTPClient = null;

Method to connect to FTP server:
public boolean ftpConnect(String host, String username,
                          String password, int port)
{
    try {
        mFTPClient = new FTPClient();
        // connecting to the host
        mFTPClient.connect(host, port);

        // now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
            // login using username & password
            boolean status = mFTPClient.login(username, password);

            /* Set File Transfer Mode
             *
             * To avoid corruption issue you must specified a correct
             * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
             * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
             * for transferring text, image, and compressed files.
             */
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

            return status;
        }
    } catch(Exception e) {
        Log.d(TAG, "Error: could not connect to host " + host );
    }

    return false;
}

Method to disconnect from FTP server:
public boolean ftpDisconnect()
{
    try {
        mFTPClient.logout();
        mFTPClient.disconnect();
        return true;
    } catch (Exception e) {
        Log.d(TAG, "Error occurred while disconnecting from ftp server.");
    }

    return false;
}

Method to get current working directory:
public String ftpGetCurrentWorkingDirectory()
{
    try {
        String workingDir = mFTPClient.printWorkingDirectory();
        return workingDir;
    } catch(Exception e) {
        Log.d(TAG, "Error: could not get current working directory.");
    }

    return null;
}

Method to change working directory:
public boolean ftpChangeDirectory(String directory_path)
{
    try {
        mFTPClient.changeWorkingDirectory(directory_path);
    } catch(Exception e) {
        Log.d(TAG, "Error: could not change directory to " + directory_path);
    }

    return false;
}

Method to list all files in a directory:
public void ftpPrintFilesList(String dir_path)
{
    try {
        FTPFile[] ftpFiles = mFTPClient.listFiles(dir_path);
        int length = ftpFiles.length;

        for (int i = 0; i < length; i++) {
            String name = ftpFiles[i].getName();
            boolean isFile = ftpFiles[i].isFile();

            if (isFile) {
                Log.i(TAG, "File : " + name);
            }
            else {
                Log.i(TAG, "Directory : " + name);
            }
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
}

Method to create new directory:
public boolean ftpMakeDirectory(String new_dir_path)
{
    try {
        boolean status = mFTPClient.makeDirectory(new_dir_path);
        return status;
    } catch(Exception e) {
        Log.d(TAG, "Error: could not create new directory named " + new_dir_path);
    }

 return false;
}

Method to delete/remove a directory:
public boolean ftpRemoveDirectory(String dir_path)
{
    try {
        boolean status = mFTPClient.removeDirectory(dir_path);
        return status;
    } catch(Exception e) {
        Log.d(TAG, "Error: could not remove directory named " + dir_path);
    }

    return false;
}

Method to delete a file:
public boolean ftpRemoveFile(String filePath)
{
    try {
        boolean status = mFTPClient.deleteFile(filePath);
        return status;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

Method to rename a file:
public boolean ftpRenameFile(String from, String to)
{
    try {
        boolean status = mFTPClient.rename(from, to);
        return status;
    } catch (Exception e) {
        Log.d(TAG, "Could not rename file: " + from + " to: " + to);
    }

    return false;
}

Method to download a file from FTP server:
/**
 * mFTPClient: FTP client connection object (see FTP connection example)
 * srcFilePath: path to the source file in FTP server
 * desFilePath: path to the destination file to be saved in sdcard
 */
public boolean ftpDownload(String srcFilePath, String desFilePath)
{
    boolean status = false;
    try {
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
        status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
        desFileStream.close();

        return status;
    } catch (Exception e) {
        Log.d(TAG, "download failed");
    }

    return status;
}

Method to upload a file to FTP server:
/**
 * mFTPClient: FTP client connection object (see FTP connection example)
 * srcFilePath: source file path in sdcard
 * desFileName: file name to be stored in FTP server
 * desDirectory: directory path where the file should be upload to
 */
public boolean ftpUpload(String srcFilePath, String desFileName,
                         String desDirectory)
{
    boolean status = false;
    try {
        FileInputStream srcFileStream = new FileInputStream(srcFilePath);

        // change working directory to the destination directory
        if (ftpChangeDirectory(desDirectory)) {
            status = mFTPClient.storeFile(desFileName, srcFileStream);
        }

        srcFileStream.close();
        return status;
    } catch (Exception e) {
        Log.d(TAG, "upload failed");
    }

    return status;
}