0

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
                    }
            }
3
  • 4
    I should say that it's a pretty hard task for a person who 'only started learning Java'. Commented Sep 26, 2010 at 16:42
  • It is an assignment due tomorrow in a Distributed Operating Systems course. Java is the language expected. Commented Sep 26, 2010 at 16:51
  • Please not that I did not bother putting in the catch part of the Exception handlers here but they're there in the code. Commented Sep 26, 2010 at 17:06

2 Answers 2

3

EOFException is thrown from readObject when the stream ends. In your case, when the client closes its connection. So if the client sends an object to the server and immediately quits, the server will read one object and get EOFExcpetion the next time it tries to read an object, on the now closed connection.

Perhaps add a QUIT-command, in which they both agree to terminate the connection?

Sign up to request clarification or add additional context in comments.

4 Comments

My client should not quit unless there is a specific Bye message. The client keeps sending requests as and when the user types a command like File Get <filename> and hits the enter key. I want the connection to persist the entire time.
Don't see what makes the client stick around though? If it skips the not yet coded part it would just exit and terminate the connection.
Thanks a lot. I didn't realize the EOF was coming because of a closed connection. I thought it came up because it was not able to read any more objects until the client wrote any again. A while (true) loop on the client-side worked. :)
Using a QUIT-command adds nothing useful. TCP already sends that via the FIN bit. An explicit QUIT just adds to the complication without adding any value. You only need to catch EOFException explicitly - which you would anyway in case the peer couldn't send the QUIT for some reason.
-1

solution add a scanner to input some data ..on the writing object side ...this will keep the thread alive and available it will wait for the input .....

Server code

  package com.cdac.Network;

  import java.io.IOException;
  import java.io.ObjectOutputStream;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.net.SocketException;
  import java.util.Scanner;

  import com.cdac.collection.Customer;

  public class Server {


  public static void main(String[] args) throws IOException,SocketException{
    ServerSocket ss=new ServerSocket(1888);
    Customer customer=new Customer(101);
    Socket s=ss.accept();
    System.out.println("connection establishd");

    Scanner scanner=new Scanner(System.in);
    ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
    {
    oos.writeObject(customer);
scanner.next(); 
    }

}

 }

client code

    package com.cdac.Network;

  import java.io.IOException;
  import java.io.ObjectInputStream;
   import java.net.Socket;
   import java.net.SocketException;
   import java.net.UnknownHostException;

   import com.cdac.collection.Customer;

     public class Client {

    public static void main(String[] args) throws UnknownHostException,
        IOException, SocketException, ClassNotFoundException {

    Customer customer = new Customer(101, "amit", 500);

    // String str1,str2;
    Socket s = new Socket("localhost", 1888);

    ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
    {
        Object obj = ois.readObject();

        Customer c = (Customer) obj;
        System.out.println(c);
    }
}

}

1 Comment

The Scanner adds no value here. The EOFException will still be thrown. Correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.