java实现的多人聊天程序

http://www.oschina.net/code/snippet_198455_23933
用java实现的一个简单的多人聊天程序
标签: <无>

源码与演示:源码出处

代码片段(3)[全屏查看所有代码]

1. [文件] ChatClient.java ~ 2KB     下载(209)     

001 package org.dxer;
002 import java.io.BufferedReader;
003 import java.io.DataInputStream;
004 import java.io.DataOutputStream;
005 import java.io.IOException;
006 import java.io.InputStreamReader;
007 import java.net.Socket;
008  
009 public class ChatClient {
010     private DataOutputStream output;
011     private DataInputStream input;
012     /* 服务器端口号 */
013     public static final int PORT = 10001;
014     /* 客户端名称 */
015     private String clientName;
016  
017     public ChatClient(String clientName) {
018         this.clientName = clientName;
019     }
020  
021     public static void main(String[] args) {
022         if (args.length < 1) {
023             System.err.println("Usage:\n\t" + ChatClient.class.getName()
024                     " ");
025             System.exit(1);
026         }
027         // 连接服务器
028         new ChatClient(args[0]).connect("127.0.0.1");
029     }
030  
031     public void connect(String host) {
032         Socket socket = null;
033         try {
034             socket = new Socket(host, PORT);
035             System.out.println("Connected to server");
036             input = new DataInputStream(socket.getInputStream());
037             output = new DataOutputStream(socket.getOutputStream());
038             ReadThread readThread = new ReadThread();
039             WriteThread writeThread = new WriteThread();
040             readThread.start();
041             writeThread.start();
042  
043             readThread.join();
044             writeThread.join();
045         catch (Exception e) {
046             e.printStackTrace();
047         finally {
048             try {
049                 if (input != null) {
050                     input.close();
051                 }
052                 if (output != null) {
053                     output.close();
054                 }
055                 if (socket != null) {
056                     socket.close();
057                 }
058             catch (IOException e) {
059             }
060         }
061     }
062  
063     public class ReadThread extends Thread {
064  
065         public void run() {
066             String msg = null;
067             try {
068                 while (true) {
069                     msg = input.readUTF();
070                     if (msg != null) {
071                         if (!msg.equals("bye")) {
072                             System.out.println(msg);
073                         else {
074                             break;
075                         }
076                     }
077                 }
078             catch (Exception e) {
079                 e.printStackTrace();
080             }
081         }
082     }
083  
084     public class WriteThread extends Thread {
085  
086         public void run() {
087             try {
088                 BufferedReader stdIn = new BufferedReader(
089                         new InputStreamReader(System.in));
090                 String message = null;
091                 while (true) {
092                     System.out.print("> ");
093                     message = stdIn.readLine();
094                     output.writeUTF("[" + clientName + "]$ " + message);
095                     if (message.equals("bye")) {
096                         break;
097                     }
098                 }
099             catch (Exception e) {
100                 e.printStackTrace();
101             }
102         }
103     }
104  
105 }

2. [文件] ChatServer.java ~ 2KB     下载(184)     

001 package org.dxer;
002 import java.io.DataInputStream;
003 import java.io.DataOutputStream;
004 import java.io.IOException;
005 import java.net.ServerSocket;
006 import java.net.Socket;
007 import java.util.ArrayList;
008  
009 public class ChatServer {
010  
011     /* 客户端列表 */
012     ArrayList clientList = new ArrayList();
013     /* 端口号 */
014     public static final int PORT = 10001;
015  
016     public static void main(String[] args) {
017         // 在main函数中,启动服务器
018         new ChatServer().start();
019     }
020  
021     public void start() {
022         ServerSocket server = null;
023         try {
024             server = new ServerSocket(PORT);
025             System.out.println("Server is started...");
026             Socket socket = null;
027             while ((socket = server.accept()) != null) {
028                 clientList.add(socket);
029                 System.out.println(socket.getInetAddress().getHostAddress()
030                         " connected to the server");
031                 new WorkThread(socket).start();
032             }
033         catch (Exception e) {
034             System.out.println(e.toString());
035         finally {
036             if (server != null) {
037                 try {
038                     server.close();
039                 catch (IOException e) {
040                 }
041             }
042         }
043     }
044  
045     public class WorkThread extends Thread {
046         private Socket socket;
047  
048         public WorkThread(Socket client) {
049             this.socket = client;
050         }
051  
052         @Override
053         public void run() {
054             DataOutputStream output = null;
055             DataInputStream input = null;
056             try {
057                 String msg = null;
058                 String message = null;
059                 input = new DataInputStream(socket.getInputStream());
060                 while (true) {
061                     msg = input.readUTF();
062                     System.out.println(msg);
063

你可能感兴趣的:(java,聊天,实例)