Linux ate my RAM?!

What’s going on?

Linux is borrowing unused memory for disk caching. This makes it looks like you are low on memory, but you are not! Everything is fine!

Why is it doing this?

Disk caching makes the system much faster! There are no downsides, except for confusing newbies. It does not take memory away from applications in any way, ever!

What if I want to run more applications?

If your applications want more memory, they just take back a chunk that the disk cache borrowed. Disk cache can always be given back to applications immediately! You are not low on ram!

Do I need more swap?

No, disk caching only borrows the ram that applications don’t currently want. It will not use swap. If applications want more memory, they just take it back from the disk cache. They will not start swapping.

How do I stop Linux from doing this?

You can’t disable disk caching. The only reason anyone ever wants to disable disk caching is because they think it takes memory away from their applications, which it doesn’t! Disk cache makes applications load faster and run smoother, but it NEVER EVER takes memory away from them! Therefore, there’s absolutely no reason to disable it!

Why does top and free say all my ram is used if it isn’t?

This is just a misunderstanding of terms. Both you and Linux agrees that memory taken by applications is "used", while memory that isn’t used for anything is "free".

But what do you call memory that is both used for something and available for applications?

You would call that "free", but Linux calls it "used".

Memory that is: taken by applications
You’d call it: Used
Linux calls it: Used

Memory that is: available for applications, and used for something
You’d call it: Free
Linux calls it: Used

Memory that is: not used for anything
You’d call it: Free
Linux calls it: Free

This "something" is what top and free calls "buffers" and "cached". Since your and Linux’s terminology differs, you think you are low on ram when you’re not.

How do I see how much free ram I really have?

Too see how much ram is free to use for your applications, run free -m and look at the row that says "-/+ buffers/cache" in the column that says "free". That is your answer in megabytes:

$ free -m
             total       used       free     shared    buffers     cached
Mem:          1504       1491         13          0         91        764
-/+ buffers/cache:        635        869
Swap:         2047          6       2041
$

If you don’t know how to read the numbers, you’ll think the ram is 99% full when it’s really just 42%.

How to Find and Check Number of Connections to a Server

Whenever a client connects to a server via network, a connection is established and opened on the system. On a busy high load server, the number of connections connected to the server can be run into large amount till hundreds if not thousands. Find out and get a list of connections on the server by each node, client or IP address is useful for system scaling planning, and in most cases, detect and determine whether a web server is under DoS or DDoS attack (Distributed Denial of Service), where an IP sends large amount of connections to the server. To check connection numbers on the server, administrators and webmasters can make use of netstat command.
Below is some of the example a typically use command syntax for ‘netstat’ to check and show the number of connections a server has. Users can also use ‘man netstat’ command to get detailed netstat help and manual where there are lots of configurable options and flags to get meaningful lists and results.

netstat –na

Display all active Internet connections to the servers and only established connections are included.

netstat -an | grep :80 | sort

Show only active Internet connections to the server at port 80 and sort the results. Useful in detecting single flood by allowing users to recognize many connections coming from one IP.

netstat -n -p|grep SYN_REC | wc –l

Let users know how many active SYNC_REC are occurring and happening on the server. The number should be pretty low, preferably less than 5. On DoS attack incident or mail bombed, the number can jump to twins. However, the value always depends on system, so a high value may be average in another server.

netstat -n -p | grep SYN_REC | sort –u

List out the all IP addresses involved instead of just count.

netstat -n -p | grep SYN_REC | awk '{print $5}' | awk -F: '{print $1}'

List all the unique IP addresses of the node that are sending SYN_REC connection status.

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort –n

Use netstat command to calculate and count the number of connections each IP address makes to the server.

netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort –n

List count of number of connections the IPs are connected to the server using TCP or UDP protocol.

netstat -ntu | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort –nr

Check on ESTABLISHED connections instead of all connections, and displays the connections count for each IP.

netstat -plan|grep :80|awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1

Show and list IP address and its connection count that connect to port 80 on the server. Port 80 is used mainly by HTTP web page request.

mydigitallife.info

Cài đặt ứng dụng vào thẻ SD Card

Android-2.2-Install-Android-Apps-to-SD-Card

Chú Milestone xài Android 2.1 lạc hậu đã vừa được nâng cấp lên v.2.2 những ngày gần đây, một sửa đổi nhỏ đã giải quyết được rất nhiều vấn đề lớn là việc move được ứng dụng (apps) qua thẻ nhớ (SD Card) qua giao diện quản lý ứng dụng mà không cần phải "độ" thêm bất cứ thứ gì.

Cà phê muối

