發表文章

epoll - I/O event notification facility

最近要 寫一 支 Network Service Daemon 的程式,本來想用 select 來寫,後來印象中有看過其他的解決方案,所以就稍微查了一下,於是就發現了今天的主角 ~ epoll 。epoll 是一套在 Linux 2.6上推出的 I/O Event 機制(其實是2.5.44,但因為我只看穩定版的關係,所以直接寫2.6)。 和 select 或是 poll 機制比起來,epoll 最大的優點是 它只會對Active的FD進行操作 。什麼叫作只會對 Active 的 FD 進行操作?回想一下在寫 select 的程式時, 總是會寫一個迴圈去掃描所有 FDSET 裏面的成員,去找到到底是哪一個 FD 有事件發生 ,而在epoll 裏面就不會這樣,它只會回傳有在運作的FD_SUBSET,實作的方式以後再研究吧,不過聽說是根據 FD 的 callback 函式有沒有被呼叫進行判別。其他的優點像是避開了select 的 FD_SETSIZE 限制之類的就不提了,因為感覺上這個參數是可以被改大的。 epoll 的函式只有三個: int epoll_create ( int size ); int epoll_ctl ( int epfd, int op, int fd, struct epoll_event *event ); int epoll_wait ( int epfd, struct epoll_event *events, int maxevents, int timeout ); 看名字並不難理解,所以就不加說明了,有問題就看下面的範例吧。下面是一支使用 epoll 的 TCP Echo Server 程式。Client 端就不附上了,反正改改也就是了。 # include < stdio.h > // for printf() and fprintf() # include < sys/socket.h > // for socket(), bind(), and connect() # include < arpa/inet.h > // for sockaddr_in and inet_ntoa() # include < st...

一隻檢查 IP 和 Hostname 格式的 JAVA 小程式

還是那句老話,我不喜歡JAVA。那這篇文章怎麼會存在呢?因為要解決同事開發Android軟體上所遇到的問題,所以不得不跳下來寫。 import java . util . regex . * ; public class CheckIPHostName { public static void main( String [] args) { String input; if (args.length > 0) { input = args[0]; } else { // No input was given. Print a message and exit. System .out.println( "Usage: java DateClient <server_host_name>" ); return ; } Pattern IpPattern = Pattern.compile( "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \\ .){3 } ([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ " ) ; Pattern HostPattern = Pattern.compile( "^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9 \\ -]*[a-zA-Z0-9]) \\ .) * ([A-Za-z]|[A-Za-z][A-Za-z0-9\\-]*[A-Za-z0-9])$ " ) ; Matcher matcherIp = IpPattern.matcher( input ); ...

如何用 Python 建立 HTTP Access Server

只是稍微紀錄一下。 Python 2.x python -m SimpleHTTPServer 9914 Python 3.x python -m http.server 9914

JAVA TCP Echo Server/Client Example Code

基本上我不喜歡 JAVA。倒不是說物件導向(Object-Oriented)的觀念不好,而是我不喜歡 一些簡單的事情也要要用物件導向的觀念來寫 (單單一個 Hello World 也要寫個 Class ...)。當然有人會說 C++ 不也是這樣,以 Hello World 來說,你也要先 include iostream 這個 Class,但起碼它看起來比較像 C,而且開發者不用特地去包一層物件。 抱怨完畢,現在要列在這裡的是 TCP 的 Server 和  Client 的範例程式,範例來源一樣是 http://www.cs.uic.edu/~troy/spring05/cs450/sockets/socket.html 。 在這個範例裡面,要特別注意的是 thread 的用法。在呼叫 start() 的時候,程式會去執行 run() 的函式,下面是 javadoc 的說明。 public void start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method). TCP Server import java . net . * ; import java . io . * ; class TCPEchoServer extends Thread { protected static boolean serverContinue = true ; protected Socket clientSocket ; public static void main ( String [ ] args ) throws IOException ...

JAVA UDP Echo Server/Client Example Code

說實話,我不會寫 JAVA。我個人的程式語言母語是 C++(感謝廖婉君教授),工作上比較常用的是 C,JAVA 還真的沒碰過。雖然我一直跟別人說,程式語言這東西,只要掌握住一種,要跨入其他語言並不困難,因為已經有最起碼的程式邏輯(迴圈、判斷式等),但其實我也很少去用其他的程式語言。最近因為工作的關係,不得不使用 JAVA,所以就紀錄一些 JAVA 的範例程式在這裡,以便未來使用。 在這裡要紀錄的是 UDP 的 Echo Server 和 Client 的程式。資料來源為  http://www.cs.uic.edu/~troy/spring05/cs450/sockets/socket.html 。 UDP Echo Server import java . io . * ; import java . net . * ; class UDPServer { public static void main ( String args [ ] ) throws Exception { int serverPort = 9876 ; try { if ( args . length > 0 ) { serverPort = new Integer ( args [ 0 ] ) . intValue ( ) ; } DatagramSocket serverSocket = new DatagramSocket ( serverPort ) ; byte [ ] receiveData = new byte [ 1024 ] ; byte [ ] sendData = new byte [ 1024 ] ; while ( true ) { receive...