Java:socket未获取输入

Java:socket未获取输入,java,sockets,input,io,output,Java,Sockets,Input,Io,Output,所以,我正在做一个井字游戏项目,我的网络部分出现了问题,一切都结束了,只是缺少了连接玩家之间的部分,这是有问题的课程: public class Enemy implements Runnable{ private static Socket enemy; public Enemy(Socket sock){ enemy = sock; } public static void passaJogada(int xPos, int yPos){

所以,我正在做一个井字游戏项目,我的网络部分出现了问题,一切都结束了,只是缺少了连接玩家之间的部分,这是有问题的课程:

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没有在读

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

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

从现有的OutputStream创建一个新的PrintWriter,,而不进行自动换行

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

PrintWriter saida = new PrintWriter(enemy.getOutputStream(), true);
…或显式刷新PrintWriter:

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

我假设你希望玩家彼此玩,即使他们都不在服务器“游戏”所在的计算机上。在这种情况下,您应该做的是将游戏与网络分开。我还要假设你的所有游戏部分都能正常工作。这就是你能做的。 1) 首先创建一个类,其中只有连接到服务器的部分发生,您可以将其命名为server(在下面的示例代码中查看)。 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");
     }
    }
}
这是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);
        }
    }
}

请让我知道你的情况如何。也许我可以帮你更好:)祝你好运:)

套接字的另一面是否为
readLine()
(例如一个新行字符)编写了正确的delim?套接字的另一面是passaJogada方法,它正在用println方法打印一个只包含int的字符串。我已经有一个用于网络的类,它有一些javafx在它的用户界面,如果你想我可以发布所有的代码为你帮助我(如果你可以)。我也是新的。我还是个学生。但我喜欢帮助并尝试解决这些问题。将代码链接给我,以便至少可以看到,也许我可以改进并帮助您使用代码。1。您还没有演示如何发送。2.
Socket.isBound()
在连接后将永远不会返回false。这不是连接有效性的有效测试。没有一个:然而,
readUTF()
在流的末尾抛出
EOFException
,您没有捕捉到它并正确处理它。我只是想展示一下bacis将人们连接到套接字的方法。我知道代码需要修改。我只是想帮他。如果你愿意的话,我可以修改代码。哇,它成功了!!谢谢你,伙计,对不起,我不能投你的票。
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);
        }
    }
}