简单的Echo服务器和客户端
package tcp; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class EchoServer { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ServerSocket serverSocekt = null; Socket socket = null; BufferedReader inBuffer = null; PrintWriter out = null; try { serverSocekt = new ServerSocket(12); System.out.println("The server is start:" + serverSocekt); socket = serverSocekt.accept(); System.out.println("Accpet the Client:" + socket); inBuffer = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); while(true) { String instr = inBuffer.readLine(); System.out.println("In Server reveived the info:" + instr); if (instr.equals("bye")) { break; } out.println(instr); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (socket != null) { socket.close(); } if (inBuffer != null) { inBuffer.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
package tcp; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.NetworkInterface; import java.net.Socket; import java.net.UnknownHostException; public class EchoClient { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String hostname = "localhost"; if (args.length > 0) { hostname = args[0]; } PrintWriter out = null; BufferedReader networkIn = null; Socket theSocket; try { System.out.println(hostname); theSocket = new Socket(hostname, 12); networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream())); BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(theSocket.getOutputStream())), true); System.out.println("connect to echo server."); while(true) { String theLine = userIn.readLine(); if (theLine.equals(".")) { break; } out.println(theLine); out.flush(); System.out.println(networkIn.readLine()); } } catch (UnknownHostException e) { // TODO Auto-generated catch block System.err.println(e); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (networkIn != null) { try { networkIn.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (out != null) { out.close(); } } } }
相关推荐
zhangwentaohh 2020-09-16
leodengzx 2020-06-28
服务器端攻城师 2020-06-26
xcznb 2020-06-26
sapliang 2020-06-12
xasdfg 2020-06-01
bruce 2020-05-17
cuiweisaidelike 2020-05-11
songjie 2020-02-14
仁鱼 2020-02-12
furongwei 2020-05-10
丁丁爸爸的技术 2020-04-26
xcznb 2020-04-16
服务器端攻城师 2020-02-17
hygbuaa 2020-02-13
SoarFly00 2020-02-11