我当前正在制作一个客户服务器程序,该程序使客户端连续列出5个随机整数的列表,然后将其发送给客户端并继续重复此操作,直到我告诉它停止为止。
服务器将从客户端获取列表,找出哪些数字是素数,将素数放入新列表中,然后将其从客户端发送,只要客户端发送列表。
HER是客户代码:
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();
}
}
和HERES服务器代码:
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;
}
}
现在,一切都可以使用我想要的...除了客户端发送第一个列表的事实,它将正确地收回第一个Prime列表。但是对于第二个列表及以后,它继续仅收到第一个列表,而不是新列表。甚至认为服务器仍在收到并正确列出素数清单。我对此有点困难。谁能帮忙?
是客户的输出:
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()
。