1.客户端接受服务端的消息并打印:
客户端:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; public class Client { private static final String IP = "localhost";//服务器地址 private static final int PORT = 8000;//服务器端口号 public static void main(String[] args) { handler(); } private static void handler() { //创建一个流套接字,连接到指定主机上的指定端口号 Socket client = null; try { while(true){ client = new Socket(IP, PORT); //取得输入流 InputStream is = client.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); //读取服务器端数据 System.out.println(br.readLine()); br.close(); is.close(); } }catch (Exception e) { System.out.println("客户端异常:" + e.getMessage()); }finally{ if(client != null){ try { client.close(); } catch (IOException e) { System.out.println("客户端异常:" + e.getMessage()); } } } } }
服务端:
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class Server { private static final int PORT = 8000;//监听的端口号 public static void main(String[] args) { Server server = new Server(); server.init(); } private void init(){ try { ServerSocket serverSocket = new ServerSocket(PORT); while(true){ Socket client = serverSocket.accept(); //一个客户端连接就开户一个线程处理 new Thread(new HandlerThread(client)).start(); } } catch (IOException e) { System.out.println("服务器异常:" + e.getMessage()); } } private class HandlerThread implements Runnable{ private Socket client; public HandlerThread(Socket client) { this.client = client; } @Override public void run() { try { //取得输出流 OutputStream os = client.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); //输入要输出给客户端的内容 Scanner sc = new Scanner(System.in); bw.write(sc.nextLine()); bw.close(); } catch (IOException e) { System.out.println("服务器异常:" + e.getMessage()); }finally{ if(client != null){ try { client.close(); } catch (IOException e) { client = null; System.out.println("服务器异常:" + e.getMessage()); } } } } } }
2.服务端接受客户端的消息并打印:
客户端:
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner; public class Client { private static final String IP = "localhost";//服务器地址 private static final int PORT = 8000;//服务器端口号 public static void main(String[] args) { handler(); } private static void handler() { //创建一个流套接字,连接到指定主机上的指定端口号 Socket client = null; try { while(true){ client = new Socket(IP, PORT); //取得输出流 OutputStream os = client.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); //输入要发送到服务端的消息 Scanner sc = new Scanner(System.in); bw.write(sc.nextLine()); bw.close(); os.close(); } }catch (Exception e) { System.out.println("客户端异常:" + e.getMessage()); }finally{ if(client != null){ try { client.close(); } catch (IOException e) { System.out.println("客户端异常:" + e.getMessage()); } } } } }
服务端:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Server { private static final int PORT = 8000;//监听的端口号 public static void main(String[] args) { Server server = new Server(); server.init(); } private void init(){ try { ServerSocket serverSocket = new ServerSocket(PORT); while(true){ Socket client = serverSocket.accept(); //一个客户端连接就开户一个线程处理 new Thread(new HandlerThread(client)).start(); } } catch (IOException e) { System.out.println("服务器异常:" + e.getMessage()); } } private class HandlerThread implements Runnable{ private Socket client; public HandlerThread(Socket client) { this.client = client; } @Override public void run() { try { //取得输入流 InputStream is = client.getInputStream(); BufferedReader bw = new BufferedReader(new InputStreamReader(is)); //读取客户端发送来的数据 System.out.println(bw.readLine()); bw.close(); } catch (IOException e) { System.out.println("服务器异常:" + e.getMessage()); }finally{ if(client != null){ try { client.close(); } catch (IOException e) { client = null; System.out.println("服务器异常:" + e.getMessage()); } } } } } }