Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 我可以为两个节点之间不同类型的数据传输使用多次(输入/输出)流吗_Java_Sockets_Networking_Tcp - Fatal编程技术网

Java 我可以为两个节点之间不同类型的数据传输使用多次(输入/输出)流吗

Java 我可以为两个节点之间不同类型的数据传输使用多次(输入/输出)流吗,java,sockets,networking,tcp,Java,Sockets,Networking,Tcp,在这段代码中,我使用多次从两个节点检索输入输出数据。。。当我使用两次以上的输入输出流时,它会在运行此代码时生成这种类型的错误,我需要不同的输入和输出,并且我希望存储它们,但不幸的是,如果我使用三次以上的输入/输出流,它会显示错误 InputStream和OutputStream旨在使用单个源和目标。一旦获得从文件/套接字/任何源读取的InputStream,您就可以使用它进行多次连续读取。但是,一旦完成了从该源的读取,就需要在流上调用close()方法 不关闭流是Java内存泄漏的典型原因

在这段代码中,我使用多次从两个节点检索输入输出数据。。。当我使用两次以上的输入输出流时,它会在运行此代码时生成这种类型的错误,我需要不同的输入和输出,并且我希望存储它们,但不幸的是,如果我使用三次以上的输入/输出流,它会显示错误






InputStream
OutputStream
旨在使用单个源和目标。一旦获得从文件/套接字/任何源读取的
InputStream
,您就可以使用它进行多次连续读取。但是,一旦完成了从该源的读取,就需要在流上调用
close()
方法

不关闭流是Java内存泄漏的典型原因。事实上,由于这个原因,您总是希望在源代码的使用中使用try-catch,并始终在finally语句中调用
close()
方法。以确保它始终被调用。此外,由于
close()
方法本身可能导致
异常
,因此在final语句中,您需要用它自己的try-catch来包围它。从Java7开始,有一个名为“try with resources”的新特性处理这个特殊问题


请阅读相关内容

不,一旦为给定套接字获取了多个输入/输出流,就不能使用它们。请您在问题BTW中也提供例外/错误详细信息?您的问题与RMI无关。不要乱贴标签。
   public class Server {
private static Socket socket;
public void connect() throws IOException{
        int port = 25000;
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Server Started and listening to the port 25000");
 socket = serverSocket.accept();
}
        //Server is running always. This is done using this while(true) loop

            //Reading the message from the client
  public void first() throws IOException{      
      connect();
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String number = br.readLine();
            System.out.println("Message received from client is "+number);

            //Multiplying the number by 2 and forming the return message
            String returnMessage;
            try
            {
                int numberInIntFormat = Integer.parseInt(number);
                int returnValue = numberInIntFormat*2;
                returnMessage = String.valueOf(returnValue) + "\n";
            }
            catch(NumberFormatException e)
            {
                //Input was not a number. Sending proper message back to   

          client.
                returnMessage = "Please send a proper number\n";
            }
            second();
            String e=br.readLine();System.out.println(e);
      }

       public void second() throws IOException{
            //Sending the response back to the client.
      String returnMessage="Second";
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(returnMessage);
            System.out.println("Message sent to the client is "+returnMessage);
            bw.flush();
        }

    public static void main(String args[]) throws IOException{

    Server obj = new Server();

    obj.first();
   // obj.second();

           }public class Server {
  private static Socket socket;
     public void connect() throws IOException{
        int port = 25000;
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Server Started and listening to the port 25000");
  socket = serverSocket.accept();
   }
        //Server is running always. This is done using this while(true) loop

            //Reading the message from the client
   public void first() throws IOException{      
      connect();
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String number = br.readLine();
            System.out.println("Message received from client is "+number);

            //Multiplying the number by 2 and forming the return message
            String returnMessage;
            try
            {
                int numberInIntFormat = Integer.parseInt(number);
                int returnValue = numberInIntFormat*2;
                returnMessage = String.valueOf(returnValue) + "\n";
            }
            catch(NumberFormatException e)
            {
                //Input was not a number. Sending proper message back to client.
                returnMessage = "Please send a proper number\n";
            }
            second();
            String e=br.readLine();System.out.println(e);
      }

      public void second() throws IOException{
            //Sending the response back to the client.
      String returnMessage="Second";
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(returnMessage);
            System.out.println("Message sent to the client is "+returnMessage);
            bw.flush();
        }

       public static void main(String args[]) throws IOException{

       Server obj = new Server();

       obj.first();
   // obj.second();

    }
           public class client {

       private static Socket socket;

    //public void connect() throws UnknownHostException, IOException{



   //}
    public void first() throws IOException{
   String host = "localhost";
        int port = 25000;
        InetAddress address = InetAddress.getByName(host);
        socket = new Socket(address, port);

     OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);

        String number = "2";

        String sendMessage = number + "\n";
        bw.write(sendMessage);
        bw.flush();
        System.out.println("Message sent to the server : "+sendMessage);
     String sendMessage1="3333";
    bw.write(sendMessage1);
     bw.flush();
    //second();

        }


        public void second1() throws IOException{

     //Get the return message from the server
    InputStream is = socket.getInputStream();
        InputStreamReader isr;
       isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String message = br.readLine();
        System.out.println("Message received from the server : " +message);
    socket.close();
  }
  public static void main (String argd[]) throws IOException{

    client obj1 = new client();
    obj1.first();




    }
    -----------------------------------------

          error

          Message received from client is 2
            Message sent to the client is Second
      Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:209)
       at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)