Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.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客户端服务器无法让服务器继续发送正确的信息_Java_Sockets_Serialization_Server_Client - Fatal编程技术网

Java客户端服务器无法让服务器继续发送正确的信息

Java客户端服务器无法让服务器继续发送正确的信息,java,sockets,serialization,server,client,Java,Sockets,Serialization,Server,Client,我目前正在制作一个客户机-服务器程序,让客户机连续制作一个包含5个随机整数的列表,然后将其发送给客户机,并不断重复,直到我告诉它停止 服务器将从客户端获取列表,找出哪些数字是素数,将素数放入新列表中,然后将其从客户端发送出去,在客户端发送列表时重复该列表 客户代码如下: public class Client2 { static boolean isRunning = false; public static void main(String[] args) throws Excep

我目前正在制作一个客户机-服务器程序,让客户机连续制作一个包含5个随机整数的列表,然后将其发送给客户机,并不断重复,直到我告诉它停止

服务器将从客户端获取列表,找出哪些数字是素数,将素数放入新列表中,然后将其从客户端发送出去,在客户端发送列表时重复该列表

客户代码如下:

public class Client2 {
    static boolean isRunning = false;


public static void main(String[] args) throws Exception {
    System.out.println("Running Client");

    Socket clientSocket = new Socket(InetAddress.getLocalHost(), 6789);
    ObjectOutputStream toServer = 
            new ObjectOutputStream(clientSocket.getOutputStream());
    toServer.flush();

    ObjectInputStream inFromServer = 
            new ObjectInputStream(clientSocket.getInputStream());

    Random randint = new Random();
    Scanner input = new Scanner(System.in);
    System.out.println("Enter “!” to startand stop, “#” to quit:");

    if(input.nextLine().equals("!")) {
        isRunning = true;
    }

    Thread t = new Thread(new Runnable(){

        @Override
        public void run() {
            while(isRunning) {
                List<Integer> randList = new ArrayList<Integer>(5);
                //Makes the Random List
                for(int i = 0; i < 5; i++) {
                    int num = randint.nextInt(98)+2;
                    randList.add(num);
                }

                //Sleeps the thread
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                //Writes the list
                try {
                    toServer.writeObject(randList);
                    toServer.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("Send: " + randList);

                try {
                    System.out.println("Received: " + inFromServer.readObject());
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }//End while loop   
            }   
    });
    Thread t2 = new Thread(new Runnable(){

        @SuppressWarnings("deprecation")
        @Override
        public void run() {
            while(true) {
                if(input.nextLine().equals("!")) {
                    t.suspend();
                    System.out.println("Sleeping");
                }
                if(input.nextLine().equals("!")) {
                    t.resume();
                    System.out.println("Resuming");
                }   
            }

            }

    }); 
    t.start();
    t2.start();

}
}
和服务器的输出:

Running Server
Connected
Client list: [63, 63, 64, 4, 53]
Received: [53]
Client list: [43, 6, 70, 67, 69]
Received: [43, 67]
Client list: [2, 29, 83, 45, 67]
Received: [2, 29, 83, 67]

您需要调查
ObjectOutputStream.reset()
ObjectOutputStream.writeUnshared()

这应该只在服务器上还是在客户端上进行?
Running Client
Enter “!” to startand stop, “#” to quit:
!
Send: [63, 63, 64, 4, 53]
Received: [53]
Send: [43, 6, 70, 67, 69]
Received: [53]
Send: [2, 29, 83, 45, 67]
Received: [53]
Running Server
Connected
Client list: [63, 63, 64, 4, 53]
Received: [53]
Client list: [43, 6, 70, 67, 69]
Received: [43, 67]
Client list: [2, 29, 83, 45, 67]
Received: [2, 29, 83, 67]