android和pc端socket链接重置抛出:Connection reset 2C

这个是android客户端的socket

    @Override
    public void run() {
        try {
            socket = new Socket();
            System.out.println("body-->" + body);       
            socket.connect(new InetSocketAddress(Constans.IP, Constans.PORT),
                    Constans.TIME_OUT);
            OutputStream outputStream = socket.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                    outputStream);
            BufferedWriter bwWriter = new BufferedWriter(outputStreamWriter);
            bwWriter.write(body);
            bwWriter.close();
            outputStreamWriter.close();
            outputStream.close();
            socket.close();
        } catch (SocketTimeoutException e) {
            Log.e("TAG", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("TAG", e.getMessage(), e);
        }
    }

这个是pc服务器端的socket

 @Override
    public void run() {
        startListener();
    }

    /**
     * 开始监听
     */
    private void startListener() {

        System.out.println("Service--->开始监听");
        ServerSocket serivce;
        try {
            serivce = new ServerSocket(Constans.PORT);
            Socket socket = null;
            while (Constans.SOCKET_IS_LIFE) {
                // 等待客户端连接
                socket = serivce.accept();
                System.out.println("有一个连接上了");
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader bfr = new BufferedReader(isr);
                String body = "";
                System.out.println("brf--->"+bfr.readLine());
                body = bfr.readLine();
                System.out.println("body-->" + body);
                // 处理请求
                checkResult(body);
                bfr.close();
                isr.close();
                is.close();
            }
            socket.close();
            serivce.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

pc服务器端会抛出
图片说明

1个回答

会在System.out.println("brf--->"+bfr.readLine());这一行抛出异常

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