2016-03-27 23 views
0

CS sınıfı için bir Paint Uygulaması yazıyorum ve veriyi sunucuya gönderemiyorum. Birden fazla istemcinin birlikte aynı boyama üzerinde çalışabilmesi için sunucuya bağlanması gerekiyordu, ancak yeni nesneyi sunucuya gönderemiyorum. Sunucuya bağlandığını biliyorum çünkü bir bağlantı kurulduğunda her iki uç hakkında geri bildirim veriyor ancak ObjectOutputStream.writeObject sunucuya ulaşamıyor. Lütfen neyi kaçırdığımı bana bildirin! Herkese teşekkürler!Boya Uygulaması Yerel Sunucuya Gönderilemiyor

private ArrayList<PaintObject> shapes = new ArrayList<PaintObject>(); 
private Point startDrag, endDrag; 
private ColorShapeSelectorJPanel colorShapeChooserArea = new ColorShapeSelectorJPanel(); 
private PaintObject currPaintObj = null; 
private Socket socket; 
private ObjectOutputStream oos; 
private ObjectInputStream ois; 

private static final String ADDRESS = "localhost"; 

public PaintingField() { 
    this.setBackground(Color.WHITE); 
    this.setSize(2000, 1400); 
    this.openConnection(); 
    initializeListeners(); 

} 

// Establish connection with the server. 
private void openConnection() { 
    /* Our server is on our computer, but make sure to use the same port. */ 
    try { 
     // Connect to the Server 
     socket = new Socket(ADDRESS, Server.SERVER_PORT); 
     oos = new ObjectOutputStream(socket.getOutputStream()); 
     ois = new ObjectInputStream(socket.getInputStream()); 
     System.out.println("Connected to server at " + ADDRESS + ":" + Server.SERVER_PORT); 
    } catch (IOException e) { 
     // e.printStackTrace(); 
     this.cleanUpAndQuit("Couldn't connect to the server"); 
    } 

} 

// Remove connection with server 
private void cleanUpAndQuit(String string) { 
    JOptionPane.showMessageDialog(PaintingField.this, string); 
    try { 
     if (socket != null) 
      socket.close(); 
    } catch (IOException e) { 
     // Couldn't close the socket, we are in deep trouble. Abandon ship. 
     e.printStackTrace(); 

    } 
} 

// Get listeners running 
private void initializeListeners() { 
    this.addMouseListener(new MouseAdapter() { 
     // Begin dragging the shape 
     public void mousePressed(MouseEvent evnt) { 
      startDrag = new Point(evnt.getX(), evnt.getY()); 
      endDrag = startDrag; 
      repaint(); 
     } 

     // When mouse is released, get the shape 
     @Override 
     public void mouseReleased(MouseEvent evnt) { 

      // If rectangle... 
      if (colorShapeChooserArea.isRectangleSelected()) { 
       currPaintObj = new PaintObject(makeRectangle(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()), 
         colorShapeChooserArea.getColor(), false); 
      } 

      // If ellipse... 
      else if (colorShapeChooserArea.isEllipseSelected()) { 
       currPaintObj = new PaintObject(makeEllipse(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()), 
         colorShapeChooserArea.getColor(), false); 
      } 

      // if line 
      else if (colorShapeChooserArea.isLineSelected()) { 
       currPaintObj = new PaintObject(makeLine(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()), 
         colorShapeChooserArea.getColor(), false); 
      } 

      // if doge 
      else if (colorShapeChooserArea.isImageSelected()) { 
       currPaintObj = new PaintObject(makeRectangle(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()), 
         Color.WHITE, true); 
      } 

      // Send the object to the server 
      // TODO: FIXME: oos.writeObject NOT SENDING!!! 
      try { 
       /* Someone pressed enter? Send the message to the server! */ 
       oos.writeObject(currPaintObj); 
      } catch (IOException ex) { 
       PaintingField.this.cleanUpAndQuit("Couldn't send a message to the server"); 
      } 

      shapes.add(currPaintObj); 
      startDrag = null; 
      endDrag = null; 
      repaint(); 

     } 

    }); 

    this.addMouseMotionListener(new MouseMotionAdapter() { 
     public void mouseDragged(MouseEvent evnt) { 
      endDrag = new Point(evnt.getX(), evnt.getY()); 
      repaint(); 
     } 
    }); 

} 

sorun çizgisi işaretlenir "// TODO: FIXME:" Amaç bildirmek ve fare bırakıldığında sunucuya göndermek için olmuştur.

cevap

1

---- [Çözüldü] ----

Ben birkaç sorun vardı: Birincisi

: Ben aynı sınıfta özel ServerListener sınıf yaratmıştı hangi Yukarıdaki kod BUT’tan değil, kurucunun bir örneğini oluşturmamıştım. Salak. Biliyorum. İkinci

: sunucu (PaintObject) üzerinde gönderdiği bir amacı seri değildi. Bir nesne, ObjectOutputStream.writeObject kullanılarak gönderilmek için serileştirilebilir olmalıdır. kamu PaintObject sınıfı şimdi başlıyor:

public class PaintObject implements Serializable { 

olanlar

sorunları vardı. Sadece iyi bir internet vatandaşı olacağımı ve onu kırdığımda kendi sorumu yanıtlayacağımı düşündüm. Umarım bu, gelecekte biraz yardımcı olur.

İlgili konular