0

So I am writing a game of LAN tic-tac-toe. I have the "board" stored as a 2 dimensional char array. I want to be able to send and receive this array through Sockets. I am currently using a InputStream and OutputStream to send single bytes. However, I don't think this will work for sending the array. Also, these streams only seem capable of sending int type data. Can someone please explain to me how too send 2 dimensional char arrays over sockets using I/O streams. Example code would be great! Thanks.

Current Code:

public void communicate() {
    try {
        OutputStream os = client.getOutputStream();
        InputStream is = client.getInputStream();
    }

    while (gameOver == false) {
        char[][] board = new char[3][3];
        try {
            os.write(board); //this dosen't work, only sends non-array int types. 
        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}
Ashwin Gupta
  • 2,159
  • 9
  • 30
  • 60
  • You need to serialize your objects or use XML, JSON, or any other string-based format. – PM 77-1 Nov 24 '15 at 19:51
  • @PM77-1 well, I do hope that isn't the only way because I don't know how to use XML or JSON. Are you sure? This honestly seems like a relatively simple task that Java should be able to handle alone. I could always convert the char array into some other type of array like String or int then when its recieved on the other end, I could change it back. – Ashwin Gupta Nov 24 '15 at 19:54
  • `Array` is an object. If you use serialization, Java will create string representation of your objects for you. See [Storing and Reading Objects](https://docs.oracle.com/javase/tutorial/jndi/objects/store.html) – PM 77-1 Nov 24 '15 at 20:00
  • @PM77-1 ah, okay thanks, I get it, also found this in the meantime: http://stackoverflow.com/questions/14054828/sending-and-receiving-2d-arrays-java. Could you write me an example code though? It would help. – Ashwin Gupta Nov 24 '15 at 20:02

1 Answers1

1

Java InputStream and OutputStream classes only deal with reading and writing byte(s). You can certainly use write your character array as bytes using just input and output streams.

public static char[][] readBoard(InputStream in) throws IOException {
    char[][] board = new char[3][3];
    for(int i=0;i<9;i++) {
        board[i/3][i%3] = (char) in.read();
    }
    return board;
}

public static void writeBoard(OutputStream out, char[][] board) throws IOException {
    for(int i=0;i<9;i++) {
        out.write(board[i/3][i%3]);
    }
}

You could also use the ObjectOutputStream and ObjectInputStream to read and write objects through streams. Note that classes read and written to these streams must implement the Serializable interface (your char[][] will work).

public static char[][] readBoard(InputStream in) throws IOException {
    ObjectInputStream ois = new ObjectInputStream(in);
    return (char[][]) ois.readObject();
}

public static void writeBoard(OutputStream out, char[][] board) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream(out);
    oos.writeObject(board);
}
ug_
  • 11,267
  • 2
  • 35
  • 52
  • Okay this is exactly what I was looking for. Also @PM 77-1 thank you, I think you were saying the same thing. I'm probably going to go with the 2nd way because it seems a bit cleaner that individually sending/recieving each byte of the array. Btw, +1 for clever use of the single for loop of the 2D array. I always used 2 nested for loops in the past for 2D arrays. – Ashwin Gupta Nov 24 '15 at 20:11