日期:2014-05-20 浏览次数:21194 次
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 这个程序仅仅是为了让你明白服务端或者客户端关闭对双方造成的影响(其实没啥影响..) * 发生你的异常的原因主要是你尝试向一个服务/客户端已经关闭的输入流输出内容 * 通过调整一些属性可以更改这个程序的运行流程 用Lock来保证程序的执行顺序(通过设置IS_CLOSE_SERVER来确定是先关闭服务端还是客户端 另外凡是先关闭那一方都是负责输出内容) * @author Lv9 */ public class StudySocket { static final int PORT = 14455; static final boolean IS_CLOSE_SERVER = true;// true为先关闭服务端 false为先关闭客户端 static final Lock LOCK = new ReentrantLock(); static java.util.concurrent.atomic.AtomicBoolean isAccpet = new AtomicBoolean(); public static void main(String[] args) { new Thread(new Server()).start(); new Thread(new Client()).start(); } static void send(OutputStream o, String message) { PrintWriter out = new PrintWriter(o); out.print("Hello World"); out.flush(); out.close(); System.err.println("OutputStream is closed"); } static String receive(InputStream i) throws IOException { StringBuilder result = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(i)); String line = null; while ((line = reader.readLine()) != null) { result.append(line).append("\r\n"); } reader.close(); System.err.println("InputStream is closed"); return result.toString(); } static class Client implements Runnable { private Socket client; public Client() { } @Override public void run() { try { try { while (!isAccpet.get()) { TimeUnit.SECONDS.sleep(2); } if (!IS_CLOSE_SERVER) { LOCK.lock(); } } catch (InterruptedException e) { throw new RuntimeException(e); } this.client = new Socket(InetAddress.getLocalHost(), PORT); if (!IS_CLOSE_SERVER) { send(client.getOutputStream(), "I'm client."); LOCK.unlock(); } else { LOCK.lock(); System.err.println(receive(client.getInputStream())); } } catch (IOException e) { throw new RuntimeException(e); } } } static class Server implements Runnable { private ServerSocket server; public Server() { try { this.server = new ServerSocket(PORT); } catch (IOException e) { throw new RuntimeException(e); } } @Override public void run() { Socket socket = null; try { if (IS_CLOSE_SERVER) { LOCK.lock(); } isAccpet.set(true); socket = server.accept(); if (IS_CLOSE_SERVER) { send(socket.getOutputStream(), "I'm server."); LOCK.unlock(); } else { LOCK.lock(); System.err.println(receive(socket.getInputStream())); } } catch (IOException e) { e.printStackTrace(); } } } }