Search code examples
javasocketsclientserverobjectinputstream

ObjectInputStream$BlockDataInputStream.peekByte when using program on internet


I created an application with TCP, it works nice when I used it on a local network with 127.0.0.1 but the server refused to works when a client try to connect to him from an another network.

I don't know what this error means and how to resolve it and I can't anderstand that an application could works only on LAN.

public class Reception {

InputStream inObjet = null;
BufferedReader inString = null;
ObjectInputStream recVec2i = null;

public Reception(Socket socket) {
    try {
        this.inObjet = socket.getInputStream();
        this.inString = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        this.recVec2i = new ObjectInputStream(inObjet);
    } catch (IOException ex) {
        Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public Vecteur2i recevoir() {
    Vecteur2i to = new Vecteur2i();
    try {

        to = (Vecteur2i) recVec2i.readObject();

    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
        to = new Vecteur2i(1000, 1000);
    }
    return to;
}

public String recevoirString() {
    String chaine = "";
    try {
        chaine = inString.readLine();
    } catch (IOException ex) {
        Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
    }
    return chaine;
}

public void fermerReception() {
    try {
        inString.close();
    } catch (IOException ex) {
        Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        inObjet.close();
    } catch (IOException ex) {
        Logger.getLogger(Emission.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        recVec2i.close();
    } catch (IOException ex) {
        Logger.getLogger(Reception.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

And here the exception :

avr. 22, 2015 9:33:33 PM Snake.Reception recevoir Grave: null java.io.EOFException at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2597) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1316) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370) at Snake.Reception.recevoir(Reception.java:41) at Snake.Partie.cycleDeJeu(Partie.java:55)

Regards,


Solution

  • You can't mix different types of stream via the same socket when one or more of them is buffered, and both BufferedInputStream and ObjectInputStream are buffered. The buffers will 'steal' data from each other. In your case you should do all the I/O via the object stream. It has String-based methods.