一、Client(客戶端代碼)java
package cn.qi.Client; import java.io.*; import java.net.Socket; public class Client { Socket clientSocket; Client() {} public void write() throws IOException { // 創建鏈接到服務器端的socket clientSocket = new Socket("127.0.0.1",5000); // 客戶端向服務器端發送消息,經過socket創建管道 OutputStream os = clientSocket.getOutputStream(); // 經過socket獲取IO管道,純IO操做 PrintWriter pw = new PrintWriter(os); pw.write("hello"); pw.flush(); // 流的關閉 pw.close(); // 關閉socket鏈接 clientSocket.close(); } public void read() throws IOException { // 創建鏈接到服務器端的socket clientSocket = new Socket("127.0.0.1",5000); // socket實現純IO InputStream is = clientSocket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String st = br.readLine(); br.close(); System.out.println(st); // 關閉socket鏈接 clientSocket.close(); } public void writeAndRead() throws IOException { // 創建鏈接到服務器端的socket clientSocket = new Socket("127.0.0.1",5000); // 客戶端向服務器端發送消息,經過socket創建管道 OutputStream os = clientSocket.getOutputStream(); // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os)); // bw.write("hello"); // bw.write("\n"); // bw.flush(); PrintWriter pw = new PrintWriter(os); pw.write("hello"); // 當服務器端用readline讀取消息時,客戶端發送消息時要附帶換行符,不然服務器端就沒法結束讀取 pw.write("\n"); pw.flush(); // 客戶端與服務器端雙向通訊時不能關閉IO流,不然默認認爲想要關閉socket InputStream is = clientSocket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String st = br.readLine(); System.out.println(st); clientSocket.close(); } }
二、Server(服務器端代碼)服務器
package cn.qi.Server; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class Server { ServerSocket serverSocket; Server() {} public void sent() throws IOException { // 創建severSocket,監聽對應端口 serverSocket = new ServerSocket(5000); while(true) { // server監聽端口,監聽成功,生成socket Socket socket= serverSocket.accept(); // 服務器端向客戶端發送消息,經過socket創建管道 OutputStream os = socket.getOutputStream(); // 經過socket獲取IO管道,純IO操做 PrintWriter pw = new PrintWriter(os); pw.write("hello"); pw.flush(); // 流的關閉 pw.close(); // 本次請求的socket的關閉 socket.close(); } } public void getInfo() throws IOException { // 創建severSocket,監聽對應端口 serverSocket = new ServerSocket(5000); while(true) { // server監聽端口,監聽成功,生成socket Socket socket= serverSocket.accept(); // socket實現純IO InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String st = br.readLine(); br.close(); System.out.println(st); // 本次請求的socket的關閉 socket.close(); } } public void readAndWrite() throws IOException { // 創建severSocket,監聽對應端口 serverSocket = new ServerSocket(5000); while(true) { // server監聽端口,監聽成功,生成socket Socket socket = serverSocket.accept(); // socket實現純IO InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String st = br.readLine(); System.out.println(st); // 服務器端向客戶端發送消息,經過socket創建管道 OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write("yes hello"); pw.flush(); pw.close(); // 本次請求的socket的關閉 socket.close(); } } }