I have only started learning Java. My task is to create a file server which accepts certain commands like File Get, File Put and File Delete from multiple clients using Threading. I am using a custom class DataObject to serialize and send commands and any data that may accompany with it. The client is to be made interactive in the sense that it involves manual user input of the various commands. This means that ObjectInputStream readObject() function will not work in a while(true) loop because of an EOFException. What can I do so that the server thread pauses at readObject() until it sees the next object and then resumes the while(true) loop?
Code at server (runs for each thread separately):
public void run() {
ObjectInputStream is = null;
ObjectOutputStream os = null;
try{
is = new ObjectInputStream(clientSocket.getInputStream());
os = new ObjectOutputStream(clientSocket.getOutputStream());
while (true) {
input = (DataObject) is.readObject();
//System.out.println("Input has been read");
output = CommandProcessor.process(input);
if(output.data == null) {
os.writeObject(output);
if(output.message.compareToIgnoreCase("Rsp Bye")==0){
clientSocket.close();
}
}
}
}
Code at client:
public Talker() {
DataObject input = new DataObject(0), output = new DataObject(0);
try {
log = new PrintStream("/home/meher/log.txt");
InetAddress serverAddress = InetAddress.getByName("127.0.0.1");
Socket serverSocket = new Socket(serverAddress, port);
os = new ObjectOutputStream(serverSocket.getOutputStream());
is = new ObjectInputStream(serverSocket.getInputStream());
CommandExecuter.Hello(output);
write(output);
read(input);
while(not-end-of-user-input){ //Yet to code this part
//Execute commands
}
}