Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:Socket inputstream未接收新对象_Java_Sockets - Fatal编程技术网

Java:Socket inputstream未接收新对象

Java:Socket inputstream未接收新对象,java,sockets,Java,Sockets,这里是Java套接字入门 我有一个服务器和一个客户端。客户端向服务器发送一个命令,例如“LIST_1”。然后,服务器将生成一个包含3个边缘对象的列表,并将它们发送到客户端。 如果命令为“List_2”,它将生成12个边对象的列表,“List_3”48等等 我的问题是它第一次工作。如果我发送“List_1”,我会得到一个返回的3个边缘对象的列表。但是,如果我随后发送命令“List_2”,我仍然只能得到3个边对象的列表,而不是12个 我试着用一些系统来调试它。输出。println在两边。似乎服务器确

这里是Java套接字入门

我有一个服务器和一个客户端。客户端向服务器发送一个命令,例如“LIST_1”。然后,服务器将生成一个包含3个边缘对象的列表,并将它们发送到客户端。 如果命令为“List_2”,它将生成12个边对象的列表,“List_3”48等等

我的问题是它第一次工作。如果我发送“List_1”,我会得到一个返回的3个边缘对象的列表。但是,如果我随后发送命令“List_2”,我仍然只能得到3个边对象的列表,而不是12个

我试着用一些系统来调试它。输出。println在两边。似乎服务器确实生成了包含12个边缘对象的正确列表,但客户端始终接收相同(旧)的3个对象列表

服务器端:

private List<Edge> edges = new ArrayList<Edge>();

@Override
public void run() {
    try {
        OutputStream outStream = socket.getOutputStream();
        InputStream inStream = socket.getInputStream();

        in = new ObjectInputStream(inStream);
        out = new ObjectOutputStream(outStream);

        boolean done = false;
        Object inObject = null;
        String welcome = "#Welcome message#";
        out.writeObject(welcome);
        System.out.println("Clientconnection has been made.");
        while (!done) {
            try {
                inObject = in.readObject();
                if (inObject instanceof String) {
                    String rawCommand = (String) inObject;   //[Command]_[Level] 
                    String[] splits = rawCommando.split("_");
                    String command = splits[0];
                    int level = Integer.parseInt(splits[1]);
                    kochfractal.setLevel(level);
                    System.out.println("Command received: " + command + ", level: " + level);
                    if (command.equals("EXIT")) {
                        done = true;
                    } else {
                        if (command.equals("LIST")) {
                        edges.clear();
                        kochfractal.generateEdges();
                        out.writeObject(edges);
                        out.flush();
                    }
                  }
                } else {
                    System.out.println("!Invalid command received!");
                }
            } catch (ClassNotFoundException e) {
                System.out.println("Object type not known");
            }
        }
        in.close();
        out.close();
        socket.close();

    } catch (IOException e) {

    }
}
private List edges=new ArrayList();
@凌驾
公开募捐{
试一试{
OutputStream outStream=socket.getOutputStream();
InputStream inStream=socket.getInputStream();
in=新对象输出流(流内);
out=新对象输出流(输出流);
布尔完成=假;
对象inObject=null;
字符串welcome=“#欢迎消息#”;
out.writeObject(欢迎);
System.out.println(“已建立客户端连接”);
而(!完成){
试一试{
inoobject=in.readObject();
if(inObject instanceof String){
String rawCommand=(String)inoobject;//[Command]u[Level]
String[]splits=rawscommando.split(“”);
字符串命令=拆分[0];
int-level=Integer.parseInt(拆分[1]);
kochfractal.setLevel(level);
System.out.println(“收到的命令:“+Command+”,级别:“+level”);
if(command.equals(“EXIT”)){
完成=正确;
}否则{
if(command.equals(“列表”)){
边。清除();
kochfractal.generateEdges();
out.writeObject(边);
out.flush();
}
}
}否则{
System.out.println(“!接收到无效命令!”);
}
}catch(classnotfounde异常){
System.out.println(“对象类型未知”);
}
}
in.close();
out.close();
socket.close();
}捕获(IOE异常){
}
}
客户端:

    public void sendCommandList(int level) throws IOException, ClassNotFoundException {
     String command = "LIST_" + Integer.toString(level);
     out.writeObject(command);
     System.out.println("Command send: " + commando);
     out.flush();
     List<Edge> edges = new ArrayList<Edge>();
     System.out.println("Reading List..");
     edges = (List<Edge>) in.readObject();
     System.out.println("List has been read. # of edges: " + edges.size());        
}
public void sendCommandList(int级别)抛出IOException、ClassNotFoundException{
String command=“LIST_”+Integer.toString(级别);
out.writeObject(命令);
System.out.println(“命令发送:“+commando”);
out.flush();
列表边=新的ArrayList();
System.out.println(“阅读列表”);
.readObject()中的边=(列表);
System.out.println(“已读取边列表:+edges.size());
}

修复了它,添加了。重置();在服务器端刷新之后。我认为刷新就足够了。

需要查看kochfractal对象的代码…,特别是setLevel和generateEdges()。另外,您的代码对于多个客户机来说不是线程安全的,如果不同的客户机同时请求不同的级别怎么办?edges和kochfractal应该是每个客户的,在这里很难显示出来,所以这里有一个包含两个项目的Zip链接。(评论是荷兰语,但我希望你能理解)