问题:当某个客户端退出后,其他客户端再发消息的时候,转发就会报错;
package com.huaxin.server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; public class Myserver { public static ArrayList<ServerThread>list =new ArrayList<ServerThread>(); public void initServer() { try { //创建服务器对象,并指定端口号 ServerSocket server = new ServerSocket(9090); System.out.println("服务器已建立......"); //不断获得客户真个连接 while(true){ Socket socket =server.accept(); System.out.println("客户端连接进来了......"); //当有客户端连接进来以后,开启1个线程,用来处理该客户真个逻辑, ServerThread st = new ServerThread(socket); st.start(); //添加该客户端到容器中 list.add(st); } } catch (Exception e) { e.printStackTrace(); } } }
package com.huaxin.server; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class ServerThread extends Thread { public Socket socket; public InputStream ins; public OutputStream ous; public ServerThread(Socket socket) { this.socket = socket; } public void run() { try { // 获得输入输出流 ins = socket.getInputStream(); ous = socket.getOutputStream(); // 发送消息给客户端 String msg = "welcome to zhou's server !"; sendMsg(ous, msg); // 发送要求登录信息给客户端 String userinfo = "please input your name:"; sendMsg(ous, userinfo); // 获得客户端输入的用户名 String userName = readMsg(ins); // 发送要求密码信息给客户端 String pwd = "please input your password:"; sendMsg(ous, pwd); // 获得客户端输入的密码 String pass = readMsg(ins); // 登录验证 boolean falg = loginCheck(userName, pass); // 校验不通过时,循环校验 while (!falg) { msg = "Fail to connect server......"; sendMsg(ous, msg); msg = "please check your name and password and login again....."; sendMsg(ous, msg); msg = "please input your name:"; sendMsg(ous, msg); // 获得客户端输入的用户名 userName = readMsg(ins); // 发送要求密码信息给客户端 msg = "please input your password:"; sendMsg(ous, msg); // 获得客户端输入的密码 pass = readMsg(ins); falg = loginCheck(userName, pass); } // 校验成功后:开始聊天 msg = "successful connected..... you can chat with your friends now ......"; sendMsg(ous, msg); // 聊天处理逻辑 //读取客户端发来的消息 msg=readMsg(ins); //输入bye结束聊天 while(!"bye".equals(msg)){ //给容器中的每一个对象转发消息 for (int i = 0; i <Myserver.list.size(); i++) { ServerThread st =Myserver.list.get(i); //不该自己转发消息 if(st!=this){ sendMsg(st.ous, userName+" is say:"+msg); } } //等待读取下1次的消息 msg=readMsg(ins); } } catch (Exception e) { System.out.println("客户端不正常关闭......"); // e.printStackTrace(); } //有异常后统1将流关闭 try { ins.close(); ous.close(); socket.close(); //将当前已关闭的客户端从容器中移除 Myserver.list.remove(this); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 校验客户端输入的账号和密码的函数,由于没有数据库,暂时写死了 public boolean loginCheck(String name, String pwd) { if (name.equals("zhou") && pwd.equals("zhou") || name.equals("user") && pwd.equals("pwd") || name.equals("huaxinjiaoyu") && pwd.equals("huaxinjiaoyu")) { return true; } return false; } // 发送消息的函数 public void sendMsg(OutputStream os, String s) throws IOException { // 向客户端输出信息 byte[] bytes = s.getBytes(); os.write(bytes); os.write(13); os.write(10); os.flush(); } // 读取客户端输入数据的函数 public String readMsg(InputStream ins) throws Exception { // 读取客户真个信息 int value = ins.read(); // 读取整行 读取到回车(13)换行(10)时停止读 String str = ""; while (value != 10) { // 点击关闭客户端时会返回⑴值 if (value == ⑴) { throw new Exception(); } str = str + ((char) value); value = ins.read(); } str = str.trim(); return str; } }
package com.huaxin.server; public class Test { public static void main(String[] args) { Myserver ms = new Myserver(); ms.initServer(); } }
上一篇 <Oday安全 11.6利用加载模块之外的地址绕过SafeSEH>一节注记---jmp [ebp+N] (上)
下一篇 springMVC源码解析--HandlerMethodArgumentResolverComposite参数解析器集合(二)