package test;
import java.io.BufferedReader;
import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket;import java.net.UnknownHostException;public class DailyAdviceClient {
public void go(){ try { Socket s = new Socket("127.0.0.1",5000); InputStreamReader ir = new InputStreamReader(s.getInputStream()); BufferedReader br = new BufferedReader(ir); String Advice = br.readLine(); System.out.println(Advice); br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { DailyAdviceClient d = new DailyAdviceClient(); d.go(); }}
package test;
import java.io.IOException;
import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.rmi.server.SocketSecurityException;public class DailyAdviceServer {
static String[] words = {"aaaaa","bbbbb","ccccc"};
public static void go(){ try { ServerSocket ss = new ServerSocket(5000); while(true){ Socket s = ss.accept(); PrintWriter pw = new PrintWriter(s.getOutputStream()); String Advice = getAdvice(); pw.println(Advice); pw.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }private static String getAdvice() {
int random =(int)( Math.random() * words.length); return words[random]; }}