so I'm making a messenger app in Android Studio. I have basically set up a server on my pc (a program running in Java) and a client (the android code). These 2 maintain an object input stream and an object output stream. When the client's code is executed (it's an async task), it writes some things to its output stream that end up to the input stream of the server successfully every time. However, when the server attempts to write to its output stream, the messages are successfully written and flushed (I have checked that the code after that is running so there is no problem in the writing part) but when the client tries to read from the input stream it crashes. And what's weird about it is that it doesn't happen all the time, it happens randomly. No idea how to fix it, the only thing the client does is read a boolean.
The error is this:
2022-07-10 21:34:47.823 11035-11084/com.example.smartchatters W/System.err: java.io.StreamCorruptedException: unexpected EOF in middle of data block
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStream.java:2692)
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2855)
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2777)
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at java.io.DataInputStream.readFully(DataInputStream.java:198)
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at java.io.DataInputStream.readChar(DataInputStream.java:365)
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at java.io.ObjectInputStream$BlockDataInputStream.readChar(ObjectInputStream.java:2937)
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at java.io.ObjectInputStream.readChar(ObjectInputStream.java:995)
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at com.example.smartchatters.logic.Usernode$Publisher.sendFile(Usernode.java:255)
2022-07-10 21:34:47.824 11035-11084/com.example.smartchatters W/System.err: at com.example.smartchatters.logic.Usernode$Publisher.run(Usernode.java:217)
Server's code (thread, the publisherBrokerHelper() is executed first and then calls the sendFiles()):
public void publisherBrokerHelper() {
try {
MultimediaFile item;
String r = "";
while (!r.equals("send")) {
item = (MultimediaFile) in.readObject();
r = new String(item.FileChunk, StandardCharsets.UTF_8);
System.out.println("Command: "+r);
if (r.equals("send")) {
receiveFile(item.getName(),item.getExtension(),item);
}
}
} catch (ClassNotFoundException e) {
System.err.println("PublisherBrokerHelper ClassNotFound error in run.");
e.printStackTrace();
} catch (IOException e) {
System.err.println("Connection with client terminated unexpectedly");
}
}
public void receiveFile(String fileName,String fileExtension, MultimediaFile sendMessage) {
String topicName=null;
ProfileName userName=null;
boolean verification=false;
try {
topicName = (String)in.readObject();
userName = (ProfileName)in.readObject();
for (int i=0;i<localTopics.size();i++) {
if (localTopics.get(i).getName().equals(topicName)) {
currentTopic=localTopics.get(i);
break;
}
}
for (int i=0;i<currentTopic.getSubscribedUsers().size();i++) {
if (currentTopic.getSubscribedUsers().get(i).getUsername().equals(userName.getUsername())) {
verification=true;
break;
}
}
System.out.println("WOW");
System.out.println(verification);
if (verification)out.writeChar('Y');
// else out.writeChar('N');
// out.flush();
out.writeBoolean(verification);
out.flush();
System.out.println("WOW");
} catch (ClassNotFoundException e1) {
System.err.println("PublisherBrokerHelper ClassNotFound error in topic name fetch.");
e1.printStackTrace();
} catch (IOException e1) {
System.err.println("PublisherBrokerHelper IO error error in topic name fetch.");
e1.printStackTrace();
}
.
.
.
}
Client's code (Async thread):
public void sendFile() {
try {
MultimediaFile item;
File file=null;
if (fileExtension!=null){
file = new File(fileName+"."+fileExtension);
item = new MultimediaFile(fileName,user,dateCreated,fileExtension, (int) Math.ceil((float)file.length() / 8192), "send".length()+1,0);
}
else {
item = new MultimediaFile(fileName,user,dateCreated,fileExtension, 0, "send".length()+1,0);
}
item.FileChunk = "send".getBytes(StandardCharsets.UTF_8);
out.writeObject(item);
out.flush();
out.writeObject(topicName);
out.flush();
out.writeObject(user);
out.flush();
System.out.println("in"+in);
System.out.println("WOW");
// System.out.println(in.readChar());
// boolean verification;
// verification= in.readChar() == 'Y';
boolean verification=in.readBoolean();
if (!verification) {
//System.out.println("Seems like you are trying to push in a topic that you don't belong");
return;
}
if (fileExtension!=null) {
InputStream FileStream = new FileInputStream(file);
int count=1;
item = new MultimediaFile(fileName, user, dateCreated,fileExtension, (int) Math.ceil((float)file.length() / 8192), 8192,count);
while ((FileStream.read(item.FileChunk)) > 0) {
out.writeObject(item);
out.flush();
//System.out.println("Publisher sent "+item.getChunkId()+" chunk of file :"+ item.getName()+"."+item.getExtension());
item = new MultimediaFile(fileName, user, dateCreated,fileExtension, (int) Math.ceil((float)file.length() / 8192), 8192,count+1);
count++;
}
FileStream.close();
}
} catch (IOException e) {
System.err.println("UserNode IO error in sendFile.");
e.printStackTrace();
}
}