1. 程式人生 > >Socket簡單通信

Socket簡單通信

eth 新的 線程 finally 實現接口 inpu cnblogs ack ()

服務端代碼:

public class MyServer {
    private ServerSocket serverSocket;

    public MyServer() {
        try {
            // 設置你服務器監聽的端口為10000,用戶能使用的端口為1025-65535
            serverSocket = new ServerSocket(10001);
        } catch (IOException e) {
            // 捕獲異常,不懂的話好好看java,
            e.printStackTrace();
        }
        
// 創建新的監聽主線程,這個線程創建ServerSocket監聽 new Thread(new Runnable() { public void run() { while (true) { Socket socket = null; try { socket = serverSocket.accept(); // 當監聽到了客戶端連接後,創建新線程傳輸數據,這樣可以實現多個客戶端同時訪問
new Thread(new SocketHandler(socket)).start(); } catch (Exception e) { e.printStackTrace(); } } } }).start(); } class SocketHandler implements Runnable { private
Socket socket; private BufferedReader reader; private PrintWriter writer; SocketHandler(Socket socket) { try { this.socket = socket; reader = new BufferedReader(new InputStreamReader( this.socket.getInputStream(), "GB2312")); writer = new PrintWriter(socket.getOutputStream(), true); writer.println("-------welcome---------"); writer.println("-------welcome---------"); writer.println("-------welcome---------"); } catch (IOException e) { e.printStackTrace(); } } // 這裏是覆蓋實現接口Runnable裏的run() public void run() { try { // 讀取數據,這裏只能讀取一行String String line = reader.readLine(); System.out.println(line); } catch (IOException e) { e.printStackTrace(); } finally { // 最後要關閉Socket try { if (socket != null) socket.close(); if (reader != null) reader.close(); if (writer != null) writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { new MyServer(); } }

客戶端代碼:

public class SocketClientDemo implements Runnable {
    private Socket socket;
    BufferedReader reader;
    private PrintWriter writer;

    public SocketClientDemo() {
        try {
            // 127.0.0.1表示本機IP,10000為服務器Socket設置的端口
            socket = new Socket("127.0.0.1", 10001);
            reader = new BufferedReader(new InputStreamReader(
                    socket.getInputStream(), "GB2312"));
            writer = new PrintWriter(socket.getOutputStream(), true);
            writer.println("working.............");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        try {
            // 這裏就可以讀取所有行String
            String line, buffer = "";
            while (!((line = reader.readLine()) == null))
                buffer += line;
            System.out.println(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("problem");
        } finally {
            // 最後關閉Socket
            try {
                if (socket != null)
                    socket.close();
                if (reader != null)
                    reader.close();
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
        new Thread(new SocketClientDemo()).start();

    }

}

Socket簡單通信