JAVA基础篇—模拟服务器与客户端通信
2017-08-19 21:01 lc_java 阅读(207) 评论(0) 编辑 收藏 举报第一种:
客户端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(); } } }