2

Recently I am making a server-client program using multithread concept. For some reason, I have to sent an array of string from client to server. I am using ObjectOutputStream and writeObject() method to send that array of string. For example I have an array named String data[] = new String[3] then I send it over socketwriteobject(data).

Question is, how do I receive this array of string on server? Is it ObjectInputStream and this method readObject() can help me? I just want to iterate this object and make it into new array of string in server side. For example String newData[] = new String[3] and then put the string in data[0] into newData[0] until data[2] into newData[2].

1 Answer 1

3

I believe when you read your object using readObject(), it should return your String[] itself.

            String[] myObjects = (String[])inObjectStream.readObject();

Is that not working?

Sign up to request clarification or add additional context in comments.

3 Comments

Yep, its work! thank you. But i have a question, why we must put a try-catch in the block of this code?
Since ObjectInputStream.readObject() throws some exceptions for some possible failures e.g. ClassNotFoundException - Class of a serialized object cannot be found. InvalidClassException - Something is wrong with a class used by serialization. StreamCorruptedException - Control information in the stream is inconsistent. OptionalDataException - Primitive data was found in the stream instead of objects. IOException - Any of the usual Input/Output related exceptions.
If any of the above scenarios arise, i.e. its unable to read the file, it will throw IOException, which you need to handle in catch block. If you don't want to add try/cath, add the above mentioned exceptions as throws IOException, OptionalDataException, InvalidClassException , ClassNotFoundException , StreamCorruptedException` towards the end of of your method signature. Please let me know, if you need any help in doing that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.