⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 todayclient.java

📁 RMI通讯
💻 JAVA
字号:
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 今日焦点客户端
 */
public class TodayClient {
	// 服务器监听主机和端口号
	public static String HOST = "127.0.0.1";
	public static int PORT = 8080;

	// 客户端套接字
	private Socket client = null;

	// 传输流
	private ObjectInputStream ois;

	// 主窗体对象配置信息传递对象
	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);
			}
		});
	}

	// 连接服务器
	public void connectServer() {
		try {
			client = new Socket(HOST, PORT);
		} catch (UnknownHostException e) {
			// 使用日志处理文件 重定向
		} catch (IOException e) {
			// 使用日志处理文件
		}
	}

	/**
	 * 
	 * @return Object 如果出现异常,返回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(), "服务器关闭,客户端将要关闭");
			e.printStackTrace();
			return null;
    	}// finally {
//			try {
//				ois.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
	}

	// 关闭套接字
	public void close() {
		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new TodayClient();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -