-1

I'm trying to make a very basic example of a network connection where I use an ObjectOutputStream, ObjectInputStream and sockets. The client sends the string "Hello" to the server. As you see this string is sent 10 times with the very same ObjectOutputStream. Then the server sends the string "Goodbye" to the client. The console should display 10 times the string "hello" and 10 times the string "Goodbye". But they are shown only one time. I read in many threads about the method reset() of the ObjectOutputStream. But it does not seem to work for me. Any ideas what the problem is? I've already tried many different things without success.

Here is the client:


    try  
    {       
        Socket socket = new Socket("localhost", 5555);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

        int i = 10;
        while (i > 0) 
        {           
            //send String
            out.writeObject("Hello");               
            out.flush();
            out.reset();

            //receive String
            String result = (String) in.readObject();
            System.out.println(result);
            i--;
        }       
    } catch (IOException e) 
    {
        e.printStackTrace();
    } catch (ClassNotFoundException e) 
    {           
        e.printStackTrace();
    } finally
    {
        out.close();
        in.close();
        socket.close();
    }

Here is the server:


try
    {   
        ServerSocket server = new ServerSocket(5555);
        while(true)
        {
            try
            {
                Socket client = server.accept();                            
                ObjectInputStream in = new ObjectInputStream(client.getInputStream());
                ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());

                //receive String
                String input = (String) in.readObject();
                System.out.println(input);

                out.writeObject("Goodbye");
                out.flush();
                out.reset();

            } catch(IOException e1)
            {
                e1.printStackTrace();
            } catch (ClassNotFoundException e) 
            {               
                e.printStackTrace();
            }
        }
    } catch(IOException e2)
    {
        e2.printStackTrace();
    }
Charles
  • 50,943
  • 13
  • 104
  • 142
Sheldon
  • 959
  • 2
  • 9
  • 16
  • The problem is that your server is only reading one object per connection. You need an inner read loop inside the accept loop that terminates on `EOFException`. NB You don't need `reset()` here. – user207421 Apr 26 '22 at 03:50

1 Answers1

0

Your server is only reading and writing once to each accepted socket. So the client can't possibly read an object ten times.

Your server also isn't closing the stream when it's finished.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I haven't closed the socket on the server because it does not matter. I have a while(true) loop... so the server never ends its service. What I try to accomplish is that the client only once establish a connection to the server. And this connection is then maintained. So everytime the client connects to the server it uses the very same ObjectOutputStream-Object. Any idea how I have to change the server in order to accomplish that goal? Assuming there are 10 clients... so every client establishes a connection to the server which is then maintained as long as the client-app is launched. – Sheldon Jan 23 '14 at 13:19
  • It does matter. When the server catches `EOFException` it must close the accepted socket. Otherwise you have a resource leak. You need an inner reading loop inside the accepting loop. – user207421 Apr 26 '22 at 03:52