IO建立的客户端和服务端

在这里插入图片描述
上图描述了客户端可服务端的网络连接以及数据交换。下面的代码是基于IO的方式去通信的。
Server代码:

public class ServerEcho {

    static int port=8080;
    static ServerSocket serverSocket=null;

    public ServerEcho() throws IOException {
        serverSocket=new ServerSocket(port);
        System.out.println("服务器已经启动");
    }

    private String echo(String msg){
        return "echo:"+msg;
    }

    private  PrintWriter getWriter(Socket socket) throws IOException {
        OutputStream outputStream = socket.getOutputStream();
        return new PrintWriter(outputStream,true);
    }

    private BufferedReader getReader(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();
        return new BufferedReader(new InputStreamReader(inputStream));
    }


    public void service(){
        while (true){
            Socket socket=null;
            try {
                socket = serverSocket.accept();
                System.out.println("新建连接:"+socket.getInetAddress()+":"+socket.getPort());
                //服务端读取数据和打印数据
                BufferedReader reader = getReader(socket);
                PrintWriter writer = getWriter(socket);
                String msg=null;
                while((msg=reader.readLine())!=null){
                    System.out.println(msg);
                    writer.println(echo(msg));
                    if("bye".equals(msg)){
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(socket!=null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    public static void main(String[] args) throws IOException {
        new ServerEcho().service();
    }
}

客户端d代码如下:

public class ClientEcho {

    static int port = 8080;
    static Socket socket = null;
    static String host = "127.0.0.1";

    public ClientEcho() throws IOException {
        socket = new Socket(host, port);
    }

    private PrintWriter getWriter(Socket socket) throws IOException {
        OutputStream outputStream = socket.getOutputStream();
        return new PrintWriter(outputStream, true);
    }

    private BufferedReader getReader(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();
        return new BufferedReader(new InputStreamReader(inputStream));
    }

    public void talk() {
        try {
            BufferedReader reader = getReader(socket);
            PrintWriter writer = getWriter(socket);
            //本地输入数据
            BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));
            String msg=null;
            while((msg=localReader.readLine())!=null){
                writer.println(msg);
                System.out.println(reader.readLine());
                if("bye".equals(msg)){
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    public static void main(String[] args) throws IOException {
        new ClientEcho().talk();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值