Andi Domi
Andi Domi

Reputation: 751

java.io.EOFException error on object serialization with HttpHandler

I am trying to serialize an object in a HttpHandler class.

I have 2 files, Server3.java:

package server3;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class Server3 {
public static void main(String[] args) throws Exception {

HttpServer server = HttpServer.create(new InetSocketAddress(3333), 0);
server.createContext("/", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}

static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
        String response = "Kjo eshte nje pergjigje nga serveri! n";

        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes());
        os.close();

        Personat obj = new Personat();
        ObjectOutputStream objOut = new     ObjectOutputStream(t.getResponseBody());
        objOut.writeObject(obj);
        objOut.close();
}
}
}
class Personat implements Serializable{
private static final long serialVersionUID = 1L;
int ID=3;
String Name="Andi";
}

and Client3.java:

package server3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//te gjithe personat qe jan ne database me nej objekt 
public class Client3 {
public static void main(String[] args) throws Exception {
    try {
        URL url = new URL("http://localhost:3333");
        HttpURLConnection s = (HttpURLConnection) url.openConnection();
        s.setDoOutput(true);
        s.setDoInput(true);
        s.setRequestMethod("POST");
        s.setUseCaches(false);

        InputStream in = s.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        int c;
        while ((c = br.read()) != -1) {
            System.out.print((char) c);
        }
        ObjectInputStream ios = new ObjectInputStream(s.getInputStream());
        Personat oin = (Personat) ios.readObject();
        String emri=oin.Name;
        System.out.println(emri);
        ios.close();
        s.disconnect();
    } catch (IOException ex) {
        System.err.println(ex);
        System.out.print(ex);
    }
}
  }

But when I run it eclipse shows me

java.io.EOFException Kjo eshte nje pergjigje nga serveri! njava.io.EOFException`

and I cant understand why.

Upvotes: 2

Views: 5346

Answers (1)

cyon
cyon

Reputation: 9538

The problem is that you are trying to fit both the string response and the object into response.length() bytes. What happens is that only response.length() bytes are sent and so if you try to read more you get the EOFException.

If you instead set the responseLength parameter to be 0 it will allow you to transmit an arbitrary amount of data

t.sendResponseHeaders(200, 0); 

You also shouldn't close the stream if you are going to write more data into it. Don't call os.close() until all the writing is complete.

Upvotes: 2

Related Questions