本文對編寫客戶服務器應用的java程序員有所幫助
目前java平台已經廣泛應用於各類客戶/服務器系統中
網絡異步應用涉及到如下幾個關鍵點
◇ 客戶應用啟動後
◇ 客戶應用和服務應用在數據通信中
該網絡異步編程首先涉及到一個定時器和定時器事件
定時器包含在網絡通信類中
客戶程序類如下結構
public class NetComm
implements ActionListener
{
javax
Socket sock;
private EventNotifier en;
public static int net_state =
InetAddress ServerAddr;
int ServerPort;
public NetComm(InetAddress addr
ServerAddr = addr;
ServerPort = port;
}
public void NetComm_Init() {
net_state =
try {
sock = new Socket(ServerAddr
} catch (IOException e) {
net_state =
}
timer
}
public void NetComm_Data()
{
try {
OutputStream outputstream = sock
BufferedWriter out = new BufferedWriter
(new OutputStreamWriter(outputstream));
out
out
BufferedReader in = new BufferedReader
(new InputStreamReader(sock
boolean more = true;
while(more) {
String str = in
if(str == null) more = false;
else
// 處理數據
System
}
in
} catch (IOException e) {
NetComm_Close();
net_state =
}
timer
}
public void NetComm_Close()
{
if(sock != null)
try{
sock
} catch ( IOException e) {
}
}
public void actionPerformed(ActionEvent e)
{
if(net_state ==
NetComm_Init();
else
NetComm_Data();
}
}
在以上程序中
作者簡介
郭洪鋒
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27252.html