Java:从 ObjectInputStream 读取

发布于 2024-09-25 03:38:37 字数 1770 浏览 3 评论 0原文

我才刚刚开始学习Java。我的任务是创建一个文件服务器,它使用线程从多个客户端接受某些命令,例如文件获取、文件放置和文件删除。我正在使用自定义类 DataObject 来序列化和发送命令以及可能附带的任何数据。客户端是交互式的,因为它涉及用户手动输入各种命令。这意味着 ObjectInputStream readObject() 函数由于 EOFException 而无法在 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
                    }
            }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2024-10-02 03:38:37

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

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

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?

请持续率性 2024-10-02 03:38:37

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

服务器代码

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文