horsecao2
horsecao2
2015-06-09 07:14
采纳率: 100%
浏览 2.2k
已采纳

急求socket编程阻塞问题

我想做个测试,用cs模式,从客户端传一个对象给服务器,服务器修改这个对象的属性,再传回客户端。但是出了个问题。客户端运行到ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());这一行时,停住了。
服务器端也是运行到ObjectInputStream ois= new ObjectInputStream(socket.getInputStream());这一行停住了。
请大侠解释一下,为什么。

 public class Client {

    public static void main(String[] args) {
         Good good = new Good();
         User user = new User();
         good.setName("good one");
         good.setPrice(18);

         user.setAge(21);
         user.setName("jack");
                try {
                    Socket socket =new Socket("192.168.52.129",30000);
                    System.out.println("socket successful");
                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    System.out.println("oos is ok");

                    oos.writeObject(good);
                    socket.shutdownOutput();
                    ois.readObject();
                    System.out.println(good.getName()+good.getPrice());


                    oos.close();
                    ois.close();
                    socket.close();
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


    }
}
 public class Server {

    public static void main(String[] args) throws Exception {

        try {
            ServerSocket serverSocket = new ServerSocket(30000);
            System.out.println("服务器已经启动,等待连接");
            Socket socket = serverSocket.accept();
            System.out.println("服务器收到请求");
            ObjectInputStream ois= new ObjectInputStream(socket.getInputStream());
            System.out.println("ois is ok");
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            System.out.println("oos is ok");
            Good good = (Good)ois.readObject();
            System.out.println(good.getName()+good.getPrice());
            good.setName("servergood");
            good.setPrice(25);
            socket.shutdownInput();
            oos.writeObject(good);
                    ois.close();
                    oos.close();
                    socket.close();

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • Tiger_Zhao
    Tiger_Zhao 2015-06-09 07:27
    已采纳
    点赞 评论
  • nswcfd
    nswcfd 2015-06-09 07:39

    两边都block在read上了吧,结果谁都没有发数据出去。

    点赞 评论
  • horsecao2
    horsecao2 2015-06-09 08:27

    自己修改代码后发现,创建了ois之后,不能马上创建oos。这样就会阻塞,oos什么时候用,什么时候再创建。

    点赞 评论

相关推荐