Java:从ObjectInputStream读取

时间:2010-09-26 16:39:29

标签: java sockets objectinputstream

我刚开始学习Java。我的任务是创建一个文件服务器,它使用线程从多个客户端接受某些命令,如文件获取,文件放入和文件删除。我正在使用自定义类DataObject来序列化和发送命令以及可能伴随的任何数据。在涉及各种命令的手动用户输入的意义上,客户端将被制成交互式。这意味着由于EOFException,ObjectInputStream readObject()函数将无法在while(true)循环中工作。我可以做什么,以便服务器线程在readObject()暂停,直到它看到下一个对象然后恢复while(true)循环?

服务器上的代码(分别为每个线程运行):

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();
                }
            }
        }
    }

客户代码:

    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
                    }
            }

2 个答案:

答案 0 :(得分:3)

当流结束时,从readObject抛出EOFException。在您的情况下,当客户端关闭其连接时。因此,如果客户端将对象发送到服务器并立即退出,则服务器将读取一个对象,并在下次尝试读取对象时获取EOFExcpetion,在现已关闭的连接上。

也许添加一个QUIT命令,他们都同意终止连接?

答案 1 :(得分:0)

解决方案添加一个扫描仪来输入一些数据..在写入对象方面...这将使线程保持活动状态并可用它等待输入......

服务器代码

  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(); 
    }

}

 }

客户端代码

    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);
    }
}

}