Vaibhav
Vaibhav

Reputation: 703

datainputstream throwing eofexception when receiving messages

i am trying to exchange message between server and client using sockets in java. i am using the following code for my sever

...
    try{
            if(acceptConnection){
                s = serverSocket.accept();
                System.out.println("connected");
                acceptConnection = false;
                dis = new DataInputStream(s.getInputStream());
            }
            System.out.println(dis.readUTF());
...

the first message is properly received but when i send the second message i get the following error

java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at Server.run(Server.java:23)

can anyone tell me what the problem is and how to solve it? thnx in advance

Upvotes: 0

Views: 958

Answers (1)

Kalaiarasan Manimaran
Kalaiarasan Manimaran

Reputation: 1658

use dis.read() method of DataInputStream for not getting the exception.

Kindly change to BufferedInputStream from DataInputStream , as DataInputStream will throw an EOFException when the stream reaches the end before reading all the bytes

Upvotes: 1

Related Questions