网络通讯---聊天室
Server
1 package com.wanwan.net; 2 3 import java.io.DataInputStream; 4 import java.io.DataOutputStream; 5 import java.io.IOException; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 public class Server { 12 13 private List<MyChannel> all = new ArrayList<MyChannel>(); 14 15 public static void main(String[] args) throws IOException{ 16 new Server().start(); 17 } 18 19 public void start() throws IOException{ 20 21 ServerSocket server = new ServerSocket(9999); 22 23 while(true){ 24 Socket client = server.accept(); 25 MyChannel channel = new MyChannel(client); 26 all.add(channel); 27 new Thread(channel).start(); 28 } 29 } 30 31 private class MyChannel implements Runnable{ 32 33 private DataInputStream dis; 34 private DataOutputStream dos; 35 private boolean isRunning = true; 36 private String name; 37 38 public MyChannel(Socket client) { 39 // TODO Auto-generated constructor stub 40 try { 41 dis = new DataInputStream(client.getInputStream()); 42 dos = new DataOutputStream(client.getOutputStream()); 43 this.name = dis.readUTF(); 44 this.send("欢迎进入聊天室"); 45 this.sendOthers(this.name + "进入了聊天室",true); 46 } catch (IOException e) { 47 // TODO Auto-generated catch block 48 CloseUtil.closeAll(dis,dos); 49 isRunning = false; 50 } 51 } 52 53 private String recevie(){ 54 String msg = ""; 55 try { 56 msg = dis.readUTF(); 57 } catch (IOException e) { 58 // TODO Auto-generated catch block 59 CloseUtil.closeAll(dis,dos); 60 isRunning = false; 61 all.remove(this); 62 } 63 return msg; 64 } 65 66 private void send(String msg){ 67 if(null==msg || msg.equals("")){ 68 return; 69 } 70 71 try { 72 dos.writeUTF(msg); 73 dos.flush(); 74 } catch (IOException e) { 75 // TODO Auto-generated catch block 76 CloseUtil.closeAll(dis,dos); 77 isRunning = false; 78 all.remove(this); 79 } 80 } 81 82 83 private void sendOthers(String msg,boolean sys){ 84 85 if(msg.startsWith("@") && msg.indexOf(":")>-1){ 86 87 String name = msg.substring(1, msg.indexOf(":")); 88 String content = msg.substring(msg.indexOf(":")+1); 89 90 for(MyChannel other:all){ 91 if(other.name.equals(name)){ 92 other.send(this.name + "对你悄悄的说:"+content); 93 } 94 } 95 96 }else{ 97 98 for(MyChannel other:all){ 99 if(other == this){ 100 continue; 101 } 102 if(sys){ 103 other.send("系统信息:"+msg); 104 }else{ 105 other.send(this.name+"对所有人说:"+msg); 106 } 107 108 } 109 } 110 } 111 112 113 @Override 114 public void run() { 115 // TODO Auto-generated method stub 116 while(isRunning){ 117 sendOthers(recevie(),false); 118 } 119 } 120 } 121 }
Client
1 package com.wanwan.net; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.Socket; 7 import java.net.UnknownHostException; 8 9 public class Client { 10 11 public static void main(String[] args) throws UnknownHostException, IOException{ 12 13 System.out.println("请输入名称:"); 14 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 15 String name = br.readLine(); 16 if(name.equals("")){ 17 return; 18 } 19 20 Socket client = new Socket("localhost", 9999); 21 22 new Thread(new Send(client,name)).start(); 23 new Thread(new Receive(client)).start(); 24 25 } 26 27 }
Send
1 package com.wanwan.net; 2 3 import java.io.BufferedReader; 4 import java.io.DataOutputStream; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.net.Socket; 8 9 public class Send implements Runnable{ 10 11 private BufferedReader console; 12 private DataOutputStream dos; 13 private boolean isRunning = true; 14 private String name; 15 16 public Send(){ 17 console = new BufferedReader(new InputStreamReader(System.in)); 18 } 19 20 public Send(Socket client,String name){ 21 this(); 22 try { 23 dos = new DataOutputStream(client.getOutputStream()); 24 this.name = name; 25 sendM(this.name); 26 } catch (IOException e) { 27 // TODO Auto-generated catch block 28 isRunning = false; 29 CloseUtil.closeAll(dos,console); 30 } 31 } 32 33 private String getMsgFromConsole(){ 34 try { 35 return console.readLine(); 36 } catch (IOException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 return ""; 41 } 42 43 public void sendM(String msg){ 44 if(null != msg && !msg.equals("")){ 45 try { 46 dos.writeUTF(msg); 47 dos.flush(); 48 } catch (IOException e) { 49 // TODO Auto-generated catch block 50 isRunning = false; 51 CloseUtil.closeAll(dos,console); 52 } 53 } 54 } 55 56 @Override 57 public void run() { 58 // TODO Auto-generated method stub 59 while(isRunning){ 60 sendM(getMsgFromConsole()); 61 } 62 } 63 64 }
Receive
1 package com.wanwan.net; 2 3 import java.io.DataInputStream; 4 import java.io.IOException; 5 import java.net.Socket; 6 7 public class Receive implements Runnable{ 8 9 private DataInputStream dis; 10 private boolean isRuning = true; 11 12 public Receive() { 13 // TODO Auto-generated constructor stub 14 } 15 16 public Receive(Socket client){ 17 try { 18 dis = new DataInputStream(client.getInputStream()); 19 } catch (IOException e) { 20 // TODO Auto-generated catch block 21 isRuning = false; 22 CloseUtil.closeAll(dis); 23 } 24 } 25 26 public String receiveM(){ 27 String msg = ""; 28 try { 29 msg = dis.readUTF(); 30 } catch (IOException e) { 31 // TODO Auto-generated catch block 32 isRuning = false; 33 CloseUtil.closeAll(dis); 34 } 35 return msg; 36 } 37 38 @Override 39 public void run() { 40 // TODO Auto-generated method stub 41 while(isRuning){ 42 System.out.println(receiveM()); 43 } 44 } 45 46 }
CloseUtil
1 package com.wanwan.net; 2 3 import java.io.Closeable; 4 5 public class CloseUtil { 6 7 public static void closeAll(Closeable... io){ 8 for(Closeable temp:io) 9 { 10 try{ 11 if(null != temp){ 12 temp.close(); 13 } 14 }catch (Exception e) { 15 // TODO: handle exception 16 } 17 } 18 } 19 20 }