【问题标题】:Socket ObjectOutpuStream ObjectInputStream套接字 ObjectOutpuStream ObjectInputStream
【发布时间】:2012-12-22 10:32:37
【问题描述】:

我正在编写简单的客户端 - 服务器应用程序。一切正常,但是当我将 InputStream 和 OutputStream 更改为 ObjectOutputStream 和 ObjectInputStream 时,我的应用程序不会发送消息。任何人都可以帮助我并告诉我问题吗?

这里是 Serwer.java:

class InWorke implements Runnable{

    BufferedReader odczyt=null;
    String slowo;
    ObjectInputStream ois=null;
    Message message;

    InWorke(ObjectInputStream ois) {
        this.ois=ois;
    }

    public void run() {
        while(true) {
            try {
                slowo = (String) ois.readObject();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(slowo);
            Thread.yield();
        }               }

}

class OutWorke implements Runnable{
    Socket socket=null;
    BufferedReader odczytWe=null;
    DataOutputStream zapis=null;
    String slowo=null;
    Message message; // it is the simple class to serialization
    ObjectOutputStream oos;

    OutWorke(Socket socket,ObjectOutputStream oos) {
        this.socket=socket;
        this.oos=oos;
    }

    public void run() {
        while(true) {
            odczytWe=new BufferedReader(new InputStreamReader(System.in));
            try {
                slowo=odczytWe.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                    oos.writeObject(slowo);
                    oos.flush();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            Thread.yield();

    }

}
}

public class Klient {
    public static void main(String[] args) {

    Socket socket=null;
    ExecutorService exec=Executors.newCachedThreadPool();

    ObjectOutputStream oos=null;
    ObjectInputStream ois=null;

    try {
        socket=new Socket("localhost", 8881);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        //we=socket.getInputStream();
        ois=new ObjectInputStream(socket.getInputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        //wy=socket.getOutputStream();
        oos=new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    exec.execute(new OutWorke(socket, oos));
    exec.execute(new InWorke(ois));

 }
} 

客户端.java:

class InWorker implements Runnable{

    Socket socket=null;
    //InputStream we=null;
    //OutputStream wy=null;
    String slowo=null;
    BufferedReader odczyt=null;
    ObjectOutputStream oos;
    ObjectInputStream ois;
    Message message=null;

    InWorker(Socket socket,ObjectInputStream ois) {
        this.socket=socket;
        this.ois=ois;
    }

    public void run() {
                while(true) {
                try {
                    slowo=(String) ois.readObject();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(slowo);
                Thread.yield();
            }

            }
}

class OutWorker implements Runnable{

    DataOutputStream zapis=null;
    BufferedReader odczyt=null;
    //OutputStream wy=null;
    String tekst=null;
    ObjectOutputStream oos=null;
    Message message;

    OutWorker(ObjectOutputStream oos) {
        this.oos=oos;
    }

    public void run() {
        while(true) {
        odczyt=new BufferedReader(new InputStreamReader(System.in));
        try {
            tekst=odczyt.readLine();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            oos.writeObject(tekst);
            oos.flush();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Thread.yield();
        }
    }

}

public class Serwer {

    public static void main(String[] args) {
        ServerSocket serversocket=null;
        Socket socket=null;
        //InputStream we=null;
        //OutputStream wy=null;
        ObjectOutputStream oos=null;
        ObjectInputStream ois=null;

        ExecutorService exec=Executors.newCachedThreadPool();

        try {
            serversocket=new ServerSocket(8881);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            socket=serversocket.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            ois=new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            oos=new ObjectOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        exec.execute(new InWorker(socket, ois));
        exec.execute(new OutWorker(oos));

    }

}

【问题讨论】:

  • 那么异常呢?错误?
  • 编译器没有显示任何错误或异常。程序运行但不工作。
  • new ObjectInputStream() blocks 的可能重复项

标签: java sockets serversocket objectoutputstream objectinputstream


【解决方案1】:

检查ObjectInputStream的构造函数:constructor。
它说:

这个构造函数会阻塞直到对应的ObjectOutputStream 已写入并刷新标题。

所以需要创建并刷新对应的ObjectOutputStream。现在,您在服务器和客户端的输出流之前创建 ObjectInputStreams。它们阻止程序,因为没有创建输出流。您应该首先创建输出流,在它们上调用flush,然后才创建输入流。

【讨论】:

  • AFAIK 不需要调用flush(),实现自己做
  • 它可以在没有flush() 的情况下工作,但是ObjectOutputStream 的构造函数的文档说:“......调用者可能希望立即刷新流以确保接收 ObjectInputStreams 的构造函数不会阻塞...... "
  • 我更改订单(在服务器和客户端):try { oos=new ObjectOutputStream(socket.getOutputStream()); oos.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { ois=new ObjectInputStream(socket.getInputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 但程序仍然无法运行。
  • @user1518451 它对我有用:gist.github.com/7802c07588beb198b7fd 只有客户端发送“你好,世界”。而不是从控制台读取。
【解决方案2】:

正如 Nikita 指出的那样,根本问题是。实现方案是让服务器和客户端以相反的顺序获取输入和输出流。如果一个首先获取输入流,则另一个获取输出流,等等。我切换客户端以首先获取输出流,一切正常。然后我把它放回去并更改服务器,一切都以这种方式工作......您可以选择更改哪个。

供参考: http://docs.oracle.com/javase/6/docs/api/index.html?java/io/ObjectOutputStream.html

【讨论】:

  • 为了安全起见,两者都应该先打开 ObjectOutputStream
猜你喜欢
  • 2018-05-23
  • 2014-04-02
  • 2017-09-06
  • 1970-01-01
  • 2020-05-24
  • 2014-04-17
  • 1970-01-01
  • 2014-10-18
  • 2013-05-13
相关资源
最近更新 更多