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

急求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 2015-06-09 07:27
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