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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.