Java套接字的问题(在井字游戏中)

4
我用Java编写了一个双人井字游戏,使用了套接字(没有线程)。我已经让它运行起来了,除了结束游戏时终止客户端。基本上,一旦游戏结束(服务器识别出胜利或平局),服务器将向两个客户端发送消息。如果客户端读取到该特定消息,则它们将“退出”do-while循环并关闭套接字连接。
问题是,每当一个客户端关闭时,另一个客户端就会“崩溃” - 它将终止,但不成功(它将抛出一个错误消息)。想象一下打开三个终端 - 两个客户端和一个服务器。如果我在其中一个客户端终端上按“Ctrl-C”(退出),则另一个客户端将停止。客户端应该是完全独立的,我不明白为什么会发生这种情况。
我将发布我的服务器代码和客户端代码(已删除井字游戏逻辑) - 有人能看出我做错了什么吗?谢谢!
更新:我在try-catch中添加了打印语句,但这并没有解决问题。我收到的错误消息如下:
 Exception in thread "main" java.lang.ClassCastException: java.lang.String
    at Requester.run(Requester.java:32)
    at Requester.main(Requester.java:142)

我修改了下面的代码,包括所有的井字棋逻辑。Requester.java:32是:

 currentPlayer = (Integer) in.readObject();

...在第一个do-try语句之后立即执行。有人能看出发生了什么吗?

服务器


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

public class Provider {

  TBoard board = new TBoard();

  ServerSocket providerSocket;
  Socket connection1 = null, connection2 = null;
  ObjectOutputStream out, out2;
  ObjectInputStream in, in2;
  String message;
  Boolean done = false;

  int row;
  int col;

   Provider() {
  }

   void run() {
     try {
        providerSocket = new ServerSocket(20092);

        System.out.println("Waiting for connection...");
        connection1 = providerSocket.accept();
        System.out.println("Connection received from Player 1 "
           + connection1.getInetAddress().getHostName());
        connection2 = providerSocket.accept();
        System.out.println("Connection received from Player 2 "
           + connection2.getInetAddress().getHostName());

        out = new ObjectOutputStream(connection1.getOutputStream());
        out2 = new ObjectOutputStream(connection2.getOutputStream());

        in = new ObjectInputStream(connection1.getInputStream());
        in2 = new ObjectInputStream(connection2.getInputStream());

        do {

           if (board.get_player() == 1) {
              out.writeObject(board.get_player());
              out.flush();
              out.writeObject(board.print_board());
              out.flush();
           } 
           else {
              out2.writeObject(board.get_player());
              out2.flush();
              out2.writeObject(board.print_board());
              out2.flush();
           }

           sendMessage(board.get_player(),
              "Please enter a row, press Enter, then enter a column: ");

           if (board.get_player() == 1) {
              int[][] c_array = (int[][]) in.readObject();
              board.set_array(c_array);
           } 
           else {
              int[][] c_array = (int[][]) in2.readObject();
              board.set_array(c_array);
           }

           if (board.get_player() == 1) {
              board.set_player(2);
           } 
           else {
              board.set_player(1);
           }

           if (board.winner() != 0) {


              System.out.print("The winner is...");

              if (board.get_player() == 1) {
                 System.out.println("Player 2!");
              } 
              else {
                 System.out.println("Player 1!");
              }

              out.writeObject("bye");
              out.flush();
              out2.writeObject("bye");
              out2.flush();

              done = true;


           } 
           else {

              if(board.get_player() == 2){
                 out.writeObject("nothing");
                 out.flush();
              }
              else{
                 out2.writeObject("nothing");
                 out2.flush();
              }


           }

        } while (done != true);

     } 
         catch (IOException ioException) {
           ioException.printStackTrace();
        } 
         catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
           e.printStackTrace();
        } 
     finally {

        try {

           in.close();
           out.close();
           in2.close();
           out2.close();
           providerSocket.close();
        } 
            catch (IOException ioException) {
              ioException.printStackTrace();
           }
     }
  }

   void sendMessage(int player, String msg) {
     try {
        if (player == 1) {
           out.writeObject(msg);
           out.flush();

        } 
        else {
           out2.writeObject(msg);
           out2.flush();
        }
     } 
         catch (IOException ioException) {
           ioException.printStackTrace();
        }
  }

   public static void main(String args[]) {
     Provider server = new Provider();
     while (true) {
        server.run();
     }
  }

客户:


    import java.io.*;
    import java.net.*;
    import java.util.Scanner;

public class Requester {

  TBoard board = new TBoard();

  Socket requestSocket;
  ObjectOutputStream out;
  ObjectInputStream in;
  String message;
  String endmessage = "";
  int row, col, currentPlayer;

  Scanner scan = new Scanner(System.in);

   Requester() {
  }

