Matrix reading sent by Socket JAVA

0

I can send an array from the Server Class to the Client class and read it, but I can not read a matrix.

Where is the error?

public class Client extends Thread {

InetAddress host = null;
Socket socket = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
 //int arreglo[][] = new int[2][2];

public void run() {
    try {

        for (int x = 0; x < 1; x++) {

            host = InetAddress.getLocalHost();
            socket = new Socket(host.getHostName(), 4444);
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject("Client Message " + x);
            ois = new ObjectInputStream(socket.getInputStream());  
            int[][] arreglo=(int[][])ois.readObject();     
            System.out.print(arreglo[0][0]);

            ois.close();
            oos.close();
            socket.close();
        }
    } catch (Exception e) {
    }
}

}

class Server extends Thread {

Socket socket = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;

public void run() {
      int[][] matriz2 = {{1,1},{1,1}};
      //int[][] arreglo={{1},{2},{3}};

    try {
        ServerSocket server = new ServerSocket(4444);
        while (true) {



            socket = server.accept();
            ois = new ObjectInputStream(socket.getInputStream());
            String message = (String) ois.readObject();
            System.out.println("Server Received: " + message);
             oos = new ObjectOutputStream(socket.getOutputStream());
             oos.writeObject(matriz2);

            ois.close();
            oos.close();
            socket.close();
        }
    } catch (Exception e) {
    }
}

}

    
asked by csvg2 21.04.2018 в 01:54
source

1 answer

0

The code is perfectly valid and the result is as expected. I have copied your code as is in two classes, I have executed them in Eclipse and the client writes in the output the value that is in [0,0].

Check how you are running the client, because it seems more a problem that you do not see the output but that you are not writing it.

    
answered by 21.04.2018 в 12:07