2013-12-15 106 views
0

編程時遇到了相當煩人的問題。我是新來的Java套接字,並盡我所能學習,所以我道歉在先進的。無論如何,我的當前程序是如何設置的,我有一個在屏幕上移動的立方體,當你點擊Q鍵時,它將立方體的x和y座標發送到服務器。當你第一次發送座標時,它就像一個魅力;但是,第二次點擊Q時,您會收到一條可愛的錯誤消息:"java.io.StreamCorruptedException: invalid stream header: 71007E00"如何通過套接字發送/更新對象?

這裏是我的代碼:

SnakeServer類

public class SnakeServer { 
    public static JFrame frame = new JFrame(); 
    public static ArrayList<Integer> alist = new ArrayList<Integer>(); 

    public static void main(String[] args) { 
     runServer(); 
    } 
    public static void runServer() { 
     try { 
      Connection connection = new Connection(); 
      connection.start(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

連接類

public class Connection extends Thread { 

    private volatile BufferedReader br; 
    private volatile PrintWriter pw; 
    private volatile ObjectInputStream oos; 
    private Socket s; 
    private ServerSocket ss; 

    Connection() throws IOException 
    { 
     ss = new ServerSocket(12355); 
     s = ss.accept(); 
     //s.setKeepAlive(true); 
     br = new BufferedReader(new InputStreamReader(s.getInputStream())); 
     pw = new PrintWriter(s.getOutputStream(), true); 
    } 

    public void run() 
    { 
     try 
     { 
      while(true) { 
       System.out.print("Server has connected!\n"); 
       oos = new ObjectInputStream(s.getInputStream()); 
       Object obj = oos.readObject(); 
       alist = (ArrayList<Integer>) obj; 
       System.out.println(alist.get(1)); 
       System.out.println(alist.get(0)); 

      } 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex); 
     }  
    } 
} 

SnakeGame類

public class SnakeGame extends JPanel implements ActionListener { 
    public static Timer timer; 
    public static int x = 100; 
    public static int y = 100; 
    public static boolean left,right,up,down; 
    public static Socket skt; 
    public static DataInputStream in; 
    public static ObjectOutputStream out; 
    public static ArrayList<Integer> set = new ArrayList<Integer>(); 

    public SnakeGame() { 
     timer = new Timer(100, this); 
     timer.start(); 
     right = true; 
     setupConnection(); 
    } 
    public void setupConnection() { 
     try { 
      skt = new Socket("localhost", 12355); 
      out = new ObjectOutputStream(skt.getOutputStream()); 
      in = new DataInputStream(skt.getInputStream()); 
     } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    public void actionPerformed(ActionEvent e) { 
     repaint(); 
     move(); 
     //connect(); 
    } 
    public void move() { 
     if(right) { 
      x += 25; 
     } 
     if(left) { 
      x -= 25; 
     } 
     if(up) { 
      y -= 25; 
     } 
     if(down) { 
      y += 25; 
     } 
     if(x > 400) { 
      x = 0; 
     } 
     if(x < 0) { 
      x = 400; 
     } 
     if(y > 400) { 
      y = 0; 
     } 
     if(y < 0) { 
      y = 400; 
     } 
    } 
    public void paint(Graphics g) { 
     super.paint(g); 
     g.fillRect(x, y, 25, 25); 
    } 
    public static void connect() { 
     set.add(0, x); 
     set.add(1, y); 
     try { 
      //skt.setKeepAlive(true); 
      out.writeObject(set); 
      out.flush(); 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

回答

0

擺脫的BufferedReader(未使用)時, PrintWri ter(未使用)和DataInputStream(未使用)。如果您正在使用序列化,請在所有方面使用它。按照該順序構造一個ObjectOutputStream和一個ObjectInputStream,並將它們用於套接字的生命週期。由於對象流具有標題,這些標題是在構造中讀寫的,因此每個套接字構造多個流不能非常謹慎地工作:而且這是不必要的。