[分享] Client/Server API
因為學校課業的需要
我將SocketServer和一些有的沒的IO handling包裝起來
讓使用者只需要專注於message handling就好
這裡有個簡易的echo server範例
http://cjcat.blogspot.com/2009/10/cjlibrary-java-clientserver-and-signals.html
CJLibrary Java
http://code.google.com/p/cjcat-java/
Documentation
http://cjcat-java.googlecode.com/svn/trunk/docs/index.html
需要繼承的class就這三個:Server, Client, 和SocketListener
基本操作流程如下:
1. 繼承給server和client用的SocketListener
實作mainLoop()作IO處理
可用getSocket()取得Socket
LazySocketListener已經將socket的IO stream包裝起來
可用getReader()和getWriter()來做IO handling
2. 繼承Server和Client
實作createListener()
使其回傳步驟1.所實作的SocketListener subclass物件
3. 開始撰寫主程式,以下code摘錄自echo server範例
//於port 2266開啟一個server
Server server = new EchoServer(2266);
server.start();
//創造一個client並連線到port 2266
Client client = new EchoClient();
client.connect("localhost", 2266);
//送出訊息,並且會印出echoed message
client.send("Hello.");
有興趣可以參考看看:)
其實實驗課的目標是做一個多人聊天室
我現在正在著手進行idv.cjcat.net.star package的開發
它包含利用idv.cjcat.net處理星狀網路的API
--
CJ Cat = Croa'J Cat = Cockroach Cat = 西街凱特 = 蜚蠊貓 = 蟑螂貓
Blog http://cjcat.blogspot.com
Gallery http://cjcat2266.deviantart.com
ptt2 Board CJWorkshop - 阿多比閃光(Adobe Flash)研討區
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.228.77.200
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 3 篇):