0

In this code I use multiple times of retrieve input-output data from two nodes. ... when I use more than two times input output stream it generated this type of error while running this code I need different input and output and that I want to store but unfortunate if I used more than three-time input/output stream it show error


   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)


user207421
  • 305,947
  • 44
  • 307
  • 483
paul
  • 519
  • 6
  • 13
  • No, you can't use multiple input/output streams, once they have been obtained for a given socket. Could you please provide the exception/error details too in the question BTW? – Am_I_Helpful Mar 10 '16 at 16:47
  • Your question has nothing whatsoever to do with RMI. Don't tag indiscriminately. – user207421 Mar 10 '16 at 21:24

1 Answers1

0

InputStream and OutputStream are intended to work with a single source and destination. Once you obtain an InputStream that reads from file/socket/whatever source you can use it for multiple consecutive reads. But once you are done reading from that source you need to invoke close() method on your stream.

Not closing your stream is classic reason for a memory leak in Java. In fact for that reason you ALWAYS expected to surround the usage of your source with try catch and always invoke close() method in finally statement. So to insure that it is always invoked. Further more, since close() method itself can cause an Exception, within final statement you need to surround it with its own try-catch. Starting from java 7 there is a new feature called "try with resources" that deals with this particular issue.

Please read about it here

Community
  • 1
  • 1
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36