1

I'm trying to pass data from MySQL to a client using a socket. For a List, I prefer using an array, but if I send an array using this:

InputStream input = client.getInputStream();
        OutputStream output = client.getOutputStream();
        ObjectOutputStream obOutput = new ObjectOutputStream(output);
        Statement statement = dataConnection.createStatement();
        ResultSet modList = statement.executeQuery("SELECT modID, downloads FROM modRegister");
        String[][] mods = new String[3][3];
        int column = 0;
        while (modList.next()) {
            mods[column][1] = modList.getString("modID");
            mods[column][2] = modList.getString("downloads");
            ++column;
        }

        System.out.println(mods[1][1]);

        PrintWriter printClient = new PrintWriter(output);
        printClient.println(column);
        printClient.close();
        obOutput.writeObject(mods);
        obOutput.flush();

and receive it from the client using this:

Socket server = new Socket("localhost", 25566);
    InputStream input = server.getInputStream();
    OutputStream output = server.getOutputStream();
    ObjectInputStream obInput = new ObjectInputStream(input);
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    String s;
    int columns = 0;
    while((s = reader.readLine()) != null) {
        columns = Integer.parseInt(s);
        System.out.println(s);
    }
    String [][] mods;
    TimeUnit.SECONDS.sleep(10);
    mods = (String[][])obInput.readObject();

I get an EOFException, even with a time delay. The array IS valid. I tested printing out all the data in it. Here is the exception:

Caused by: java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2601)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1319)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at layout.MainStoreController.initialize(MainStoreController.java:36)
... 32 more
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
dav20011
  • 71
  • 3
  • 14

1 Answers1

2

I suspect that closing the PrintWriter closes the OutputStream it writes to.

PrintWriter printClient = new PrintWriter(output);
printClient.println(column);
printClient.close();

When the stream is closed, then the socket is closed, and the other end will observe End-Of-File as it reads.

dsh
  • 12,037
  • 3
  • 33
  • 51
  • Sure? I dont get any Error in The Server, so I think its sending it – dav20011 Jul 27 '15 at 20:15
  • You can use wireshark to capture the traffic and see what is actually sent. I am sure you'll see a FIN packet there that closes the connection. – dsh Jul 27 '15 at 20:17
  • Nahh, replaced the close with a flush to prevent that, still the same error, and poorly, I'm using Windows, which actually doesn't support localhost with Wireshark, don't want to compile everything just to try it on my Ubuntu server. – dav20011 Jul 27 '15 at 21:06