摘要:
实现一个伪分布式打印远程通信程序。
Master:实现接收客户端消息,并分发给底层slaver打印消息。
1 package com.cn.ludy.master; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.ServerSocket; 8 import java.net.Socket; 9 import java.util.ArrayList; 10 11 //master负责两项工作,1,注册;2,分发任务。 12 //1,注册包括接收slaver的注册,将slaver的服务端口保存到list表中 13 //2,分发任务包括接收client发来的数据,交给list表中任意一个salver执行 14 public class Master { 15 // master接收client数据的端口 16 private int clientPort = 10000; 17 // master接收salver注册的端口 18 private int slaverPort = 10001; 19 // salver的注册list 20 private static ArrayListslaverMap; 21 22 public Master() { 23 slaverMap = new ArrayList (); 24 } 25 26 public void start() { 27 // 启动侦听slaver线程 28 Thread tSlaver = new listenSlaver(); 29 tSlaver.start(); 30 31 // 启动侦听client线程 32 Thread tClient = new listenClient(); 33 tClient.start(); 34 } 35 36 // 侦听client 37 class listenClient extends Thread { 38 private ServerSocket server2Client; 39 private Socket client; 40 private BufferedReader in; 41 42 public listenClient() { 43 try { 44 server2Client = new ServerSocket(clientPort); 45 } catch (IOException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 } 50 51 @Override 52 public void run() { 53 System.out.println("启动了一个监听client的线程"); 54 while (true) { 55 try { 56 client = server2Client.accept(); 57 System.out.println("监听到一个client"); 58 in = new BufferedReader(new InputStreamReader(client.getInputStream())); 59 60 // 获取client需要打印值 61 String line = in.readLine(); 62 while (line != null) { 63 System.out.println("Master:from client:" + client.getInetAddress() + ",客户端输入:" + line); 64 65 // 找到空闲slaver,命令其打印 66 int slaverPort = slaverMap.get((int) (Math.random() * slaverMap.size())); 67 68 System.out.println("任务交给" + slaverPort); 69 System.out.println("任务:" + line); 70 Socket scoket2Slaver = new Socket("127.0.0.1", slaverPort); 71 72 PrintWriter out2Slaver = new PrintWriter(scoket2Slaver.getOutputStream(), true); 73 ; 74 75 out2Slaver.println("任务:" + line); 76 out2Slaver.flush(); 77 scoket2Slaver.close(); 78 79 line = in.readLine(); 80 81 System.out.println("一次任务执行完毕,等待……"); 82 } 83 client.close(); 84 } catch (IOException e) { 85 System.out.println("error:Master:start"); 86 } 87 } 88 } 89 } 90 91 // 侦听slaver 92 class listenSlaver extends Thread { 93 private ServerSocket server2Slaver; 94 private Socket slaver; 95 private BufferedReader in; 96 97 public listenSlaver() { 98 try { 99 server2Slaver = new ServerSocket(slaverPort);100 } catch (IOException e) {101 // TODO Auto-generated catch block102 e.printStackTrace();103 }104 }105 106 @Override107 public void run() {108 System.out.println("启动了一个监听slaver的线程");109 try {110 while (true) {111 slaver = server2Slaver.accept();112 System.out.println("有一个slaver注册成功,slaver port:" + slaver.getPort());113 114 in = new BufferedReader(new InputStreamReader(slaver.getInputStream()));115 116 String line = in.readLine();117 118 while (line != null) {119 // 获取slaver的port值,注册120 slaverMap.add(Integer.parseInt(line.substring(12)));121 System.out.println("Master:from slaver:" + slaver.getInetAddress() + ":" + slaver.getPort()122 + ",其服务端口:" + line.substring(12));123 line = in.readLine();124 }125 slaver.close();126 }127 } catch (IOException e) {128 System.out.println("error:Master:listenSlaver");129 }130 }131 }132 133 public static void main(String[] args) {134 new Master().start();135 }136 }
MasterSlaver:实现打印消息。
1 package com.cn.ludyhu.masterserver; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.ServerSocket; 8 import java.net.Socket; 9 import java.net.UnknownHostException;10 11 public class MasterSlaver 12 {13 //master的IP 与 监听port14 private String masterAddr = "127.0.0.1";15 private int masterRegitePort = 10001;16 17 //slaver接受任务的服务端口18 private ServerSocket server; 19 private int serverPort = 8081;20 private Socket client; 21 private BufferedReader in; 22 private PrintWriter out; 23 24 public MasterSlaver()25 {26 27 }28 //连接master,注册29 public void register()30 {31 try {32 //连接master,并向其发送自己的服务端口号33 Socket registSocket = new Socket(masterAddr,masterRegitePort);34 String reportMsg = "i am slaver:"+serverPort;35 36 //与master的注册连接发送流,发送自己的服务端口号37 out = new PrintWriter(registSocket.getOutputStream(),true); 38 out.println(reportMsg);39 out.flush();40 registSocket.close();41 42 System.out.println("我是slaver "+serverPort+"已经向master"+masterAddr+":"+masterRegitePort+"注册"); 43 } catch (UnknownHostException e) {44 // TODO Auto-generated catch block45 e.printStackTrace();46 } catch (IOException e) {47 // TODO Auto-generated catch block48 e.printStackTrace();49 }50 }51 52 //启动监听服务端口,接收master调用,完成打印任务53 public void start()54 {55 try 56 { 57 server = new ServerSocket(serverPort); 58 while (true) 59 { 60 client = server.accept(); 61 //接受master调用,开始执行打印任务62 System.out.println("开始执行打印任务:");63 64 in = new BufferedReader(new InputStreamReader(client.getInputStream())); 65 out = new PrintWriter(client.getOutputStream(),true); 66 67 String line = in.readLine(); 68 while (line != null) 69 { 70 System.out.println("getTask : from master:"+line);71 line = in.readLine(); 72 } 73 System.out.println("任务执行完毕,服务端口为:"+serverPort);74 out.println("任务执行完毕,服务端口为:"+serverPort+"---下次任务再见!---"); 75 out.close();76 client.close(); 77 } 78 } 79 catch (IOException e) 80 {81 System.out.println("error:masterSlaver "+serverPort+" in method start");82 } 83 }84 85 86 public static void main(String[] args)87 {88 MasterSlaver slaver = new MasterSlaver();89 slaver.register();90 slaver.start();91 }92 93 }
Client:向Master发送消息。
1 package com.cn.ludy.client; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.Socket; 8 9 public class Client {10 Socket client;11 BufferedReader in;12 PrintWriter out;13 14 public Client() {15 }16 17 public void start() {18 String localhost = "127.0.0.1";19 try {20 // 连接master21 client = new Socket(localhost, 10000);22 in = new BufferedReader(new InputStreamReader(client.getInputStream()));23 out = new PrintWriter(client.getOutputStream(), true);24 BufferedReader line = new BufferedReader(new InputStreamReader(System.in));25 26 while (true) {27 String str = line.readLine();28 out.println(str);29 out.flush();30 if (str.equals("end")) {31 break;32 }33 }34 } catch (IOException e) {35 System.out.println("error:client:start");36 }37 }38 39 public static void main(String[] args) {40 new Client().start();41 }42 43 }