本帖最后由 xzmzg 于 2011-06-14 08:58:25 编辑

解决方案 »

  1.   

    建议 使用 NIO 试试我估计是你服务端等 客户端输入,而如果客户端没有输入的话 服务器就会发生阻塞
      

  2.   

    是在服务端write的时候阻塞还是客户端read的时候阻塞
    在服务端write之后加一句输出试试
      

  3.   

    首先检查是在服务器端read之后 阻塞,还是客户端在read时阻塞。你可以在read前后system.out数据进行判断。需要注意的是,read时不要尝试read多次。如果已经read了,再read,那么下个肯定会阻塞的。另外,你看看,在catch里面有没有错误发生。
      

  4.   

    如果序列化的JDK版本和和反序列化的JDK版本不统一则可能造成异常!
      

  5.   

    我给楼主试了下,
    其实原因在于,当执行 ObjectInputStream objin=new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); 时,如果另一端没有发送,代码会阻塞在这里。就楼主的具体情况,请看ClientPoint的两行,ObjectOutputStream objout=new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                ObjectInputStream objin=new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));当程序执行到下面一行时,Client已经阻塞了,既后面的发送动作都没有做。
    要解决很简单,把ObjectInputStream objin=new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); 移到 objout.flush();之后,即可,完整代码如下,import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.Socket;public class ClientPoint extends Frame{
        private Panel center,buttom;
        private Button btnSend,btnReset;
        private TextField txtName,txtScore;
        private Label lblName,lblScore;
        
        public ClientPoint(){
            super("客户端");
            center=new Panel();
            buttom=new Panel();
            btnSend=new Button("Send");
            btnReset=new Button("Reset");
            txtName=new TextField();
            txtScore=new TextField();
            lblName=new Label("姓名: ");
            lblScore=new Label("成绩: ");
            center.setLayout(new GridLayout(2,2));
            center.add(lblName);center.add(txtName);
            center.add(lblScore);center.add(txtScore);
            buttom.add(btnSend);
            buttom.add(btnReset);
            btnSend.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    processSend();
                }    
            });
            this.setLayout(new BorderLayout());
            this.add(center,BorderLayout.CENTER);
            this.add(buttom,BorderLayout.SOUTH);
            this.setSize(300,100);
            this.show();
        }
        
        private void processSend(){
            //获得用户输入的信息
            String name=txtName.getText();
            Integer score=new Integer(txtScore.getText());
            Student s=new Student(name,score);
            try {
                //发送对象到服务器,先写后读
                Socket socket=new Socket("127.0.0.1",8088);
                ObjectOutputStream objout=new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                objout.writeObject(s);
                objout.flush();
                ObjectInputStream objin=new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
                Student stu=(Student)objin.readObject();
                System.out.println (stu.getName()+" "+stu.getScore());            
                objout.close();
                socket.close();
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        
        public static void main(String[] args){
            new ClientPoint();
        }
    }