Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t方法中的用户getOutputStream和getInputStream_Java_Sockets - Fatal编程技术网

Java Can';t方法中的用户getOutputStream和getInputStream

Java Can';t方法中的用户getOutputStream和getInputStream,java,sockets,Java,Sockets,我有一个应用程序连接2个设备使用插座。在客户端,我尝试发送和接收数据。当我从客户端发送数据时,服务器可以接收数据并回复其他消息。但当我打开InputStream以捕获来自服务器的接收时,它显示notify(IOException:java.net.SocketException:Socket已关闭)。 请帮帮我@@ 客户端中的代码: Socket socket = null; String mes = message; try { socket = new Soc

我有一个应用程序连接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);
            }
        });
    }
}

您关闭了插座,然后继续使用它

ps.close();

在这里。卸下。

您关闭了插座,然后继续使用它

ps.close();
在这里。移除