我正在尝试使用从套接字检索的InputStream创建新的ObjectInputStream。这是我的代码:
这是我的MessageGetterSender类的构造函数。程序无法到达Checkpoint 4。
public MessageGetterSender(Socket socket) {
System.out.println("MessageGetterSender: Checkpoint 1");
this.socket = socket;
// Get input and output streams
try {
System.out.println("MessageGetterSender: Checkpoint 2");
InputStream is = socket.getInputStream();
System.out.println("MessageGetterSender: Checkpoint 3");
this.in = new ObjectInputStream(is);
System.out.println("MessageGetterSender: Checkpoint 4");
} catch (IOException ioe) {
System.out.println("Could not get ObjectInputStream on socket: " + socket.getLocalPort());
}
try {
this.out = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException ioe) {
System.out.println("Could not get ObjectOutputStream on socket: " + socket.getLocalPort());
}
System.out.println("MessageGetterSender: Checkpoint 5");
}
我正在从连接到服务器以获取套接字的类中实例化一个新的MessageGetterSender对象。这是相关的代码。它是InstantMessageClass的构造函数,InstantMessageClass实例化了MessageGetterSender对象:
public InstantMessageClient(String username) {
try {
socket = new Socket("localhost", 5555);
} catch (IOException ioe) {
System.out.println("Error: Could not connect to socket on port: " + serverPort);
}
messageGetterSender = new MessageGetterSender(socket);
...
由于代码不会执行到检查点4,但是会到达检查点3,因此我很确定ObjectInputStream的实例化是罪魁祸首。我不知道为什么。有任何想法吗?谢谢您的帮助。