client:
import java.io.BufferedReader;
import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.Socket;public class Send implements Runnable {
private DataOutputStream dos = null; private boolean flag = true; private String name; public Send(Socket client,String name) { try { dos = new DataOutputStream(client.getOutputStream()); this.name=name; send(this.name); } catch (IOException e) { this.flag = false; try { dos.close(); } catch (IOException e1) { } } } public void send(String msg) { try { dos.writeUTF(msg); dos.flush(); /* String readLine = br.readLine(); dos.writeUTF(readLine);*/ } catch (Exception e) { this.flag = false; try { dos.close(); } catch (IOException e1) { } } } public String a() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } @Override public void run() { while (flag) { try { send(a()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
server:
import java.io.DataInputStream;
import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.List;public class MyServer {
List<MyChannel> list=new ArrayList<MyChannel>();public static void main(String[] args) { try { //定义服务端 //获取一个连接 new MyServer(). start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}public void start() throws IOException{ ServerSocket server=new ServerSocket(8888); while(true){ Socket accept = server.accept(); MyChannel myChannel = new MyChannel(accept) ; list.add(myChannel); new Thread(myChannel).start(); }}class MyChannel implements Runnable{ private DataInputStream dis=null; private DataOutputStream dos=null; private boolean flag=true; private String name=null; private boolean sys=true;public MyChannel(Socket server) { try { dis=new DataInputStream(server.getInputStream()); dos=new DataOutputStream(server.getOutputStream()); this.name=dis.readUTF(); this.send("欢迎进入聊天室"); sendOthers(this.name+"进入了聊天室",true); } catch (IOException e) { e.printStackTrace(); try { dis.close(); dos.close(); this.flag=false; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }}public String recive() throws IOException{ String readUTF = dis.readUTF(); return readUTF;}public void send(String msg) throws IOException{ dos.writeUTF(msg); dos.flush();}public void sendOthers(String msg,boolean sys) throws IOException{ if(msg.startsWith("@")&&msg.contains(":")){ String name1 = msg.substring(msg.indexOf("@")+1,msg.indexOf(":")); String content =msg.substring(msg.indexOf(":")+1); for(MyChannel m: list){ if(m.name.equals(name1)){ m.send(this.name+"对您悄悄地说"+content); return; } } } for(MyChannel m: list){ if(m==this){ continue; } if(sys){ m.send("系统消息:"+msg); } else{ m.send(this.name+"对所有人说:"+msg); } }} @Override public void run() { while(flag){ try { sendOthers(recive(),false); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); try { dos.close(); dis.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }}}发送消息的线程:
import java.io.BufferedReader;
import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.Socket;public class Send implements Runnable {
private DataOutputStream dos = null; private boolean flag = true; private String name; public Send(Socket client,String name) { try { dos = new DataOutputStream(client.getOutputStream()); this.name=name; send(this.name); } catch (IOException e) { this.flag = false; try { dos.close(); } catch (IOException e1) { } } } public void send(String msg) { try { dos.writeUTF(msg); dos.flush(); /* String readLine = br.readLine(); dos.writeUTF(readLine);*/ } catch (Exception e) { this.flag = false; try { dos.close(); } catch (IOException e1) { } } } public String a() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); return br.readLine(); } @Override public void run() { while (flag) { try { send(a()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
接收消息的线程:
import java.io.DataInputStream;
import java.io.IOException;import java.net.Socket;public class Recive implements Runnable {
private DataInputStream dis;private boolean flag=true;public Recive(Socket client) { try { dis=new DataInputStream(client.getInputStream()); } catch (IOException e) { try { dis.close(); this.flag=false; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }}public String recive() throws IOException{ String msg = dis.readUTF(); return msg;} @Override public void run() { // TODO Auto-generated method stub while(flag){ try { String recive = recive(); if(recive==null){ this.flag=false; return; } System.out.println(recive); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}