日期:2014-05-20 浏览次数:20802 次
package net.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class ServerTester implements Runnable { @Override public void run() { ServerSocket server = null; try { server = new ServerSocket(5000); BufferedReader in = null; BufferedWriter out = null; while (true) { Socket socket = server.accept(); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); while (true) { out.write("hello to client\r\n"); out.write("i am server\r\n"); out.write("bye\r\n"); out.flush(); if (in.readLine().trim().equals("exit")) { break; } } socket.close(); System.err.println("第一次请求结束...按照你这个代码同时只能接受一个请求..."); } } catch (IOException e) { e.printStackTrace(); } finally { if (server != null) { try { server.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
------解决方案--------------------
package net.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; public class SocketTester implements Runnable { @Override public void run() { Socket socket = null; BufferedReader in = null; BufferedWriter out = null; try { socket = new Socket("localhost", 5000); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line = null; while ((line = in.readLine()) != null) { if (line.trim().equals("bye")) { break; } else { System.out.println(line); } } out.write("exit\r\n"); out.flush(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }