gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:55:57 27 4
gpt4 key购买 nike

我目前正在制作一个客户端-服务器程序,让客户端连续创建一个包含 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]

最佳答案

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

关于Java客户端-服务器无法让服务器继续发送正确的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47604150/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com