无法在方法

时间:2017-11-02 09:34:19

标签: java sockets

我有一个使用套接字连接2设备的应用程序。在客户端,我尝试发送和接收数据。当我从客户端发送数据时,服务器可以接收数据并回复其他消息。但是当我打开InputStream来捕获来自服务器的接收时,它会显示notify(IOException:java.net.SocketException:Socket已关闭)。 请帮我。 @@ 客户代码:

Socket socket = null;
    String mes = message;
    try {
        socket = new Socket(dstAddress, dstPort);

        /*Example send data to server*/
        ops =  socket.getOutputStream();
        ps = new PrintStream(ops);
        ps.print(mes);
        ps.close();
        /*End of example*/
        /*Receive data from server*/
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                1024);
        byte[] buffer = new byte[1024];

        int bytesRead;
        InputStream inputStream = socket.getInputStream();
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, bytesRead);
            response += byteArrayOutputStream.toString("UTF-8");
        }
        /*End of Receive data from server*/
        Log.e("Receive From Server:", response);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        response = "UnknownHostException: " + e.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        response = "IOException: " + e.toString();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

此服务器代码

private class SocketServerThread extends Thread {

    int count = 0;
    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(socketServerPORT);

            while (true) {
                Socket socket = serverSocket.accept();
                count++;

                /*example get input String*/
                try {
                    isr = new InputStreamReader(socket.getInputStream());
                    br = new BufferedReader(isr);
                    request = br.readLine();
                    Log.e("Mess-H.a", request);
                }catch (Exception e){
                    Log.e("Can't receive data:", e.getMessage() );
                }
                message += "#" + count + " from "
                        + socket.getInetAddress() + ":"
                        + socket.getPort() + request + "\n";
                /*End of example*/
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        activity.msg.setText(message);
                    }
                });
                SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(
                        socket, count);
                socketServerReplyThread.run();

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

private class SocketServerReplyThread extends Thread {

    private Socket hostThreadSocket;
    int cnt;

    SocketServerReplyThread(Socket socket, int c) {
        hostThreadSocket = socket;
        cnt = c;
    }

    @Override
    public void run() {
        OutputStream outputStream;
        String msgReply = "Hello from Server, you are #" + cnt;
        try {
            outputStream = hostThreadSocket.getOutputStream();
            PrintStream printStream = new PrintStream(outputStream);
            printStream.print(msgReply);
            printStream.close();

            message += "replayed: " + msgReply + "\n";

            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    activity.msg.setText(message);
                }
            });

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            message += "Something wrong! " + e.toString() + "\n";
        }

        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                activity.msg.setText(message);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

您关闭了套接字,然后继续使用它。

ps.close();

下面。删除。