乙個NIO例子

2021-08-30 04:19:01 字數 1780 閱讀 6007

例子完成的功能是,客戶端傳送命令time到服務端,服務端返回當前時間給客戶端。

服務端邏輯**

public

class

multiplexertimeserverhandler

implements

runnable

catch

(ioexception e)

}public

void

stop()

public

void

run(

)catch

(ioexception e)}}

}catch

(ioexception e)

}//多路復用器關閉後,所有註冊的channel和pipe等資源都會自動去註冊並關閉

// ,所有不需要重複釋放資源

if(selector != null)

catch

(ioexception e)}}

//處理事件

private

void

handleinput

(selectionkey key)

throws ioexception

if(key.

isreadable()

)else

if(read <0)

else}}

}private

void

dowrite

(socketchannel sc, string content)

throws ioexception

}}

服務端啟動**

public

class

timeserver

}

客戶端邏輯**

public

class

timeclienthandler

implements

runnable

catch

(ioexception e)

}public

void

run(

)catch

(ioexception e)

while

(!stop)

}catch

(ioexception e)}if

(selector != null)

catch

(ioexception e)}}

private

void

handleinput

(selectionkey key)

throws ioexception

else}if

(key.

isreadable()

)else

if(read <0)

else}}

}private

void

doconnect()

throws ioexception

else

}private

void

dowrite

(socketchannel socketchannel)

throws ioexception

}}

客戶端啟動**

public

class

timeclient

}

乙個cmd ftpscript 例子

乙個例子,每日自動更新norton病毒庫的 bat檔案 dir e software symantec viruslib sy exe pause ftp s cescript.txt rem e software symantec viruslib symcdefsx86.exe q copy e...

乙個Makefile例子

main.c include log.h intmain sum.c include log.h void sum sub.c include log.h void sub dev.c include log.h void sub mul.c include log.h void sub log.h...

乙個Lucene例子

public void test1 迴圈陣列,建立文件,給文件新增字段,並把文件新增到索引書寫器裡 document doc null for inti 0 i words.length i 索引優化 ramwriter.optimize 關閉索引讀寫器,一定要關哦,按理說應該把上面的 用try括主...