我编写此代码,该代码应为应用程序的“评论部分”或“评论部分”。 我试图发送一个ArrayList(commentcontent只有2个字符串,用户和注释,并实现了Serializable)。似乎服务器将数据发送到客户端,但是在readObject()的那一刻什么也没发生,并且代码冻结,就像他还在等待什么一样。有人可以帮助解决这个问题吗?
这是我的通信服务器处理程序
public class CommentHandler extends Thread{
private Socket socket;
private ObjectInputStream in;
private ObjectOutputStream out;
public CommentHandler(Socket s) throws IOException {
this.socket=s;
this.out = new ObjectOutputStream(s.getOutputStream());
}
@Override
public void run() {
try {
this.in = new ObjectInputStream(socket.getInputStream());
while(true) {
try {
String todo = (String )in.readObject();
sendMessage();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage() {
try {
out.writeObject(new ArrayList<CommentContent>(DataBaseAccess.getInstance().getComments(13)));
out.flush();
System.out.println("SENT");
} catch (IOException e) {
System.out.println("ERROR");
e.printStackTrace();
}
//DataBaseAccess.getInstance().getComments(idGame);
}
}
这是我的通讯客户处理程序
public class CommentHandler {
private static Socket socket;
private static ObjectInputStream in;
private static ObjectOutputStream out;
public static void connect() throws Exception {
if(socket!=null) return;
socket= new Socket("localhost", 8000);
out= new ObjectOutputStream(socket.getOutputStream());
}
public static void read() throws Exception{
if(socket==null) return;
//sendRequest("todo");;
if(in == null)
in = new ObjectInputStream(socket.getInputStream());
//??????????????????????????????????????????????????????????????????????????
System.out.println("TRY TO RECEIVE");
ArrayList<CommentContent> comm = new ArrayList<CommentContent>((ArrayList<CommentContent>) in.readObject()) ;
System.out.println("RECEIVED");
}
public static void sendMessage(CommentContent message) throws Exception {
if(out == null) return;
out.writeObject(message);
out.flush();
}
public static void sendRequest(String todo) throws IOException {
if(out==null) return;
out.writeObject(todo);
out.flush();
}
}
请帮助。
PS。我将client.read()调用到我的窗格的控制器中,并使用Timer类来刷新页面。