资源简介
完全地利用面向对象开发,由达内唐亮老师做框架设计,本人自己独立完成,思路清晰,有需要的可以下载下来学习,是javase阶段不错的学习材料。
代码片段和文件信息
package client;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.objectInputStream;
import java.io.objectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
import entity.Request;
import entity.Response;
public class Action {
private Socket socket;
private Properties pro;
public Action() {
pro = new Properties();
loadProperties();
}
public Response doAction(Request req) {
objectOutputStream oos = null;
objectInputStream ois = null;
createSocket();
try {
oos = new objectOutputStream(socket.getOutputStream());
oos.writeobject(req);
oos.flush();
ois = new objectInputStream(socket.getInputStream());
Response res = (Response) ois.readobject();
return res;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
closeStream(oos ois);
}
return null;
}
private void closeStream(objectOutputStream oos objectInputStream ois) {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
closeSocket();
}
/**
* 加载客户端配置参数的方法.
*
*/
private void loadProperties() {
try {
pro.load(new FileInputStream(“client.cfg“));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建连接的方法,该方法将在doAction方法中调用。
*
*/
private void createSocket() {
try {
socket = new Socket(pro.getProperty(“ServerIP“) Integer
.parseInt(pro.getProperty(“ServerPort“)));
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭连接的方法,该方法将在doAction方法中调用。
*
*/
private void closeSocket() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 上一篇:Java软著代码整理开源工具
- 下一篇:JUDE professional 破解版
评论
共有 条评论