A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 请许我一世安宁 初级黑马   /  2015-10-26 21:18  /  716 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. 这是一个能实现两个人聊天的小程序
  3. */
  4. import java.net.*;
  5. import java.io.*;
  6. class Chat{
  7.         public static void main(String[] args)throws Exception{
  8.                
  9.                 ServerSocket ss = new ServerSocket(10002);
  10.                 new Thread(new ServerThread(ss)).start();
  11.                 try{
  12.                         Thread.sleep(5000);
  13.                         Socket s = new Socket("192.168.1.71",10001);
  14.                         Thread t = new Thread(new ClientThread(s));
  15.                         t.start();
  16.                 }
  17.                 catch (Exception e){
  18.                         throw new RuntimeException("线程出现异常");
  19.                 }
  20.         }
  21. }
  22. class ClientThread implements Runnable{
  23.         private Socket s;
  24.         public ClientThread(Socket s){
  25.                 this.s = s;
  26.         }
  27.         public void run(){
  28.                 BufferedReader bufr = null;
  29.                 BufferedWriter bufOut = null;
  30.                 try{
  31.                         bufr = new BufferedReader(new InputStreamReader(System.in));
  32.                         bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
  33.                         String line = null;
  34.                         while((line=bufr.readLine())!=null){
  35.                                 if (line.equals("exit")) {  
  36.                     System.out.print("发送完毕");  
  37.                     bufOut.write(line);  
  38.                     bufOut.flush();  
  39.                     s.shutdownOutput();  
  40.                     break;  
  41.                 }  
  42.                                 bufOut.write(line);
  43.                                 bufOut.newLine();
  44.                                 bufOut.flush();
  45.                         }
  46.                         bufOut.close();
  47.                         s.close();
  48.                 }
  49.                 catch (Exception e){
  50.                         throw new RuntimeException("发送消息失败");
  51.                 }
  52.         }
  53. }
  54. class ServerThread implements Runnable{
  55.         private ServerSocket ss;
  56.         BufferedWriter bufOut = null;
  57.         public ServerThread(ServerSocket ss){
  58.                 this.ss = ss;
  59.         }
  60.         public void run(){
  61.                 Socket s;
  62.                 try{
  63.                         s = ss.accept();
  64.                 }
  65.                 catch (Exception ee){
  66.                         throw new RuntimeException("服务端建立失败");
  67.                 }
  68.                 String ip = s.getInetAddress().getHostAddress();
  69.                
  70.                 try{
  71.                         System.out.println(ip+".....connected");
  72.                         BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
  73.                         //PrintWriter ps = new PrintWriter(s.getOutputStream(),true);
  74.                         String str = null;
  75.                         while((str=bufIn.readLine())!=null){
  76.                                 if("exit".equals(str)){
  77.                                         System.out.println(s.getInetAddress().getHostName()+"发送的消息"+str);
  78.                                         break;
  79.                                 }
  80.                                 System.out.println(s.getInetAddress().getHostAddress()+"发送的消息"+str);           
  81.                         }
  82.                         s.shutdownInput();
  83.                         bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
  84.                         bufOut.write("服务器已接收到消息");
  85.                         bufOut.flush();
  86.                         s.shutdownOutput();
  87.                         s.close();
  88.                 }
  89.                 catch(Exception e){
  90.                         throw new RuntimeException("接收消息失败");
  91.                 }
  92.                 finally{
  93.                         if(bufOut!=null){
  94.                                 try        {
  95.                                         bufOut.close();
  96.                                 }
  97.                                 catch (Exception e){
  98.                                         e.printStackTrace();
  99.                                 }
  100.                         }
  101.                         if(s!=null){
  102.                                 try{
  103.                                         s.close();
  104.                                 }
  105.                                 catch (Exception e){
  106.                                         e.printStackTrace();
  107.                                 }
  108.                         }
  109.                         if(ss!=null){
  110.                                 try{
  111.                                         ss.close();
  112.                                 }
  113.                                 catch (Exception e){
  114.                                         e.printStackTrace();
  115.                                 }
  116.                         }
  117.                 }
  118.         }
  119. }

  120.                
复制代码


1 个回复

倒序浏览
有点意思
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马