//以下是客户端 import java.io.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.util.*; import java.util.List; public class ChatClient extends Frame{ ArrayList list = new ArrayList(); InputStream is = null; OutputStream s = null; DataInputStream dis=null; DataOutputStream dos=null; private boolean bConnected =false; //是否 连上了 private static String IP =null;//"192.168.0.2"; //ip地址 Socket s = null; TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); // ----------------------------- public static void main(String[] args) { new ChatClient().getIP(); //得到ip 地址 new ChatClient().launchFrame(); //运行 窗体 } // ----------------------------- //得到 IP 地址 public void getIP(){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in) ); System.out.println("请输入服务器端的ip地址.."); try { IP = br.readLine(); //获得输入的ip地址 //System.out.println("你输入的ip地址是:>>"+IP); } catch (IOException e) { System.out.println("获取IP地址出错....."); e.printStackTrace(); } } // ----------------------------- //窗体 public void launchFrame(){ connect(); //连接服务器 setLocation(400,300); this.setSize(300,300); add(tfTxt,BorderLayout.SOUTH); add(taContent, BorderLayout.NORTH); pack(); //关闭 按钮 实现关闭 this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent arg0){ disconnec(); //关闭声明dos 和 s System.exit(0); // 结束程序 } }); // 当文本框 回车时触发事件 调用方法 //当鼠标光标 在文本框内 敲回车的时候 执行方法 tfTxt.addActionListener(new TFListener()); setVisible(true); //connect(); //连接服务器 new Thread(new RecvThread()).start(); } //----------------------------- // 服务器 连接方法 public void connect(){ try { //System.out.println(IP+"LLLLLL"); System.out.println("正在连接服务器请稍后........"); s = new Socket(IP,2222);//连接服务器 s = s.getOutputStream(); dos = new DataOutputStream(os); is = s.getInputStream(); dis=new DataInputStream(is); bConnected = true; //确认已经连上了 System.out.println("成功和服务器连接...."); taContent.setText("成功和服务器连接......"); } catch (UnknownHostException e) { System.out.println("连接服务器失败....."); System.exit(0); //e.printStackTrace(); } catch (IOException e) { System.out.println("连接服务器失败....."); System.exit(0); //e.printStackTrace(); } } // ----------------------------- //关闭dos,s方法 public void disconnec(){ try { dos.close(); s.close(); } catch (IOException e) { System.out.println("关闭dos,s ,出错........"); e.printStackTrace(); } } // ----------------------------- //处理 回车时发送消息 和显示内容 private class TFListener implements ActionListener{ public void actionPerformed(ActionEvent arg0) { // TODO 自动生成方法存根 String str = tfTxt.getText().trim(); //获得 为本框的值, 去掉 两边的空格 //taContent.setText(str); //向 显示内容框 显示值 tfTxt.setText("");//重新设置 文本框为空值 try { dos.writeUTF(str); //向服务器发送 输入的值 dos.flush(); } catch (IOException e) { System.out.println("向服务器发送内容失败..服务器已关....."); e.printStackTrace(); } } } // ----------------------------- private class RecvThread implements Runnable{ public void run() { // TODO 自动生成方法存根 try{ while(bConnected){ String str = dis.readUTF(); //读取从服务器端 发过来的值 String s = ""; list.add(str); if(list.size()>= 12){ list.remove(1); } for(int i=0;i>"+str); //向客户端发送 内容 //System.out.println("ClientList.size()"+ClientList.size()); for(int i =0; i
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22002461/viewspace-607843/
,如需转载,请注明出处,否则将追究法律责任。