在类里创建一个Socket连接后,为什么不能在不同方法中使用其getOutputStream()和getInputStream()???
如我在类A中声明了一个Socket成员变量,并在构造函数中初始化,并连接成功,并在构造函数中使用getOutputStream()和getInputStream()完成一次通讯,然后在类的其它方法也再次调用getOutputStreamm()和getInputStream()则一直等待,没有返回.为什么?
代码片断如下:
public class A
{
private Socket socket = null;
private DataOutputStream dos = null;
private DataInputStream dis = null;
public A()
{
socket = new Socket("127.0.0.1", 2222);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataOutputStream(socket.getInputStream());
.... // 通讯
dis.close();
dos.close();
}
public void fun()
{
dos.writeInt(1);
.... // 通讯
dis.close();
dos.close();
}
}