码迷,mamicode.com
首页 > 其他好文 > 详细

Socket通信入门小实例

时间:2017-02-07 17:55:01      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:throws   get   port   count   system.in   input   unknown   cut   put   

客户端:

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通信入门小实例

标签:throws   get   port   count   system.in   input   unknown   cut   put   

原文地址:http://www.cnblogs.com/liushao/p/6375256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!