Cà phê, một thứ đắng nghét. Muối, quá trời mặn. Khi ông trời đã đặt tên cho đối trọng thì cớ sao người đời đem pha cà phê với muối, cộng hưởng vị đắng chát. Nghe qua thật trớ trêu. Này nha, người chán đời lại tìm đến rượu giải sầu, có lẽ rượu say thì quên hết sự đời, thế rồi tỉnh ra lại nhớ. Đã dấn sâu vào chiêu thức này, tức không đi tìm đối trọng mà kiếm tìm cộng hưởng, không chuốc say được suốt đời, người đời quyết tâm tự vẫn. Đúng rồi, đã tự vẫn thì tỉnh thế quái nào nữa…

Nói lông bông để biết cà phê muối hay ở chỗ đi tìm cái cộng hưởng. Đắng, cà phê rất đắng, pha thêm muối quả thực càng thêm đắng. Nhấp miếng đầu tiên thật sự gian nan, nhấp tiếp từng miếng, từng miếng tiếp theo, cái vị đắng bỗng ngọt dần. Ghiền.

Đã là cuộc sống, đắng cay khổ đau ngọt bùi hạnh phúc vẽ không đẹp như hình sin mà một mớ lùng bùng zíc zắc. Lúc thế này khi thế nọ. Như Ly cà phê, thêm đường, ngọt, thêm muối, chát.

Cuộc sống, thấp thoáng đâu đó là bóng dáng ly cà phê. Người tỉnh uống ly cà phê muối. Người mật ngọt chọn ly đường. Để đến lúc đặt lên bàn ly cà phê muối, sẽ bị ly cà phê đường đạp văng đi. Để đến lúc cà phê đường sảy chân vỡ, Người nghiến răng khi chạm mặt ly cà phê muối, bỏ, dang dở, run rẩy, thấy Người không muốn tận hưởng cà phê tiếp nữa…

Người có biết, đằng sau vị mặn đắng kia là hạnh phúc thực thụ.

FTP Client

(Bài này là đồ án hồi sv năm 2-3 gì đó) :-)

  • Định nghĩa

FTP (File Transfer Protocol) là giao thức truyền tập tin giữa các máy tính trên hệ thống mạng, chẳng hạn như mạng Internet. Ta có thể dùng FTP để truy cập tài nguyên từ xa trên một máy tính khác, một tài khoản từ máy chủ nào đó, chẳng hạn như tài khoản website cá nhân hoặc công ty.

FTP được định nghĩa chi tiết trong RFC-959[1].

- Anonymous FTP (truy cập cộng đồng):

Đây thực chất là một tài khoản công cộng cho bất cứ người dùng nào cũng có thể truy cập vào tài nguyên mà server muốn chia sẻ (các dịch vụ chia sẻ file miễn phí). Các nguồn tài nguyên này thường được giới thiệu trên mạng, người dùng chỉ việc click vào và trình duyệt web sẽ tự động mở truy cập. Thông thường các link chia sẻ đều có dạng ftp://domain.com. Ví dụ: ftp://mysql.mirror.kangaroot.net/pub/mysql/

- Tài khoản FTP

Khác với các dịch vụ chia sẻ file công cộng, các dịch vụ truyền file khác đều cung cấp từng tài khoản FTP cụ thể cho từng người dùng để bảo mật thông tin. Hầu hết các máy chủ Web đều được người quản trị liên lạc để quản lý file đều sử dụng giao thức FTP.

Mỗi tài khoản kiểu này đều được khai báo với tên đăng nhập và mật khẩu riêng, chỉ người sở hữu mới nắm được thông tin, và người sở hữu phải xác thực với server trước mỗi phiên kết nối. Thực tế tài khoản Anonymous FTP cũng có tên đăng nhập là anonymous với mật khẩu rỗng. Các ứng dụng duyệt web (và các ứng dụng truy cập khác) đều ngầm hiểu và tự động khai báo giúp người dùng.

  • Cơ chế hoạt động

Ở mỗi phiên hoạt động, các ứng dụng FTP Client khởi tạo socket kết nối tới dịch vụ FTP của máy chủ ở cổng (Port) quy ước. Hầu hết các cổng kết nối đến server đều là cổng 21, trong một vài trường hợp nhằm che dấu thông tin, các quản trị của server thay đổi thành cổng khác để hạn chế sự nhòm ngó từ các máy quét bên ngoài. Quá trình kết nối này gọi là Control Channel connection.

Giao thức FTP có hai hệ kết nối là hệ thường (Normal FTP) và hệ bị động (Passive FTP). Trong đó hệ bị động được bảo mật hơn hệ thông thường.

Normal FTP (còn gọi là Active Mode):

Ứng dụng client khởi tạo kèm một port mới kết nối tới cổng 21 của sever (bằng lệnh PORT [port]) tạo ra kết nối port-to-port (quá trình này gọi là Data Channel). Ứng dụng FTP server sẽ dùng cổng 20 kết nối tới client qua một cổng mới nào đó (lớn hơn 1023). Vì client không thể điều khiển được quá trình này nên dữ liệu sẽ bị kiểm soát và chặn bởi tường lửa. (Nếu không có tường lửa thì việc vào ra bất hợp pháp một cách thoải mái trên mọi cổng của client sẽ rất dễ bị khai thác)

