2015-05-03 35 views
1

所以,我正在製作一個tic tac toe項目,並且我在網絡部分遇到了一個問題,它全部完成,只缺少連接玩家的部分,這是類問題:Java:套接字沒有輸入

public class Enemy implements Runnable{ 
    private static Socket enemy; 

    public Enemy(Socket sock){ 
     enemy = sock; 
    } 

    public static void passaJogada(int xPos, int yPos){ 
     try { 
      PrintWriter saida = new PrintWriter(enemy.getOutputStream()); 
      String x = "" + xPos; 
      saida.println(x); 
      String y = "" + yPos; 
      System.out.print(x + y); 
      saida.println(y); 

     } catch (IOException e) { 
      System.out.println("Ocorreu um erro!"); 
     }  
    } 

    public void run() { 
     try { 
      BufferedReader entrada = new BufferedReader(new InputStreamReader(enemy.getInputStream())); 
      while(!EndGameWindow.getEnd()) {  
       int x = Integer.parseInt(entrada.readLine()); 
       int y = Integer.parseInt(entrada.readLine()); 
       GameWindow.gameButton[x][y].fazerJogada(x,y); 
      } 
      entrada.close(); 
     } catch (IOException e) { 
      System.out.println("Um errro ocorreu!"); 
     } 
    } 
} 

和我沒有什麼事情的線索,我所知道的是,PrintWriter的是寫作,但BufferedReader中不讀書。

只是忽略了變量和方法的葡萄牙語名稱。

+0

插座的另一端是否爲'readLine()'(例如換行符)編寫適當的分隔符? – copeg

+0

套接字的另一面是passaJogada方法,它使用println方法打印只有int的字符串。 –

回答

1

見API爲PrintWriter,特別是單一參數OutputStream構造你使用:

創建一個新的PrintWriter,沒有自動行刷新,根據現有的OutputStream。

換句話說,PrintWriter被緩衝並需要刷新。要啓用自動行刷新,使用適當的構造函數

PrintWriter saida = new PrintWriter(enemy.getOutputStream(), true); 

...或明確沖洗的PrintWriter:

.... 
saida.println(y); 
saida.flush(); 
+0

哇,它的工作!謝謝你,夥計,抱歉,我不能投票你的帖子。 –

-1

我假設你希望玩家互相玩,即使他們沒有坐在服務器「遊戲」所在的計算機上。在這種情況下,你應該做的是將遊戲與網絡分開。我也會假設你所有的遊戲部分都應該如此。所以這裏是你能做的。 1)首先創建一個只有連接到服務器部分的類,你可以將它命名爲服務器(請看下面的示例代碼)。 2)讓另一個班級成爲這些客戶的對象(在你的情況下,玩家)。(再次看下面的代碼) 3)然後你可以讓他們與遊戲交互。

如果您在修理時遇到問題,請告訴我也許我可以幫助您瞭解更多細節。

你簡單的服務器部分。

import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.ArrayList; 
import java.util.List; 


/** 
* Created by baljit on 03.05.2015. 
* @author Baljit Sarai 
*/ 

public class Server { 
    ServerSocket serverSocket; 
    public static List<ClientConnection> clientConnectionList = new ArrayList<ClientConnection>(); 
    public Server() { 
     try { 
      serverSocket = new ServerSocket(12345); 
      System.out.println("Server started"); 
     } catch (IOException ioe) { 
      System.out.println("Something went wrong"); 
     } 
    } 




public void serve(){ 
    Socket clientSocket; 
    try { 
     while((clientSocket = serverSocket.accept()) != null){ 
      System.out.println("got client from"+clientSocket.getInetAddress()); 
      ClientConnection clientConnection = new ClientConnection(clientSocket); 
      clientConnectionList.add(clientConnection); 
      ClientHandler clientHandler = new ClientHandler(clientConnection,serverSocket); 
      clientHandler.start(); 
     } 
    }catch (IOException ioe){ 
     System.out.println("Something went wrong"); 
    } 
    } 
} 

在這種情況下,clientConnection可以是pllayer對象,你應該做只是爲了讓事情你自己更容易。

clientHandler是可以同時處理每個玩家的類。

這裏的「clientConnectionList」只是存儲連接,所以你知道在哪裏找到它們。

所以這裏是ClientConnection類。

import java.io.*; 
import java.net.Socket; 

/** 
* Created by baljit on 03.05.2015. 
* @author Baljit Sarai 
*/ 
public class ClientConnection { 
    private Socket connection; 
    private DataInputStream streamIn; 
    private DataOutputStream streamOut; 
    private BufferedInputStream bufferedInputStream; 
    public ClientConnection(Socket socket){ 
     connection = socket; 
     try{ 

      bufferedInputStream = new BufferedInputStream(connection.getInputStream()); 
      streamIn = new DataInputStream(bufferedInputStream); 
      streamOut = new DataOutputStream(connection.getOutputStream()); 
     }catch (IOException ioe){ 
      System.out.println("Something went wrong when trying create the Connection Object"); 
     } 
    } 

    public boolean isBound(){ 
     return connection.isBound(); 
    }  
    public DataInputStream getStreamIn() { 
     return streamIn; 
    } 

    public DataOutputStream getStreamOut() { 
     return streamOut; 
    } 

    public void close(){ 
     try{ 

      connection.close(); 
     }catch (IOException ioe){ 
      System.out.print("Something went wrong trying to close the connection"); 
     } 
    } 

    public String getAddress(){ 
     return connection.getInetAddress().toString(); 
    } 




} 

這裏是ClientHandler的類

import java.io.*; 
import java.net.ServerSocket; 
import java.net.Socket; 

/** 
* Created by baljit on 03.05.2015. 
* @author Baljit Sarai 
*/ 
public class ClientHandler extends Thread{ 
    private ServerSocket serverSocket; 
    String data; 
    ClientConnection connection; 

    public ClientHandler(ClientConnection clientConnection, ServerSocket serverSocket) { 
     connection = clientConnection; 
    } 

    public void makeMove(String data){ 
     //This is where you put the logic for how the server can 
     //interpetate the readed data to a game move 

    } 

    @Override 
    public void run(){ 
     System.out.println("ClientHandler running"); 
     try{ 
      while(connection.isBound()){ 
       data= connection.getStreamIn().readUTF(); 
       this.makeMove(data); 
      } 
      connection.close(); 
     }catch (IOException ioe){ 
      System.out.println("Connection dead "+connection.getAddress()); 
      Server.clientConnectionList.remove(connection); 
     } 
    } 
} 

請讓我知道它是如何工作爲你。也許我可以幫助你更好:)祝你好運:)

+0

我已經有一個類只是爲了網絡,它有一些用於UI的javafx,如果你想我可以發佈你所有的代碼來幫助我(如果可以的話)。 –

+0

我也是新人。我還是學生。但我喜歡幫助並嘗試解決這些問題。鏈接我的代碼,以便至少可以看到,也許我可以改進和幫助你的代碼。 – Wafsek

+0

1.你沒有展示如何發送。 2.連接後''Socket.isBound()'永遠不會返回false。這不是對連接有效性的有效測試。沒有一個:但是'readUTF()'在流結束時拋出'EOFException',並且你沒有捕獲它並正確處理它。 – EJP