Java:socket没有输入

时间:2015-05-03 20:35:23

标签: java sockets input io output

所以,我正在制作一个tic tac toe项目,我遇到了网络部分的问题,它已经完成,只是错过了连接玩家的部分,这个是有问题的班级:

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没有读取。

只需忽略变量和方法的葡萄牙语名称。

2 个答案:

答案 0 :(得分:1)

请参阅PrintWriter的API,特别是您正在使用的单个参数OutputStream构造函数:

  

从现有的OutputStream创建一个新的PrintWriter,不带自动行刷新。

换句话说,PrintWriter是缓冲的,需要刷新。要启用自动行刷新,请使用适当的构造函数

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

...或显式刷新PrintWriter:

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

答案 1 :(得分:-1)

我假设您希望玩家互相玩耍,即使他们都没有坐在服务器上的游戏“游戏”#34;是。在这种情况下,你应该做的是将游戏与网络分开。我还假设你让所有的游戏部分都像它应该的那样工作。所以这就是你能做的。 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是可以同时处理每个玩家的类。

&#34; clientConnectionList&#34;这里只是存储连接,以便您知道在哪里找到它们。

所以这是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);
        }
    }
}

请告诉我它是如何为您服务的。也许我可以帮助你更好:)祝你好运:)