?? todayclient.java
字號(hào):
package com.vanceinfo.socket.client;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import com.vanceinfo.socket.today.Today;
/**
* @author 谷明亮
* @description 今日焦點(diǎn)客戶端
*/
public class TodayClient {
// 服務(wù)器監(jiān)聽(tīng)主機(jī)和端口號(hào)
public static String HOST = "127.0.0.1";
public static int PORT = 8080;
// 客戶端套接字
private Socket client = null;
// 傳輸流
private ObjectInputStream ois;
// 主窗體對(duì)象配置信息傳遞對(duì)象
private Today today;
// 主窗體
private MainFrame mainFrame = null;
public TodayClient() {
connectServer();
this.today = (Today) this.getConfigInfoObject();
mainFrame = new MainFrame(today);
mainFrame.getJfMain().addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TodayClient.this.close();
System.exit(0);
}
});
}
// 連接服務(wù)器
public void connectServer() {
try {
client = new Socket(HOST, PORT);
} catch (UnknownHostException e) {
// 使用日志處理文件 重定向
} catch (IOException e) {
// 使用日志處理文件
}
}
/**
*
* @return Object 如果出現(xiàn)異常,返回null
*/
public Object getConfigInfoObject() {
try {
ois = new ObjectInputStream(client.getInputStream());
Today temp = (Today) ois.readObject();
return temp;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
JOptionPane.showMessageDialog(mainFrame.getJfMain(), "服務(wù)器關(guān)閉,客戶端將要關(guān)閉");
e.printStackTrace();
return null;
}// finally {
// try {
// ois.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
// 關(guān)閉套接字
public void close() {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TodayClient();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -