很疑惑的一个问题,客户端、服务端同时初始化ObjectOutputStream、ObjectInputStream时就卡住在初始化那行了,下面就是测试的程序,很简单典型的通信;现在的状态是正常的,也就是客户端只用输出流ObjectOutputStream,服务端只用输入流ObjectInputStream,通信正常;但是如果把注释中说明出问题的地方打开,也就是去掉“//”使其生效的话,程序就卡在流初始化那行了,非常困惑,是我有什么概念上的错误??不能同时初始化?有懂的大大一定请指教指教
import java.net.*;
import java.io.*;//要发送的类样例:Factory
class Factory implements Serializable {
    public int fid = 111;
}
public class ShowObjOutput {
    public static void main(String[] arg) {
        try {
            ObjectOutputStream os;
            //-------打开出问题
            //ObjectInputStream is;
            Socket sock = new Socket("10.10.1.248", 6000); //panda 为主机名
            Factory fa = new Factory();
            os = new ObjectOutputStream(new BufferedOutputStream(sock.
                    getOutputStream()));
            ///------打开出问题
            ///is = new ObjectInputStream(new
            ///                           BufferedInputStream(sock.getInputStream()));
            
            os.writeObject(fa);
            os.flush();
            os.close();
            sock.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}//对象输入
import java.net.*;
import java.io.*;public class ShowObjInput {
    public static void main(String[] arg) {
        try {
            ObjectInputStream is;
            //------打开出问题
            //ObjectOutputStream os;
            ServerSocket servSock = new ServerSocket(6000);
            Socket sock;            sock = servSock.accept();
            is = new ObjectInputStream(new
                                       BufferedInputStream(sock.getInputStream()));
            ///------打开出问题
            //os = new ObjectOutputStream(new BufferedOutputStream(sock.
            //        getOutputStream()));
            
            Factory o = (Factory) is.readObject();
            System.out.println("!!!:"+o.fid);
        } catch (Exception ex) {System.out.println(ex);}
    }
}

解决方案 »

  1.   

    ShowObjInput 没有问题
    ShowObjOutput 顺序问题os = new ObjectOutputStream(new BufferedOutputStream(sock.
                        getOutputStream()));os.writeObject(fa);
    os.flush();
    //然后才可 就是获得服务器的返回的意思
    is = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));
    os.close();
    sock.close();
      

  2.   

    os 是发送的用途
    is 是获得返回的概念
    你写反了~
      

  3.   

    感谢大家的回答
    回mdxk(mdxk) 你说的顺序问题应该没要紧的 否则 线程处理就实现不起来了,我改了下程序是如下,偷懒没用线程,运行ok的,cs双方都正常打印出输出了,但是这里用的是基础的os、is,所以顺序应该不是问题所在。//对象输入
    import java.net.*;
    import java.io.*;public class ShowObjInput {
        public static void main(String[] arg) {
            try {
                InputStream is;
                OutputStream os;
                ServerSocket servSock = new ServerSocket(6000);
                Socket sock;            sock = servSock.accept();
                os = sock.getOutputStream();
                is = sock.getInputStream();
                os.write("54321".getBytes());
                os.flush();
                byte[] temp = new byte[5];
                is.read(temp);
                System.out.println(new String(temp));
            } catch (Exception ex) {System.out.println(ex);}
        }
    }import java.net.*;
    import java.io.*;public class ShowObjOutput {
        public static void main(String[] arg) {
            try {
                    OutputStream os;
                    InputStream is;
                Socket sock = new Socket("10.10.1.248", 6000); //panda 为主机名
                Factory fa = new Factory();
                os = sock.getOutputStream();
                is = sock.getInputStream();
                
                os.write("12345".getBytes());
                os.flush();
                byte[] temp = new byte[5];
                is.read(temp);
                System.out.println(new String(temp));
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
    }
      

  4.   

    是这样的,ObjectInputStream 对象在刚刚创建的时候,就试图从 InputStream 里读入 4 个字节,如果这时候对方还没有发送任何东西过来,那么,这个创建过程就 block 在那儿了。
      

  5.   

    试了maquan说的答案,尝试后ok了 谢谢