_Everglow
2017-02-13 20:11
//服务器端 package chatroom; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; public class ChatServer { private static HashMap<String, Socket> users = new HashMap<String, Socket>(); public static void addClient(String addr, Socket socket) { users.put(addr, socket); } public static Socket getClient(String addr) { return users.get(addr); } public static HashMap<String, Socket> getUsers() { return users; } public static void main(String[] args) { new ChatServer().startServer(); } public void startServer() { try { ServerSocket s = new ServerSocket(9000); System.out.println("服务器已经开启"); while (true) { Socket socket = s.accept(); String addr = socket.getInetAddress().getHostAddress(); System.out.println("接收到" + addr + "的访问"); ChatServer.addClient(addr, socket); new Service(socket); } } catch (Exception e) { e.printStackTrace(); } } } class Service extends Thread { private Socket socket; private BufferedReader br; private String addr; public Service(Socket socket) { super(); this.socket = socket; try { BufferedReader br = new BufferedReader(new InputStreamReader( socket.getInputStream())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.addr = socket.getInetAddress().getHostAddress(); this.start(); } public boolean check() throws IOException { String str = br.readLine(); if (str == null || str.equals("")) { br.readLine(); } String ip = str.split("/")[0]; Socket clientSocket = ChatServer.getClient(ip); if (clientSocket == null) { return true; } else { return false; } } @Override public void run() { try { while (true) { String str = br.readLine(); if (str == null || "".equals(str))continue; if ("exit".equals(str)) { System.out.println("有程序退出"); } int index = str.indexOf("/"); if (index == -1) {// 群聊--->遍历所有的socket进行写操作 for (String ip : ChatServer.getUsers().keySet()) { Socket clientSocket = ChatServer.getUsers().get(ip); PrintStream toAll = new PrintStream( clientSocket.getOutputStream(), true); toAll.println(str); } } else if (this.check()) { PrintStream ps = new PrintStream(socket.getOutputStream()); ps.println("您要发送的客户端不存在"); } else {// 私聊 String ip = str.split("/")[0]; String content = str.split("/")[1]; String text = this.addr + content;// 最后要发送的信息,开头得是发送者 Socket clientSocket = ChatServer.getClient(ip); if (clientSocket == null) { PrintStream returns = new PrintStream( socket.getOutputStream()); returns.println("您要发送的客户端不存在"); } PrintStream clientToClient = new PrintStream( clientSocket.getOutputStream(), true); clientToClient.println(text); } } } catch (Exception e) { e.printStackTrace(); } } }
//客户端 package chatroom; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class ChatClient { public static void main(String[] args) { new ChatClient().starCliant(); } public void starCliant(){ try { Socket socket = new Socket("127.0.0.1",9000); new AcceptData(socket); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(socket.getOutputStream(),true); while(true){ String str = br.readLine(); if("exit".equals("str"))break; out.println(str); }socket.close(); } catch (Exception e) { e.printStackTrace(); } } } class AcceptData extends Thread { private Socket socket; private BufferedReader br; public AcceptData(Socket socket) { this.socket = socket; try { BufferedReader br = new BufferedReader(new InputStreamReader( socket.getInputStream())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.start(); } @Override public void run() { try { while (true) { String str = br.readLine(); if(str==null||"".equals(str))continue; if("exit".equals(str)){ System.out.println("有客户端退出"); } int index = str.indexOf("/"); if(index==-1){//说明是群聊 System.out.println("群聊的内容为"+str); }else{//私聊的内容 String[] ss = str.split("/"); System.out.println(ss[0]+"对你说"+ss[1]); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
一个用IO做的简易聊天室,为什么做了判断还是报空指针异常?
----------------------------------------------------------------------------------------
客户端同样方式修改。。。
Java Socket应用---通信是这样练成的
125013 学习 · 590 问题
相似问题