Passive FTP (Chế độ Bị động)

Client sẽ thông báo cho server khi sử dụng cơ chế này qua lệnh PASV thay cho lệnh PORT ở trên. PASV sẽ “hỏi” server cổng mà server thông tin về Data Channel server muốn tương tác, sau đó bên cạnh port 21, client chủ động kết nối tới cổng “mong muốn” của server. Vì client sẽ chủ động khởi tạo kết nối nên firewall (tường lửa) kiểm soát được và cho phép thực hiện luồng dữ liệu này.

Khi đã kết nối, server và client sẽ trao đổi với nhau qua các lệnh command được định nghĩa chung từ RFC hoặc các command riêng được định nghĩa bởi server.

CÁC COMMAND THƯỜNG GẶP

* ABOR – ngưng lệnh đang thực hiện

* CWD – thay đổi thư mục làm việc

* DELE – xoá file

* LIST – xem danh sách files

* MDTM – trả về thông tin sửa đổi của files

* MKD – tạo thư mục mới

* NLST – danh sách tên các thư mục

* PASS – gửi nội dung password

* PASV – chế độ PASV

* PORT – mở một cổng dữ liệu

* PWD – hỏi thư mục đang thực hiện

* QUIT – tắt phiên kết nối

* RETR – nhận một file về local

* RMD – xoá một thư mục

* RNFR – đổi tên từ

* RNTO – đổi tên tới

* SITE – hỏi các command riêng của site

* SIZE – kích thước một file

* STOR – lưu trữ file lên server

* TYPE – kiểu truyền của dữ liệu (binary/ ascii)

* USER – gửi thông tin username

  • Chú thích

[1] http://www.faqs.org/rfcs/rfc959.html

[2] http://en.wikipedia.org/wiki/SSH_file_transfer_protocol

Sample FTP Class (C#)

#region FTP Actions
private void readReply()
{
    receivedMsg = "";
    reply = readLine();
    if (reply != "") replyValue = Int32.Parse(reply.Substring(0, 3));
}

private void cleanup()
{
    if (clientSocket != null)
    {
        clientSocket.Close();
        clientSocket = null;
    }
    onLogin = false;
}

private string readLine()
{

    while (true)
    {
        try
        {
            bytes = clientSocket.Receive(buffer, buffer.Length, 0);
        }
        catch { setStatus("Reading error"); return ""; }
        receivedMsg += ASCII.GetString(buffer, 0, bytes);
        if (bytes < buffer.Length)         {             break;         }     }     char[] seperator = { '\n' };     string[] mess = receivedMsg.Split(seperator);     if (receivedMsg.Length > 2)
    {
        receivedMsg = mess[mess.Length - 2];
    }
    else
    {
        receivedMsg = mess[0];
    }

    if (!receivedMsg.Substring(3, 1).Equals(" "))
    {
        return readLine();
    }

    for (int k = 0; k < mess.Length - 1; k++)
    {
        setStatus(mess[k]);
    }
    return receivedMsg;
}

private void sendCommand(String command)
{

    Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
    try
    {
        clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
    }
    catch
    {
        setStatus("Error on Connecting to remote!");
        return;
    }
    readReply();
}

private Socket createTranmissonSocket()
{

    sendCommand("PASV");

    if (replyValue != 227)
    {
        setStatus(reply.Substring(4));
        close();
    }

    int index1 = reply.IndexOf('(');
    int index2 = reply.IndexOf(')');
    string ipData = reply.Substring(index1 + 1, index2 - index1 - 1);
    int[] parts = new int[6];

    int len = ipData.Length;
    int partCount = 0;
    string buf = "";

    for (int i = 0; i < len && partCount <= 6; i++)
    {

        char ch = Char.Parse(ipData.Substring(i, 1));
        if (Char.IsDigit(ch))
            buf += ch;
        else if (ch != ',')
        {
            throw new IOException("Malformed PASV reply: " + reply);
        }

        if (ch == ',' || i + 1 == len)
        {

            try
            {
                parts[partCount++] = Int32.Parse(buf);
                buf = "";
            }
            catch (Exception)
            {
                throw new IOException("Malformed PASV reply: " + reply);
            }
        }
    }

    string ipAddress = parts[0] + "." + parts[1] + "." +
      parts[2] + "." + parts[3];

    int port = (parts[4] << 8) + parts[5];

    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(ipAddress).AddressList[0], port);

    try
    {
        s.Connect(ep);
    }
    catch (Exception)
    {
        setStatus("Can't connect to remote server");
    }

    return s;
}
#endregion

Download this class: http://vietfov.net/soft/download.php?f=FTP.zip