【问题标题】:Java Client-Server Cant get the Server to keep sending the right informationJava Client-Server 无法让服务器继续发送正确的信息
【发布时间】:2017-12-02 03:03:26
【问题描述】:

我目前正在制作一个客户端-服务器程序,它让客户端不断创建一个包含 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();

}
}

这是服务器代码:

public class Server {
    static List clientNums = new ArrayList();
    static List primes = new ArrayList();
    public static void main(String[] args) throws Exception {



    System.out.println("Running Server");
    ServerSocket welcome = new ServerSocket(6789);

    Socket connectionSocket = welcome.accept();
    System.out.println("Connected");

    ObjectInputStream inFromClient = 
            new ObjectInputStream(connectionSocket.getInputStream());

    ObjectOutputStream toClient = 
            new ObjectOutputStream(connectionSocket.getOutputStream());
    toClient.flush();

    while(true) {
        clientNums = (ArrayList) inFromClient.readObject();
        System.out.println("Client list: " + clientNums);

        for(int i = 0; i < clientNums.size(); i++) {
            if(isPrime((int) clientNums.get(i))) {
                primes.add(clientNums.get(i));
            }
        }//End for loop

        System.out.println("Received: " + primes);
        toClient.writeObject(primes);
        primes.clear();
    }//End while loop
}//End main
public static boolean isPrime(int n) { //CITE: https://www.mkyong.com/java/how-to-determine-a-prime-number-in-java/
    for(int i=2;2*i<=n;i++) {
        if(n%i==0)
            return false;
    }
    return true;

  }
}

现在一切都按照我的意愿运行了...除了当客户端发送第一个列表时,它将正确接收回第一个素数列表。但是对于第二个列表及以后的列表,它继续只接收第一个素数列表而不是新列表;即使认为服务器仍在正确接收并制作素数列表。我被这件事难住了一段时间。任何人都可以帮忙吗?

这是客户端的输出:

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]

【问题讨论】:

    标签: java sockets serialization server client


    【解决方案1】:

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

    【讨论】:

    • 这应该只在服务器还是客户端上完成?
    猜你喜欢
    • 2019-01-02
    • 2023-03-08
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多