鍍金池/ 問(wèn)答/Linux  HTML/ 如何用代碼演示TCP的異常

如何用代碼演示TCP的異常

如何用代碼重現(xiàn)Connection reset by peer和Broken pipe錯(cuò)誤(異常)
不限語(yǔ)言。

--
題主不完全是神獸黨,我也在搜索和嘗試。
參考:
https://www.cnblogs.com/metoy...
https://blog.netherlabs.nl/ar...

回答
編輯回答
我不懂

重現(xiàn)了Connection reset by peer,代碼如下

//client
    public static void main(String[] args) {
        try {
            Socket s = new Socket();
            s.setSoLinger(true,0);//設(shè)置調(diào)用close就發(fā)送RST
            s.connect(new InetSocketAddress("127.0.0.1",3113));

            OutputStream os = s.getOutputStream();
            os.write("hello".getBytes());

            s.close();

            System.in.read();//防止程序退出
        }catch (Exception e){
            e.printStackTrace();
        }
    }
//server
        try {
            ServerSocket ss = new ServerSocket(3113);
            Socket s = ss.accept();
            InputStream is = s.getInputStream();
            byte[] buf =new byte[1024];
            //int len = is.read(buf);
            //System.out.println("recv:"+new String(buf,0,len));

            Thread.sleep(10000);

            s.getOutputStream().write("hello".getBytes());

            System.out.println("send over");
            System.in.read();
        }catch (Exception e){
            e.printStackTrace();
        }
2018年5月21日 07:43