JavaSocket通訊

凌浩雨發表於2017-10-26

1 . 伺服器端


public class Server {
    public static void main(String[] args) {
        
        try {
            // 1. 繫結埠,並監聽此埠
            ServerSocket serverSocket = new ServerSocket(6000);
            System.out.println("伺服器啟動成功...");
            
            // 2. 呼叫accept()方法開始監聽,等待客戶端的連結
            // 使accept()阻塞等待客戶端請求,有客戶請求到來則產生一個Socket物件,並繼續執行
            Socket socket = serverSocket.accept();
            
            // 3. 獲取輸入流,並調取客戶端資訊            
            // 由socket物件得到輸入流,並構造相應的BufferedReader物件
            String line;
            InputStream inputStream = socket.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
            
            // 4. 由socket物件得到輸出流,並構造PrintWriter物件
            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            
            // 5. 由系統的標準輸入裝置構造BufferedReader物件
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            // 6. 從標準輸入列印從客戶端度讀入的字串
            line = br.readLine();
            
            while(!line.endsWith("end")){
                writer.println(line);
                writer.flush();
                System.out.println("Server: " + line);
                System.out.println("Client: " + in.readLine());
                line = br.readLine();
            }
            
            // 7. 釋放
            writer.close();
            in.close();
            socket.close();
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("伺服器啟動失敗...");
        }
    }
}

  1. 客戶端

public class Client {
    public static void main(String[] args) {
        try {
            // 1. 建立客戶端Socket,指定伺服器地址和埠
            Socket socket = new Socket("127.0.0.1", 6000);
            System.out.println("客戶端啟動成功...");
            // 2. 獲取輸出流, 向伺服器傳送訊息
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            // 3. 獲取輸入流,並讀取伺服器端的響應資訊
            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("Client: " + readLine);
                System.out.println("Server: " + in.readLine());
                readLine = br.readLine();
            }
            
            // 4. 釋放資源
            writer.close();
            in.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.out.println("未知主機");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("客戶端啟動失敗");
        }
        
    }
}

  1. 執行時首先開啟伺服器端,再開啟客戶端。在客戶端控制檯中輸入訊息,接下來在伺服器端控制檯數輸入訊息,可以看到客戶端傳送的訊息。