JAVA ServerSocket 一个tip点
ServerSocket关了之后不会对已建立的Socket有任何影响static void testSocket() throws Exception { InetAddress ia = InetAddress.getByName("127.0.0.1"); ServerSocket ss = new ServerSocket(); ss.bind(new InetSocketAddress(ia, 0)); Socket s1 = new Socket(); s1.connect(new InetSocketAddress(ia, ss.getLocalPort())); OutputStream os = s1.getOutputStream(); os.write(10); Socket s2 = ss.accept(); ss.close(); InputStream is = s2.getInputStream(); System.out.println(is.read()); os.write(20); System.out.println(is.read());}
页:
[1]