所以我創建一個服務器和客戶端程序,ObjectInputStream的停止循環
服務器類:
package Networking;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class Server {
private static ArrayList<Socket> sockets = new ArrayList<Socket>();
private static ArrayList<ObjectOutputStream>os = new ArrayList<ObjectOutputStream>();
private static ArrayList<ObjectInputStream> is = new ArrayList<ObjectInputStream>();
private static ArrayList<Player> players = new ArrayList<Player>();
public static void main(String args[]) throws IOException, ClassNotFoundException{
ServerSocket listener = new ServerSocket(7321);
System.out.println("Server Started");
int fps = 60;
double timePerTick = 1000000000/fps;
double delta = 0.0D;
long lastTime = System.nanoTime();
long timer = 0L;
int ticks = 0;
while (true) {
long now = System.nanoTime();
delta += (now - lastTime)/timePerTick;
timer += now - lastTime;
lastTime = now;
if (delta >= 1.0D) {
Socket socket = listener.accept();
if(socket != null){
sockets.add(socket);
os.add(new ObjectOutputStream(sockets.get(sockets.size()-1).getOutputStream()));
is.add(new ObjectInputStream(sockets.get(sockets.size()-1).getInputStream()));
players.add((Player)is.get(sockets.size()-1).readObject());
}
ticks++;
delta -= 1.0D;
}
for(int i = 0 ;i < sockets.size();i++){
os.get(i).writeObject(players);
os.get(i).flush();
}
if (timer >= 1000000000L) {
ticks = 0;
timer = 0L;
}
for(int i =0; i < sockets.size();i++){
}
}
}
}
客戶端類
package Networking;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import com.sun.prism.paint.Stop;
import Dynamics.Display;
public class Client implements Runnable {
private final String serverAddress = "localhost";
private static int PORT = 7321;
private Socket socket;
private ObjectInputStream in;
private ObjectOutputStream out;
private boolean connected = false;
private static Player player = null;
private boolean isrunning = false;
private ArrayList<Player> enemy;
private Thread thread;
private Display display;
public static void main(String args[]) throws ClassNotFoundException{
Client c = new Client();
c.connect();
c.start();
}
public Client(){
player = new Player();
display = new Display("Game: " + player.getName());
}
public void connect() throws ClassNotFoundException{
try{
socket = new Socket(serverAddress, PORT);
connected = true;
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
out.writeObject(player);
}catch(UnknownHostException e){e.printStackTrace();}
catch (IOException e){e.printStackTrace();}
}
private synchronized void start(){
if(isrunning)
return;
isrunning = true;
this.thread = new Thread(this);
this.thread.start();
this.display.getFrame().setVisible(true);
}
public void run() {
int fps = 60;
double timePerTick = 1000000000/fps;
double delta = 0.0D;
long lastTime = System.nanoTime();
long timer = 0L;
int ticks = 0;
while (isrunning) {
long now = System.nanoTime();
delta += (now - lastTime)/timePerTick;
timer += now - lastTime;
lastTime = now;
if (delta >= 1.0D) {
tick();
render();
ticks++;
delta -= 1.0D;
}
if (timer >= 1000000000L) {
ticks = 0;
timer = 0L;
}
}
}
public void tick(){}
public void render(){
CheckEnemys();
}
@SuppressWarnings("unchecked")
public void CheckEnemys() {
try {
enemy = (ArrayList<Player>)in.readObject();
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(enemy.size());
}
}
我的問題,當我談到運行循環不斷檢查新玩家,循環在一次迭代後停止。播放器類可以使用默認構造函數和名稱進行序列化。 Idk y循環雖然停止,但