2016-12-03 110 views
-2

所以我創建一個服務器和客戶端程序,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循環雖然停止,但

回答

0

當您打開ObjectInputStream它將阻塞,直到對方刷新其ObjectOutputStream並寫入標頭。確保你先打開輸出流並立即刷新。

out = new ObjectOutputStream(socket.getOutputStream()); 
out.flush(); 
in = new ObjectInputStream(socket.getInputStream()); 

CheckEnemys方法也將阻塞,直到它接收到來自服務器,其中僅當客戶端連接發生的對象。您應該在單獨的線程中運行該方法,並通過同步隊列將事件從主線程提供給主線程。另外,你應該在滴答聲之間Thread.sleep(),而不是循環不停地檢查時鐘。

你也應該reset()你的ObjectOutputStream在你寫消息後,或者它將持有引用的所有內容,防止垃圾收集。

0

您不應該在接受循環中執行任何I/O操作,包括創建ObjectinputStreamsObjectOutputStreams。所有這些都應該在單獨的線程的run()方法中。