运行服务器与客户端出现死机,求大神解!
}
程序代码:
//服务器 package com.fs.chat02; import import import import import public class Server { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(9999); int num = 1; while (true) { Socket client = server.accept(); int a = num++; System.out.println("一个客户端加入群聊"); System.out.println("群里有" + a + "个人"); new Thread(new sonServer(client)).start(); } } static class sonServer implements Runnable { private Socket client; private DataInputStream din; private DataOutputStream dout; public sonServer(Socket client) { this.client = client; try { din = new DataInputStream(client.getInputStream()); dout = new DataOutputStream(client.getOutputStream()); } catch (IOException e) { System.out.println("构造器处发生异常"); release(); } } private String receive() {// 接收数据 String data = ""; try { data = din.readUTF(); } catch (IOException e) { System.out.println("服务器接收数据出现异常"); release(); } return data; } private void send(String msg) {// 发送数据 try { dout.writeUTF(msg); dout.flush(); } catch (IOException e) { System.out.println("服务器发送数据出现异常"); release(); } } private void release() {// 释放资源 Close.close(dout, din); } public void run() { String data=receive(); if(data.equals("汪洋")){ send("登录成功"); }else{ send("登录失败"); while (true) { String msg=receive(); send(msg); } } } } } //客户端 import import import import import import import public class Client { public static void main(String[] args) throws UnknownHostException, IOException { BufferedReader read = new BufferedReader(new InputStreamReader( System.in)); Socket client = new Socket("localhost", 9999); System.out.println("请输入用户名:"); read.readLine(); while (true) { new Thread(new Send(client)).start(); new Thread(new Receive(client)).start(); } } static class Send implements Runnable { private Socket client; private DataOutputStream dout; private BufferedReader read; public Send(Socket client) { this.client = client; try { dout = new DataOutputStream(client.getOutputStream()); read = new BufferedReader(new InputStreamReader(System.in)); } catch (IOException e) { System.out.println("构造器出现异常"); } } private void send() {// 发送消息 try { String msg = read.readLine(); dout.writeUTF(msg); dout.flush(); } catch (IOException e) { System.out.println("服务端发送消息出现异常"); } } private void release() {// 释放资源 Close.close(dout, client); } public void run() { while (true) { send(); } } } static class Receive implements Runnable { private Socket client; private DataInputStream din; public Receive(Socket client) { this.client = client; try { din = new DataInputStream(client.getInputStream()); } catch (IOException e) { System.out.println("服务端发送消息出现异常"); } } private String receive() {// 接收消息 String msg = ""; try { msg = din.readUTF(); } catch (IOException e) { System.out.println("客户端接收消息出现异常"); } return msg; } private void release() {// 释放资源 Close.close(din, client); } public void run() { while (true) { receive(); } } } } //释放资源 import import public class Close { public static void close(Closeable... targets) { for (Closeable off : targets) { if (off.equals("")) { try { off.close(); } catch (IOException e) { System.out.println("释放资源出现异常"); } } } }