[Networking] Reading a String[] [SOLVED]

Well, my problem today is that when I attempt to send a String[] to the client, it won’t print out each index of the String[] :confused:

Here is what I came up with for reading a ArrayList:


	public Object readObject() throws IOException, ClassNotFoundException {
		Object inc = getInputStream().readObject();
		if (inc instanceof ArrayList) {
			ArrayList<?> list = (ArrayList<?>) inc;
			int size = list.size();
			System.out.println("ArrayList<?> Size: "+size);
			return list;
		}
		return null;
	}

Returns:

Now that works perfect, the client receives the ArrayList, and prints out each index correctly.

Now onto reading a String[], this is what I have so far :emo::


	public Object readObject() throws IOException, ClassNotFoundException {
		Object inc = getInputStream().readObject();
		if (inc instanceof String[]) {
			/* Create a Instance of the incoming String[] */
			String[] tmp = (String[]) inc;
			/* Calculate the length of the incoming String[] */
			int size = tmp.length;
			System.out.println("String[] Size: "+size);
			String[] tmp2 = new String[size];
			/* Attempt to assign each index of the incoming String[] to a new String[] for printing */
			for (int i = 0; i < size; i++) {
				tmp2[i] = tmp[i];
				return tmp2;
			}
		}
		return null;
	}

Returns:

Please don’t post a reply relating to Serialization, for I am attempting to do this my way ;D.
Any help on this matter would be greatly appreciated.
If you notice a error in the code for reading the String[] please tell me.

Other Info:
When I highlight the block of code for reading the String[] in my IDE (Eclipse), it says “Dead Code” T_T.

You should use a ByteBuffer o.o

Thanks Agro, looking into it as we speak, if anyone else has a idea please do post it. :smiley:
I’ll post back with my progress.

If you use a ByteBuffer, you can basically “write” any data type it allows you to. For example, in C++, you could just write the pointer of an object to a byte array and then just send it out. On the server or client side, you can just typecast that data to the pointer.

You could do what you’re doing right now, but I think it would be smarter to create a Packet class to hold all the data. It would be more organized then.

Great, I got it working :slight_smile:

Now Returns:

Sent From Server:


	Object[] lmfao = new Object[10];
	for(int i = 0; i < lmfao.length; i++) {
		lmfao[i] = "lol: "+i;
	}
	stream.writeObject(lmfao);

Method Now:


	public Object readObject() throws IOException, ClassNotFoundException {
		Object inc = getInputStream().readObject();
		if (inc instanceof Object[]) {
			Object[] arr = (Object[]) inc;
	        for (Object obj : arr) {  
	        	logStream("Incoming: "+obj, false);
	        }  
	        return arr;
		}
		return null;
	}