【问题标题】:Socket ObjectInputStream and ObjectOutputStream not working套接字 ObjectInputStream 和 ObjectOutputStream 不起作用
【发布时间】:2014-10-18 23:21:56
【问题描述】:

我尝试使用 ObjectInputStream 和 ObjectOutputStream 代替 BufferedReader 和 BufferedWriter 的简单客户端服务器,但这段代码不想工作。 这是代码

[客户]

[代码]

public class MainClient {

public static final String SERVER_ADDRESS_STRING = "192.168.0.2";
public static final int PORT_NO = 8000;
private static ObjectInputStream ois;
private static ObjectOutputStream oos;

public static void main(String[] args) {

    Socket socket = null;
    try {
        socket = new Socket(SERVER_ADDRESS_STRING, PORT_NO);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    System.out.println("Client : 1");

    try {
        ois = new ObjectInputStream(socket.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Client : 2");

    try {
        oos = new ObjectOutputStream(socket.getOutputStream());
        oos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Client : 3");

    //Diffie-Hellman
    try {
        @SuppressWarnings("unused")
        BigInteger shared_key = DiffieHellmanExchangeClient(socket, ois, oos);
        System.out.println(shared_key);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }   

    try {
        socket.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

[\代码]

[服务器]

[代码]

public class MainServer {

public static final int PORT_NO = 8000;
private static final int BACKLOG_NO = 10;
private static ObjectInputStream ois;
private static ObjectOutputStream oos;

public static void main(String[] args) {

    ServerSocket sslserver = null;
    try {
        sslserver = new ServerSocket(PORT_NO, BACKLOG_NO);
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    System.out.println("Server : 1");
    Socket socket = null;
    try {
        socket = (Socket) sslserver.accept();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    System.out.println("Server : 2");

    try {
        ois = new ObjectInputStream(socket.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Server : 3");

    try {
        oos = new ObjectOutputStream(socket.getOutputStream());
        oos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Server : 4");

    //Diffie-Hellman
    try {
        @SuppressWarnings("unused")
        BigInteger shared_key = DiffieHellmanExchangeServer(socket, ois, oos);
        System.out.println(shared_key);
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }

    try {
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        sslserver.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

[\代码]

问题是客户端和服务器在ois = new ObjectInputString上被阻塞。

为什么?

【问题讨论】:

  • 这里只有1个线程,所以只有一个客户端或服务器被阻塞。您可以在代码中添加注释以突出显示您被阻止的确切位置吗?
  • 这是两个独立推出的程序。服务器在尝试打开对象输入流时被阻止
  • 但是你确定它通过了accept()?这是怎么发生的?

标签: java sockets stream objectinputstream objectoutputstream


【解决方案1】:

在 ObjectInputStream 之前创建 ObjectOutputStream,否则会出现死锁。

【讨论】:

    猜你喜欢
    • 2017-09-06
    • 1970-01-01
    • 2013-08-29
    • 2015-01-05
    • 2014-04-02
    • 1970-01-01
    • 2012-12-22
    • 2012-05-29
    • 2019-06-07
    相关资源
    最近更新 更多