首页 > 代码库 > Socket通信入门小实例

Socket通信入门小实例

客户端:

public class Client {

    private int port = 8000;  
    private String host = "localhost";  
    private Socket socket;  
  
    public Client() throws UnknownHostException, IOException {  
        socket = new Socket(host, port);  
    }  
  
    public void readFromUser() throws IOException{  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        String line;  
        while((line = br.readLine()) != null){  
            if(line.equals("bye")){  
                socket.close();  
                System.out.println("关闭socket");  
                break;  
            }  
            send(line);  
        }  
    }  
      
    public void send(String msg) throws IOException {  
        PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);  
        pw.println(msg);  
    }  
  
    public static void main(String args[]) throws UnknownHostException,  
            IOException {  
        new Client().readFromUser();  
    }  
}

 

服务端:

public class Server {

    private int port = 8000;  
    private ServerSocket serverSocket;  
    private ExecutorService executorService;  
    private final int POOL_SIZE = 4;  
  
    public Server() throws IOException {  
        serverSocket = new ServerSocket(port, 2);  
        // 创建线程池  
        executorService = Executors.newFixedThreadPool(Runtime.getRuntime()  
                .availableProcessors()  
                * POOL_SIZE);  
        System.out.println("服务器启动。。。。。" + "初始线程数:"  
                + Runtime.getRuntime().availableProcessors() * POOL_SIZE);  
    }  
  
    public void service() throws IOException {  
        int count = 0;  
        while (true) {  
            Socket socket = serverSocket.accept();  
            System.out.println(++count + "客户端已链接");  
            // new Thread(new Handler(socket)).start();  
            executorService.execute(new Handler(socket));  
        }  
    }  
  
    public static void main(String args[]) throws IOException {  
        new Server().service();  
    }  
  
    private class Handler implements Runnable {  
  
        private Socket socket;  
  
        public Handler(Socket socket) {  
            this.socket = socket;  
        }  
  
        public void run() {  
            try {  
                handle();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
  
        private void handle() throws IOException {  
            BufferedReader br = new BufferedReader(new InputStreamReader(socket  
                    .getInputStream()));  
            String line;  
            while ((line = br.readLine()) != null) {  
                if(line.equals("goodbye")){  
                    socket.close();  
                    serverSocket.close();  
                    break;  
                }  
                System.out.println("From " + socket.getPort() + ":" + line);  
            }  
        }  
    }  
}

服务器采用了JDK自带的线程池来处理并发请求。 

还可以使用NIO来实现,即非阻塞通信

 

Socket通信入门小实例