Java readObject挂起应用程序
我正在尝试创建live messenger应用程序。但是,当试图毫无理由地从ObjectInputStream读取Object时,代码挂起。没有抛出异常Java readObject挂起应用程序,java,freeze,objectinputstream,Java,Freeze,Objectinputstream,我正在尝试创建live messenger应用程序。但是,当试图毫无理由地从ObjectInputStream读取Object时,代码挂起。没有抛出异常 try { System.out.println("Trying to connect to server"); socket = new Socket(InetAddress.getByName("localhost"),6789); System.out.println("Connected to server");
try {
System.out.println("Trying to connect to server");
socket = new Socket(InetAddress.getByName("localhost"),6789);
System.out.println("Connected to server");
inputStream = new ObjectInputStream(socket.getInputStream());
outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.flush();
System.out.println("Streams are set up.");
window.toggleTyping(true);
System.out.println("Typing is now enabled");
String inputMsg = null;
do {
try {
System.out.println("Reading object");
inputMsg = (String)inputStream.readObject();
System.out.println("Object read");
} catch(ClassNotFoundException ee) {
System.out.println("Clas not found exception");
ee.printStackTrace();
}
} while(!inputMsg.equalsIgnoreCase("/exit"));
closeConnection();
}catch(IOException ex) {
ex.printStackTrace();
}
打印的最后一条消息是“读取对象”
课堂聊天:
public class Chat implements Runnable {
private Socket socket;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;
public Chat(Socket s) {
System.out.println("Chat class constructor called");
this.socket = s;
try {
outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.flush();
inputStream = new ObjectInputStream(socket.getInputStream());
System.out.println("Chat streams are now set up");
}catch(IOException ex) {
ex.printStackTrace();
}
}
private void closeChat() {
try {
outputStream.close();
inputStream.close();
socket.close();
System.out.println("Chat is now closed");
}catch(IOException ex) {
ex.printStackTrace();
}
}
@Override
public void run() {
System.out.println("Chat class method run called");
try {
outputStream.writeObject("Connection is cool");
outputStream.flush();
System.out.println("Text sent");
String inputMsg = "";
do {
try {
inputMsg = (String)inputStream.readObject();
System.out.println("Message read:"+inputMsg);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
while(!inputMsg.equalsIgnoreCase("/exit"));
closeChat();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
每个聊天都是不同线程的原因是我计划有一天实现多个一对一聊天。这是因为读取方法是一种阻塞方法。这意味着它正在尝试读取,直到得到-1以显示读取数据已完成。确保写入此套接字的OutputStream正在发送此消息。因此,在写入数据后,在发送套接字时调用该方法,或者在写入数据后关闭输出流。关闭这一个将是不合理的,因为您可能希望稍后通过此流发送更多数据
一般来说,创建outputstream后不需要立即刷新它readObject()是一个阻塞调用。如果没有任何内容,那么它将等待对象被读取。更好地使用超时和检查服务器是否发送对象并刷新?服务器代码在哪里?添加套接字超时。socket.setSortimeout()。我正在测试您的代码,它似乎起作用了-收到第一条消息,然后客户端阻塞等待更多消息。您是否还期待其他东西?您是否尝试了一个简单的测试,其中客户端和服务器位于同一台机器上?一旦流被打开,服务器就会发送一些问候消息。我总是在发送数据后使用flush
public class Chat implements Runnable {
private Socket socket;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;
public Chat(Socket s) {
System.out.println("Chat class constructor called");
this.socket = s;
try {
outputStream = new ObjectOutputStream(socket.getOutputStream());
outputStream.flush();
inputStream = new ObjectInputStream(socket.getInputStream());
System.out.println("Chat streams are now set up");
}catch(IOException ex) {
ex.printStackTrace();
}
}
private void closeChat() {
try {
outputStream.close();
inputStream.close();
socket.close();
System.out.println("Chat is now closed");
}catch(IOException ex) {
ex.printStackTrace();
}
}
@Override
public void run() {
System.out.println("Chat class method run called");
try {
outputStream.writeObject("Connection is cool");
outputStream.flush();
System.out.println("Text sent");
String inputMsg = "";
do {
try {
inputMsg = (String)inputStream.readObject();
System.out.println("Message read:"+inputMsg);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
while(!inputMsg.equalsIgnoreCase("/exit"));
closeChat();
}
catch (IOException e) {
e.printStackTrace();
}
}
}