0

I've run into quite the annoying issue while programming. I am new to java sockets and trying my very best to learn so I apologize in advanced. Anyways, with how my current program is set up, I have a cube that moves across the screen and when you hit the Q key, it sends over the x and y coordinates of the cube to the server. The first time you send the coordinates over, it works like a charm; however, the second time you hit Q, you get a lovely error message: "java.io.StreamCorruptedException: invalid stream header: 71007E00".

Here is my code:

SnakeServer Class

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();
        }
    }
}

Connection Class

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 Class

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();
        }
    }
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97

1 Answers1

0

Get rid of the BufferedReader (unused), the PrintWriter (unused), and the DataInputStream (unused). If you're using serialization, use it for everything, in both directions. Construct one ObjectOutputStream and one ObjectInputStream, in that order, and use them for the life of the socket. As object streams have headers, which are read and written on construction, constructing multiple streams per socket can't really work without extreme care: and it's unnecessary.

user207421
  • 305,947
  • 44
  • 307
  • 483