import java.io.*;
import java.net.*;
public class Example13_1 {
   public static void main(String args[]) {
      String[] mess ={"您好！","我是李雷。","请问，韩梅梅在吗？"};
      Socket mysocket;
      DataInputStream in=null;
      DataOutputStream out=null;
      try{   System.out.println("李雷打电话找韩梅梅。");
mysocket=new Socket("127.0.0.1",2012);
            in=new DataInputStream(mysocket.getInputStream());
            out=new DataOutputStream(mysocket.getOutputStream()); 
            for(int i=0;i<mess.length;i++) {
              out.writeUTF(mess[i]);
              String  s=in.readUTF();      //in读取信息，堵塞状态
              System.out.println("对方说："+s);
              Thread.sleep(500);
            } 
       }
       catch(Exception e) {
            System.out.println("服务器已断开"+e);
       }
   } 
}

class Server {
   public static void main(String args[]) {
      String [] answer ={"您好！","李雷您好。","……我是韩梅梅。"};
      ServerSocket serverForClient=null;
      Socket socketOnServer=null;
      DataOutputStream out=null;
      DataInputStream  in=null;
      try { serverForClient = new ServerSocket(2010);
      }
      catch(IOException e1) {
            System.out.println(e1);
      } 
      try{  System.out.println("电话无人使用中……");
           socketOnServer = serverForClient.accept(); //堵塞状态，除非有客户呼叫
System.out.println("电话铃响了，有人来电！");
           out=new DataOutputStream(socketOnServer.getOutputStream());
           in=new DataInputStream(socketOnServer.getInputStream());
           for(int i=0;i<answer.length;i++) {
              String s=in.readUTF();        // in读取信息，堵塞状态
              System.out.println("对方说："+s);
              out.writeUTF(answer[i]);
              Thread.sleep(500);
           }
      }
      catch(Exception e) {
          System.out.println("客户已断开"+e);
      }
   }
}