   void run() {
     try {
        requestSocket = new Socket("server2.xx.xxxx.xxx", 20092);
        System.out.println("Connected to localhost in port 20092");

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

        do {
           try {

              currentPlayer = (Integer) in.readObject();
              board.set_player(currentPlayer);

              int[][] b_array = (int[][]) in.readObject();
              board.set_array(b_array);

              for (int i = 0; i < 3; i++) {
                 System.out.println("");
                 for (int j = 0; j < 3; j++) {
                    if (b_array[i][j] == 1) {
                       System.out.print(" X");
                    } 
                    else if (b_array[i][j] == 2) {
                       System.out.print(" O");
                    } 
                    else {
                       System.out.print(" -");
                    }
                 }
              }

              System.out.println();

              message = (String) in.readObject();
              System.out.print(message);

              row = scan.nextInt();
              while (row < 0 || row > 2) {
                 System.out
                    .print("Row is invalid, please choose again (0-2): ");
                 row = scan.nextInt();
              }

              col = scan.nextInt();
              while (col < 0 || col > 2) {
                 System.out
                    .print("Column is invalid, please choose again (0-2): ");
                 col = scan.nextInt();
              }

              while (!board.make_move(row, col)) {
                 System.out
                    .print("The move is not valid. Please choose another row (0-2): ");
                 row = scan.nextInt();

                 while (row < 0 || row > 2) {
                    System.out
                       .print("Row is invalid, please choose again (0-2): ");
                    row = scan.nextInt();
                 }

                 System.out.print("Please choose a column (0-2): ");
                 col = scan.nextInt();

                 while (col < 0 || col > 2) {
                    System.out
                       .print("Column is invalid, please choose again (0-2): ");
                    row = scan.nextInt();
                 }
              }

              for (int i = 0; i < 3; i++) {
                 System.out.println("");
                 for (int j = 0; j < 3; j++) {
                    if (b_array[i][j] == 1) {
                       System.out.print(" X");
                    } 
                    else if (b_array[i][j] == 2) {
                       System.out.print(" O");
                    } 
                    else {
                       System.out.print(" -");
                    }
                 }
              }

              System.out.println();

              out.writeObject(board.print_board());
              out.flush();

              endmessage = (String) in.readObject();

           } 
               catch (ClassNotFoundException classNot) {
                 System.err.println("data received in unknown format");
              }
        } while (!endmessage.equals("bye"));
     } 
         catch (UnknownHostException unknownHost) {
           System.err.println("You are trying to connect to an unknown host!");
        } 
         catch (IOException ioException) {
           ioException.printStackTrace();
        } 
     finally {
        try {
           in.close();
           out.close();
           requestSocket.close();
        } 
            catch (IOException ioException) {
              ioException.printStackTrace();
           }
     }
  }

   void sendMessage(int msg) {
     try {
        out.writeObject(msg);
        out.flush();
     } 
         catch (IOException ioException) {
           ioException.printStackTrace();
        }
  }

   public static void main(String args[]) {
     Requester client = new Requester();
     client.run();
  }

任何帮助都将不胜感激,我已经被这个问题困扰了好几天。谢谢!


抛出的错误信息是什么? - Justin Niessner
抛出的错误信息是 ClassCastException - 基本上,如果 Client1 进行了一次移动,服务器将检查是否获胜,并在客户端的 do-while 结尾检查 "done" 消息。如果接收到该消息,Client1 将终止,而 Client2 将由于处于 do-while 循环的顶部,等待来自服务器的第一个指令而崩溃,从而产生上述错误。 - littleK
2
看起来你已经成功地注释掉了代码,这段代码实际上会抛出ClassCastException异常(// 井字游戏逻辑)。无法看到你正在通过网络发送什么以及接收到的数据是如何处理的。 - jarnbjo
jarnbjo - 我添加了我的代码,如果您能看一下就非常感谢了... - littleK
可以添加 TBoard 类吗? - Martijn Courteaux
4个回答

2
当两个用户中的一个关闭程序时,连接就会“断开”。然后socket类会抛出一个IOException异常,你需要捕获它:
   ...
   } catch (IOException ioException) {
            ioException.printStackTrace();
   } 
   ...

但是在捕获异常之后,您什么都没有做。您需要添加一些信息并说明连接已断开。我认为您的程序并没有崩溃,只是停止了,因为它捕获了异常,然后在 finally 中关闭了流。
希望这可以帮助您。

1

我认为你的“同步”被卡住了。尝试打印出你读取的变量。并且也打印出你认为你将要读取的内容。

例如:

// Set Current Player
System.out.println("Reading current player");
Object inputCP = in.readObject();
System.out.println(inputCP.getClass().getName() + " " + inputCP);
board.set_player((int) (Integer) inputCP);

对于写入操作也要做同样的处理,然后比较输出结果。这样你就可以看到发送和接收数据同步出现了什么问题。

注意:如果你将一个对象写入ObjectOutputStream中,它会自动刷新。


你好,你能逐步解释如何使用Java套接字实现井字棋吗? - spt

1
这里可能是一次瞎猜,但捕获IOException并不能解决ClassCastException的问题,而这正是你所遇到的错误。
Exception in thread "main" java.lang.ClassCastException: java.lang.String
at Requester.run(Requester.java:32)
at Requester.main(Requester.java:142)

你说Requester的第32行是:

currentPlayer = (Integer) in.readObject();

你试图将in.readObject()转换为整数,但实际上它是一个字符串,因此会出现异常。

但不要仅仅捕获异常,要找出为什么会得到一个字符串而不是整数。

我猜测可能是你发布的服务器代码和void sendMessage(int player, String msg)方法。看起来你正在发送一个字符串消息,但期望在另一端得到一个整数。你可能需要稍微整理一下这个问题。


1
在你的Server类中的catch (IOException ioException) { }语句块中,你可以捕获抛出异常的ObjectInputStream对象(因为关闭客户端会在服务器端引发IOException),然后向另一个客户端发送“胜利”,因为对手已经断开连接。

Nettogrof- 请看我上面的更新。我尝试过这样做,但似乎不是问题所在... - littleK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接