码迷,mamicode.com
首页 > 其他好文 > 详细

Chat雏形程序

时间:2017-10-02 12:43:25      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:action   run   launch   ted   chat   sys   eve   cte   rem   

Server端

 1 import java.net.*;
 2 import java.io.*;
 3 import java.util.*;
 4 
 5 public class ChatServer {
 6     //储存每个客户端的输入输出流
 7     List<Client> clients = new ArrayList<Client>();
 8 
 9     public static void main(String[] args) {
10         new ChatServer().start();
11         
12     }
13 
14     private void start() {
15         try {
16             ServerSocket ss = new ServerSocket(8888);
17             while(true) {
18                 Socket s = ss.accept();
19                 Client c = new Client(s);
20                 new Thread(c).start();
21                 clients.add(c);
22             }
23         } catch (BindException e) {
24             System.out.println("端口已经被占用,请关掉相关程序!");
25             System.exit(-1);
26         } catch (IOException e) {
27             e.printStackTrace();
28         }
29     }
30 //收发客户端消息的线程    
31     private class Client implements Runnable {
32         private DataInputStream input;
33         private DataOutputStream output;
34         private boolean start;
35         private Socket s;
36         
37         public Client(Socket s) {
38             this.s = s;
39             try {
40                 input = new DataInputStream(s.getInputStream());
41                 output = new DataOutputStream(s.getOutputStream());
42                 start = true;
43             } catch (IOException e) {
44                 e.printStackTrace();
45             }
46             
47         }
48 //接收到消息后,转发给每一个客户端
49         public void run() {
50             System.out.println("有一个客户端加入了,现在人数:" + clients.size());
51             while(start) {
52                 try {
53                     String message = input.readUTF();
54                     if(message.equals("exit")) {
55                         this.close();
56                         return;
57                     }
58                     
59                     for(Iterator<Client> it = clients.iterator(); it.hasNext();) {
60                         Client c = it.next();
61                         c.getOutput().writeUTF(message);
62                     }
63                     
64                 } catch (IOException e) {
65                     e.printStackTrace();
66                 }
67             }
68             System.out.println("有一个客户端退出了,现在人数:" + clients.size());
69         }
70 
71         public DataOutputStream getOutput() {
72             return output;
73         }
74         
75         public void close() {
76             //接收到客户端发来exit后,发送给这个客户端一个确认的消息,再关闭有关他的所有管道
77             try {
78                 output.writeUTF("exit ok");
79                 start = false;
80                 this.input.close();
81                 this.output.close();
82                 this.s.close();
83                 clients.remove(this);
84                 
85             } catch (IOException e) {
86                 e.printStackTrace();
87             }
88 
89         }
90 
91 
92     }
93     
94 }

Client端

  1 import java.awt.*;
  2 import java.awt.event.*;
  3 import java.net.*;
  4 import java.io.*;
  5 
  6 public class ChatClient extends Frame {
  7 
  8     TextField input = new TextField();
  9     TextArea content = new TextArea();
 10     
 11     Socket s;
 12     DataInputStream dataInput;
 13     DataOutputStream dataOutput;
 14     boolean receiveService = true;
 15 
 16     public static void main(String[] args) {
 17         new ChatClient().launchFrame();
 18     }
 19     
 20     public void launchFrame() {
 21         setLocation(400, 300);
 22         setSize(400,300);
 23         add(input, BorderLayout.SOUTH);
 24         add(content, BorderLayout.NORTH);
 25         pack();
 26         addWindowListener(new WindowAdapter() {
 27             public void windowClosing(WindowEvent e) {
 28                 close();
 29             }
 30         });
 31         input.addActionListener(new SendMessage());
 32         setVisible(true);
 33         connect();
 34         new Thread(new ReceiveMessage()).start();
 35     }
 36 //联网,创建输入输出流    
 37     public void connect() {
 38         try {
 39             s = new Socket("127.0.0.1", 8888);
 40             dataInput = new DataInputStream(s.getInputStream());
 41             dataOutput = new DataOutputStream(s.getOutputStream());
 42             
 43         } catch(ConnectException e) {
 44             System.out.println("连接服务器失败,可能是因为未启动服务器程序!");
 45             System.exit(-1);
 46         } catch (UnknownHostException e) {
 47             System.out.println("找不到服务器……");
 48             System.exit(-1);
 49         } catch (IOException e) {
 50             e.printStackTrace();
 51         }
 52     }
 53 //响应事件,发送消息    
 54     private class SendMessage implements ActionListener {
 55 
 56         public void actionPerformed(ActionEvent e) {
 57             String message = input.getText();
 58             try {
 59                 dataOutput.writeUTF(message);
 60                 input.setText("");
 61             } catch (IOException e1) {
 62                 e1.printStackTrace();
 63             }
 64         }
 65         
 66     }
 67 //创建线程,接收消息    
 68     private class ReceiveMessage implements Runnable {
 69 
 70         public void run() {
 71             while(receiveService) {
 72                 try {
 73                     String message = dataInput.readUTF() + "\n";
 74                     content.setText(content.getText() + message);
 75                     
 76                 } catch (IOException e) {
 77                     e.printStackTrace();
 78                 }
 79             }
 80         }
 81         
 82     }
 83 //关闭    
 84     public void close() {
 85         try {
 86             receiveService = false;
 87             dataOutput.writeUTF("exit");
 88             try {
 89                 //睡一秒钟,一遍接收线程处理完手头的工作后再关闭流
 90                 Thread.sleep(2000);
 91             } catch (InterruptedException e) {
 92                 e.printStackTrace();
 93             }
 94             dataInput.close();
 95             dataOutput.close();
 96             s.close();
 97             System.exit(0);
 98 
 99         } catch (IOException e1) {
100             e1.printStackTrace();
101         }
102     }
103 }

 

Chat雏形程序

标签:action   run   launch   ted   chat   sys   eve   cte   rem   

原文地址:http://www.cnblogs.com/caisu/p/7619925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!