服务器-客户端通信Java。函数readObject()的客户端冻结

服务器-客户端通信Java。函数readObject()的客户端冻结,java,server,client,data-transfer-objects,Java,Server,Client,Data Transfer Objects,我编写的代码应该是应用程序的“注释部分或审阅部分”。 我正在尝试发送一个ArrayList(commentcontent只有2个字符串,user和comment,并实现可序列化)。似乎服务器将数据发送给客户端,但在readObject()的时候什么也没有发生,代码冻结,好像他还在等待什么。有人能帮忙解决这个问题吗 这是我的通信服务器处理程序 public class CommentHandler extends Thread{ private Socket socket; p

我编写的代码应该是应用程序的“注释部分或审阅部分”。 我正在尝试发送一个ArrayList(commentcontent只有2个字符串,user和comment,并实现可序列化)。似乎服务器将数据发送给客户端,但在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();
    }
}
公共类CommentHandler扩展线程{
专用插座;
私有对象输入流;
私有对象输出流输出;
公共CommentHandler(套接字)引发IOException{
这个.socket=s;
this.out=newObjectOutputStream(s.getOutputStream());
}
@凌驾
公开募捐{
试一试{
this.in=newObjectInputStream(socket.getInputStream());
while(true){
试一试{
.readObject()中的字符串todo=(字符串);
sendMessage();
}catch(classnotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共无效发送消息(){
试一试{
writeObject(新的ArrayList(DataBaseAccess.getInstance().getComments(13));
out.flush();
系统输出打印项次(“发送”);
}捕获(IOE异常){
System.out.println(“错误”);
e、 printStackTrace();
}
//DataBaseAccess.getInstance().getComments(idGame);
}
}
这是我的客户通信处理程序

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();
    }
}
公共类CommentHandler{
专用静态插座;
私有静态ObjectInputStream-in;
私有静态ObjectOutputStream out;
公共静态void connect()引发异常{
if(socket!=null)返回;
套接字=新套接字(“localhost”,8000);
out=newObjectOutputStream(socket.getOutputStream());
}
公共静态void read()引发异常{
if(socket==null)返回;
//发送请求(“todo”);;
if(in==null)
in=newObjectInputStream(socket.getInputStream());
//??????????????????????????????????????????????????????????????????????????
System.out.println(“尝试接收”);
ArrayList comm=newArrayList((ArrayList)in.readObject());
系统输出打印项次(“收到”);
}
公共静态void sendMessage(CommentContent message)引发异常{
if(out==null)返回;
out.writeObject(消息);
out.flush();
}
公共静态void sendRequest(字符串todo)引发IOException{
if(out==null)返回;
out.writeObject(todo);
out.flush();
}
}
请帮忙

另外,我将client.read()调用到我窗格的控制器中,并有一个类计时器,每秒刷新一次页面