一、服务端
package com.server;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;/** * @Auther: chenzhou * @Date: 2019/2/25 * @Description: 网络编程服务端 * @version: 1.0 */public class Server{ public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(9999);//设置端口号 System.out.println("等待客户端连接"); Socket accept = serverSocket.accept(); //首先接收客户端发来的消息,然后进行数据处理后发送回客户端 Scanner scanner = new Scanner(accept.getInputStream());//得到客户端发送的数据 PrintStream stream = new PrintStream(accept.getOutputStream());//向客户端发送数据 Boolean flag = true; while (flag){ if(scanner.hasNext()){ String next = scanner.next().trim(); if("bye".equals(next)){ stream.println("ByeBye!!!"); flag = false; }else{ stream.println("[Echo]"+next); } } } //关闭流 stream.close(); scanner.close(); serverSocket.close(); }}
二、客户端
package com.client;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;import java.util.Scanner;/** * @Auther: chenzhou * @Date: 2019/2/25 * @Description: 网络编程客户端 * @version: 1.0 */public class Client { private static final BufferedReader BUFFERED_READER= new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception{ Socket socket = new Socket("localhost",9999); Scanner scanner = new Scanner(socket.getInputStream());//服务端发来的信息 PrintStream stream = new PrintStream(socket.getOutputStream());//向服务端发送信息 Boolean flag = true; while(flag){ String input = getString("请输入要发送的内容"); stream.println(input); if(scanner.hasNext()){ String next = scanner.next(); System.out.println(next); } if("bye".equalsIgnoreCase(input)){ flag = false; } } stream.close(); scanner.close(); socket.close(); } public static String getString(String Prompt) throws Exception{ System.out.println(Prompt); String s = BUFFERED_READER.readLine(); return s; }}
输出结果:
三、多线程实现服务端(多个客户端连接一个服务端)
package com.server;import java.io.IOException;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;/** * @Auther: chenzhou * @Date: 2019/2/25 * @Description: 多线程实现服务端,让每个线程去处理客户端的请求 * @version: 1.0 */public class ServerForMore implements Runnable{ Socket socket = null; Scanner scanner = null; PrintStream stream= null; Boolean flag = true; //构造器初始化参数 public ServerForMore(Socket client) throws Exception { this.socket = client; scanner = new Scanner(client.getInputStream());//得到客户端发送的数据 stream = new PrintStream(client.getOutputStream());//向客户端发送消息 } @Override public void run() { while (this.flag){ if(this.scanner.hasNext()){ String next = scanner.next().trim(); if("bye".equals(next)){ stream.println("ByeBye!!!"); flag = false; }else{ stream.println("[Echo]"+next); } } } //关闭流 stream.close(); scanner.close(); try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(9999); System.out.println("等待客户端连接。。。。"); Boolean flag = true; while(flag){ Socket client = serverSocket.accept(); new Thread(new ServerForMore(client)).start(); } serverSocket.close(); }}