kustudy(酷学)
kustudy
全部博文(77)
Spring(0)
Hibernate(0)
Struts(0)
VC(0)
C++(0)
C(1)
Linux SHELL编程(0)
Linux进程间通信(0)
Linux常用命令(0)
01_安装(7)
Linux视频专集(0)
Java 代码质量(0)
Java 设计模式与(0)
Java 安全(0)
Java 多线程与并(0)
Java Web 技术(0)
Java GUI 开发(0)
Java EE 技术(0)
Java 核心技术(1)
Java 新手入门(0)
Java XML(0)
2011年(1)
2009年(76)
fkyou2d
brooks
yueshigo
vcdtwo
格伯纳
bluebird
wujiajia
yxw03082
qq李贵林
分类: Java
2009-07-10 23:04:33
/**
*1.AtomsData.java 对象类,实现Serializable接口
*/
import java.io.Serializable; public class AtomsData implements Serializable { private static final long serialVersionUID = 1L; private String version; private int[] atomsArray; AtomsData() { } AtomsData(String version, int[] atomsArray) { this.version = version; this.atomsArray = atomsArray; } public int[] getAtomsArray() { return atomsArray; } public void setAtomsArray(int[] atomsArray) { this.atomsArray = atomsArray; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String toString() { return "AtomsData info:" + this.version; } }
/** *2. ServerHandle.java 服务器对象操作类 */
*2. ServerHandle.java 服务器对象操作类
import java.io.IOException; import java.io.ObjectInputStream; import java.net.Socket; public class ServerHandle implements Runnable { private Socket socket; public ServerHandle(Socket socket) { this.socket = socket; } private ObjectInputStream getAtomsReceive(Socket socket) throws IOException { return new ObjectInputStream(socket.getInputStream()); } public void run() { try { ObjectInputStream ois = this.getAtomsReceive(socket); Object o = ois.readObject(); while (o != null) { AtomsData atomsdata = (AtomsData) o; System.out.println("The version of operation is "
+ atomsdata.getVersion() + " ."); } } catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { if (socket != null) socket.close(); } catch (Exception e) { e.printStackTrace(); } } } }
* 3.AtomsServer.java 服务类
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class AtomsServer { private int port = 8833; private ServerSocket serverSocket; private ExecutorService executorService; private final int POOL_SIZE = 10; public AtomsServer() { try { serverSocket = new ServerSocket(port); executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_SIZE); System.out.println("The server has started!"); } catch (IOException e) { System.out.println(e.getMessage()); } } public void serviceStart() { while (true) { Socket socket = null; try { socket = serverSocket.accept(); executorService.execute(new ServerHandle(socket)); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { new AtomsServer().serviceStart(); } }
*4.AtomsClient.java 客户端类
import java.io.IOException; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class AtomsClient { public static void main(String args[]) throws InterruptedException { ExecutorService exec = Executors.newFixedThreadPool(2); for (int index = 0; index < 10; index++) { Runnable run = new Runnable() { private Socket socket = null; private int port = 8833; AtomsData atomsdata; ObjectOutputStream oos = null; public void run() { while (true) { try { socket = new Socket("localhost", port); atomsdata = new AtomsData(); atomsdata.setVersion((new Double((long) (Math.random() * 1000))).toString()); oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(atomsdata); } catch (IOException e) { e.printStackTrace(); } finally { try { oos.close(); } catch (Exception e) { e.printStackTrace(); } } } } }; exec.execute(run); } exec.shutdown(); } }
上一篇:中科院ORACLE数据库视频教程总结
下一篇:JAVA 数据传输对象DTO(Data Transfer Object)基本概念
登录 注册