简体   繁体   中英

Question with sending object over java sockets

So I have been working on a 2 player Tic-Tac-Toe game in java that utilizes sockets. All of the socket stuff is working, and I am sending data back and forth successfully between 2 clients and a server.

I have the following classes: Requester, Provider, and TBoard (which extends Serializable).

In the Requester (client) class, I instantiate an object of TBoard ( TBoard board = new TBoard() ).

I then send that object through the socket to my two clients, via an output stream.

The error I am getting is on the client-side, and it is: Exception in thread "main" java.lang.ClassCastException: java.lang.String

That's happening with:

 board = (TBoard) in.readObject(); in:

 do {
    try {
       board = (TBoard) in.readObject();
       System.out.println(board.print_board());
    } catch (ClassNotFoundException classNot) {
       System.err.println("data received in unknown format");
    }

My print_board() method in the TBoard class is meant to return a 2d array, but for right now (simplification purposes), I have the method returning the string "Hello"...

Does anyone know why this may be happening? I didn't want to bombard you all with code, but please let me know if posting any more may be helpful...

Thanks!


UPDATE:

Here is what I have going on (in more detail) with my Provider (server) class:

 // 1. creating a server socket
 providerSocket = new ServerSocket(20092);

 // 2. Wait for connection
 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());

 // 3. get Input and Output streams
 out = new ObjectOutputStream(connection1.getOutputStream());

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

 in = new ObjectInputStream(connection1.getInputStream());
 out.writeObject("Player 1 has been connected successfully.");
 in2 = new ObjectInputStream(connection2.getInputStream());
 out2.writeObject("Player 2 has been connected successfully.");

 out.flush();
 out2.flush();

 out.writeObject(board);
 out2.writeObject(board);

So I am indeed sending a String in the streams before sending the last object (board). However, I am flushing out the streams beforehand. I also tried reset()'s after the flushes, and it still gave me the IllegalCastException...

IIRC, the class mentioned in the Exception is the one that was actually found, so in the code you're showing, the error would have to be here:

board = (TBoard) in.readObject();

And a String object instead of a TBoard being read from the stream.

Edit: So you are sending strings in addition to the data. There's your problem. You either have to stop sending those strings, or read them on the receiving side before reading the data. Calling flush() is irrelevant to that - it just ensures that the Strings that you have already written to the stream are in fact sent over the connection and not kept in a buffer.

To investigate, I think the easiest would be to visualize (log or debug) the actual class that result from in.readObject() . Sample code:

    Object o = in.readObject();
    System.out.println("Object of class " + o.getClass().getName() + " is " + o);

There are a couple of possibilities. Assuming you are using ObjectOutputStream (which I think you are), it's possible that the error is arising because you are not fully serializing each object. ObjectOutputStream will try and hold of resending if you don't reset the stream each time.

I would try the following:

1) make sure you flush() and close() the sockets at the appropriate times

2) try calling reset() after each object is sent.

3) check that you are sending and receiving the same object type, just in case.

best of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM