标签:net highlight tput 模拟 main .net client csharp out
第一种:
客户端class Client
package 服务器发送到客户端; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Client { public static void main(String[] args) { try { Socket socket=new Socket("127.0.0.1", 6666); System.out.println("??????????????"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer=new PrintWriter(socket.getOutputStream()); BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); String readline; readline=br.readLine(); while (!readline.equals("end")) { writer.println(readline); writer.flush(); System.out.println("????????"+readline); System.out.println("????????"+in.readLine()); readline=br.readLine(); } writer.close(); in.close(); socket.close(); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("????????"+e); } } }
服务器端class Server
package 服务器发送到客户端; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException{ Server server=new Server(); server.onServer(); } public void onServer(){ ServerSocket server=null; try { try { server=new ServerSocket(6666); System.out.println("????????????"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("????????"+e); } Socket socket=null; try { socket=server.accept(); System.out.println("????????????"); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("Error."+e); } String line; BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter writer=new PrintWriter(socket.getOutputStream()); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("??????"+in.readLine()); line=br.readLine(); while (!line.equals("end")) { writer.println(line); writer.flush(); System.out.println("?????????"+line); System.out.println("??????"+in.readLine()); line=br.readLine(); } writer.close(); in.close(); server.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
第二种:
客户端class TCPClient
package 服务器发送到客户端; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class TCPClient { public static void main(String[] args) throws UnknownHostException,IOException{ try { Socket socket=new Socket("127.0.0.1", 6666); System.out.println("??????????????"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer=new PrintWriter(socket.getOutputStream());//????? String readline; readline=br.readLine(); writer.println(readline); writer.flush(); System.out.println("????????"+readline); BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//????????? System.out.println("????????"+in.readLine()); writer.close(); in.close(); socket.close(); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("????????"+e); } } }
服务器端class TCPServer
package 服务器发送到客户端; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class TCPServer { public static void main(String[] args) throws IOException{ ServerSocket server=null; try { try { server=new ServerSocket(6666); System.out.println("????????????"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("????????"+e); } Socket socket=null; try { socket=server.accept(); System.out.println("????????????"); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("Error."+e); } String line; BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//????????? line=in.readLine(); System.out.println("??????"+line); ArrayList<Integer> list = new ArrayList<Integer>(); Scanner scanner=new Scanner(line); while (scanner.hasNext()) { list.add(scanner.nextInt()); } // ?????????????????????,?????????????????鷵??? Collections.sort(list); PrintWriter writer=new PrintWriter(socket.getOutputStream());//????? writer.println(list); writer.flush(); System.out.println("?????????"+list); writer.close(); in.close(); server.close(); socket.close(); scanner.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
标签:net highlight tput 模拟 main .net client csharp out
原文地址:http://www.cnblogs.com/lc-java/p/7397909.html