3

I ve coded a simple Server that Listen for client and when the client is connected it opens a Datainputstream that read all the data sent from client( my client wirte UTF data).

This is the ServerCode:

    @Override
public void run() {
    // TODO Auto-generated method stub  
    try {           
        ServerSocket ss = new ServerSocket(7000);
        while(true){
        System.out.println("Il Server sta cercando Connessioni");
        Socket s = ss.accept();
        System.out.println("Il Server ha accettato un Client");

        Thread t2 = new Thread(new Runnable(){
            public void run(){             
                   try {
                    while(true){
                    DataInputStream dis = new DataInputStream(s.getInputStream());
                    isAlreadyOpened = true;                     
                    System.out.println(dis.readUTF());
                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    isAlreadyOpened = false;
                }  
            }           
        });
        if(!isAlreadyOpened){
        t2.start();
        }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

It is very basic and My client is an Andorid App that send data when button is clicked:

 @Override
public void onClick(View v) {
    try {
        DataOutputStream out = new DataOutputStream(s.getOutputStream());
        out.writeUTF("Testiamo sto socket");
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Everything is perfect when the client is connected to the server. But when I click just one time the Client's button, the data sent is displayed in my server logs but immediatly after the exception is thrown.

Il Server sta cercando Connessioni
Il Server ha accettato un Client
Il Server sta cercando Connessioni
Testiamo sto socket
java.io.EOFException
   at java.io.DataInputStream.readUnsignedShort(Unknown Source)
   at java.io.DataInputStream.readUTF(Unknown Source)
   at java.io.DataInputStream.readUTF(Unknown Source)

reading from Oracle documentation this exception is thrown <>. But how can I avoid this?

Alessio Trecani
  • 713
  • 2
  • 10
  • 25
  • 1
    Have you tried to put an if statement before readuTF like `if(dis.readUTF() != null){do the stuff}` – Briesanji Feb 07 '16 at 11:06
  • Yes I have tried it. But it doesn't work.. it throws the exception But it doesn't print neither once the data. So I think it is worse than before.. – Alessio Trecani Feb 07 '16 at 11:11
  • `while(true)...` statements are not efficient and should be avoided. `EOFException` is End Of File Exception. So that means that you are doing something wrong (maybe reading stuff that doesn't exist). – xdevs23 Feb 07 '16 at 11:16
  • 1
    While(true) is set to allow server to listen for data forever. I don't think there is an alternative way to do so.. The problem is that when client sends the data the server can read it.. But since it is inside a while loop.. at the second time there is no data to read so it throws exception. And this is what I would like to avoid even if I need the server that listen everytime for data. – Alessio Trecani Feb 07 '16 at 11:20
  • 1
    @xdevs23 Your statement about `while (true)` statements not being efficient is 100% nonsense. Nothing could be more efficient. – user207421 Feb 07 '16 at 11:38
  • @Briesanji That method doesn't return null. See the Javadoc. – user207421 Feb 07 '16 at 11:38

2 Answers2

9

You are sending one string and then closing the connection. You are trying to read infinitely many strings. They aren't being sent. What you're getting instead is the expected EOFException.

There is no problem here to solve. If you need to send more strings over the same connection, don't close it after sending one.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 3
    Oh thanks for the answer.. i didn't get it. I thought the connection wasn't closed beacause Server side everything is good.. The problem was the client that after flushing the outputstream closed the socket. I simply deleted dos.close() and everything worked.. Thanks so much – Alessio Trecani Feb 07 '16 at 12:27
0

Although this was not Alesso's problem, here's another way that the EOFException can occur:

You open an output stream to a file and then later open an input stream to that same file without closing the output stream first.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15