Java Socket 示例01
Server 端代码:
package ServerSide; 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.ServerSocket; import java.net.Socket; public class EchoServer { private int port = 8088; private ServerSocket serverSocket; public EchoServer() { try { serverSocket = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); } } public String echo(String msg) { return "echo: " + msg; } private PrintWriter getWriter(Socket socket) { OutputStream socketOut = null; try { socketOut = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } return new PrintWriter(socketOut, true); } private BufferedReader getReader(Socket socket) { InputStream socketIn = null; try { socketIn = socket.getInputStream(); } catch (IOException e) { e.printStackTrace(); } return new BufferedReader(new InputStreamReader(socketIn)); } public void service() { while(true) { Socket socket = null; try { socket = serverSocket.accept(); System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort()); BufferedReader br = getReader(socket); PrintWriter pw = getWriter(socket); String msg = null; while((msg = br.readLine()) != null) { System.out.println(msg); pw.println(echo(msg)); if(msg.equals("bye")) break; } } catch (IOException e) { e.printStackTrace(); } finally { try { if(socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) throws IOException { new EchoServer().service(); } }
Client 代码:
package ClientSide; 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.Socket; public class EchoClient { private static String host = "10.11.121.239"; private static int port = 8088; private Socket socket; public EchoClient(String hostname) { host = hostname; try { socket = new Socket(host, port); } catch (IOException e) { e.printStackTrace(); } } public EchoClient() { this(host); } private PrintWriter getWriter(Socket socket) { OutputStream socketOut = null; try { socketOut = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } return new PrintWriter(socketOut, true); } private BufferedReader getReader(Socket socket) { InputStream socketIn = null; try { socketIn = socket.getInputStream(); } catch (IOException e) { e.printStackTrace(); } return new BufferedReader(new InputStreamReader(socketIn)); } public void talk() { try { BufferedReader br = getReader(socket); PrintWriter pw = getWriter(socket); BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in)); String msg = null; while((msg = localReader.readLine()) != null) { pw.println(msg); System.out.println(br.readLine()); if(msg.equals("bye")) break; } } catch (IOException e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { new EchoClient().talk(); } }