0

so I have a PrintWriter and a ObjectOutputStream trying to send an int, String and a Color[] from the client when the user clicks the list, where 'out' is the PrintWriter and outObject is the ObjectOutputStream.

private class MyMouseListener implements MouseListener{
  public void mouseExited(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}
  public void mouseClicked(MouseEvent e){
      if(e.getSource() == list){
          int index = list.locationToIndex(e.getPoint());   ///get the game that the player has selected          
          try{
              out.println(index);
                              out.println("hel");
                              Color[] colors = new Color[5];
                              outObject.writeObject(colors)
          }
}

Server, where 'in' is a scanner, 'objectIn' is an objectInputStream :

                while(true)
                    if(in.nextLine().equals("hel")){
                    System.out.println("fee");
                }
                else if(in.hasNextInt()){
                    System.out.println("dee");
                }
                Object o = objectIn.readObject();
                if(o instanceof Color[]){
                    System.out.println("ree");
                }

If i try to send all three, on the first click the server only registers the String, and then all clicks after the server only registers the int, never registering the Color[], if i send the int first, then the Color[] and then the int like :`

                              out.println(index);
                              Color[] colors = new Color[5];
                              outObject.writeObject(colors);
                              out.println("hel");

Only the int and Object are registered first, on the second click only the String and then subsequent clicks only the int. `

                Object o = objectIn.readObject();
                if(in.nextLine().equals("hel")){
                    System.out.println("fee");
                }
                //Object o = objectIn.readObject(); 
                else if(in.hasNextInt()){
                    System.out.println("dee");
                }
                else if(o instanceof Color[]){
                    System.out.println("ree");
                    o = null;
                }

If I use the above series of if statements nothing is registered

Using the first series of if statements, if i try to send the Color[] on it's own it's not registered, but if I send it with an int both are registered. If i try to send the String and the Color[], sending the String first like so :

              out.println("hel");
              Color[] a = new Color[5];
              outObject.writeObject(a);

both are registered once and then never again, also in the opposite way than was expected i.e the Color[] was registered first and then the String; if i send both but the Color[] first and then the String neither are registered.

1 Answer 1

1

I assume your out object (which is a PrintWriter) wraps the outObject object, which is the ObjectOutputStream. You should not use both a PrintWriter and a ObjectOutputStream for the same underlying connection. See this question for more details.

Sign up to request clarification or add additional context in comments.

1 Comment

I assume a scanner would interfere with with an objectinputstre as we'll ?,not that I would want one without the printwriter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.