1

我有一个在控制台中运行的简单服务器 - 客户端井字游戏,我在本地主机上成功运行了此代码,我现在需要在服务器上部署服务器代码,例如 Heroku,或除我之外的任何其他托管站点作为主持人,我已经遵循 Heroku 教程,但仍然无法弄清楚其余的https://devcenter.heroku.com/articles/java

这是我的服务器代码

public class TCPServer {
    public static void main(String argv[]) throws Exception {
        // String clientSentence;
        ServerSocket welcomeSocket = new ServerSocket(6780);

        BufferedReader[] inFromClient = new BufferedReader[2];
        Socket[] connectionSocket = new Socket[2];
        DataOutputStream[] outToClient = new DataOutputStream[2];
        String labels = "XO";
        for (int i = 0; i < 2; i++) {
            connectionSocket[i] = welcomeSocket.accept();
            inFromClient[i] = new BufferedReader(new InputStreamReader(connectionSocket[i].getInputStream()));
            outToClient[i] = new DataOutputStream(connectionSocket[i].getOutputStream());
            outToClient[i].write(labels.charAt(i));
            System.out.println("one is here");
        }
        int next = 0;
        while (true) {
            String s = inFromClient[next].readLine();
            next ^= 1;
            outToClient[next].writeBytes(s + "\n");
        }
    }
}

这是我的客户代码

class TCPClient {
    public static void main(String argv[]) throws Exception {
        String sentence;
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        Socket clientSocket = new Socket(server, port);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        char[][] XO = new char[3][3];
        for (char[] arr : XO)
            Arrays.fill(arr, '-');
        char playerSign = (char) inFromServer.read();
        boolean turn = false;
        if (playerSign == 'X')
            turn = true;
        while (true) {
            if (turn) {
                sentence = inFromUser.readLine();
                String[] split = sentence.split(" ");
                int a = Integer.parseInt(split[0]);
                int b = Integer.parseInt(split[1]);
                if (XO[a][b] != '-')
                    continue;
                outToServer.writeBytes(sentence + '\n');
                XO[a][b] = playerSign;
                turn = false;
            } else {

                String next = inFromServer.readLine();
                String[] split = next.split(" ");
                XO[Integer.parseInt(split[0])][Integer.parseInt(split[1])] = playerSign == 'X' ? 'O' : 'X';
                turn = true;
            }
            for (char[] arr : XO)
                System.out.println(new String(arr));

        }
        // clientSocket.close();
    }
}

现在我已经尝试在 heroku 上放置相同的服务器代码,但它没有用。我确定我做错了,我不确定我的客户端应该连接到哪个服务器和端口,在这件事上的任何帮助将不胜感激

4

1 回答 1

1

Heroku 首先不支持 TCP 协议,其次,您必须将您的服务器绑定到该服务为您提供的端口。例如,在 node.js 中,您可以使用 来访问它process.env.PORT,但是我承认我对通过 Java 访问这个变量的无知。但是即使您确实使用了他们提供的端口,也只支持 HTTP 和 HTTPS(如果您通过他们的实验室启用它们,则支持 websockets),但它们不支持 TCP。

于 2014-05-18T21:46:50.257 回答