2

So I'm trying to send a String[] over an open socket connection. I currently have this code:

Sending:

public void sendData() {
    try {
        OutputStream socketStream = socket.getOutputStream();
        ObjectOutputStream objectOutput = new ObjectOutputStream(socketStream);
        objectOutput.writeObject(new String[] {"Test", "Test2", "Test3"});

        objectOutput.close();
        socketStream.close();

    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

Recieving:

public Object readData() {
    try {
    InputStream socketStream = socket.getInputStream();
    ObjectInputStream objectInput = new ObjectInputStream(new GZIPInputStream(socketStream));
    Object a = objectInput.readObject();
      return a;
    } catch(Exception e) {
        return null;
    }
}

After I have recieved the String[] on the other end I want to be able to iterate through it like I would do normally so I can get the values. My current code doesn't seem to works as it returns null as the value.

Is this possible?

5
  • This seems like a great opportunity to learn about the Serialization API because this is the problem that it was designed to solve. Commented Jun 3, 2012 at 4:27
  • 2
    @RobertMassaioli - he is already using the Serialization API. His real problem is that his code is squashing exceptions. Commented Jun 3, 2012 at 4:38
  • There is no 'Array/List' here. You are sending an array of String. Commented Jun 3, 2012 at 5:28
  • @LeventDivilioglu The actual problem has nothing to do with networking at all. Commented Feb 28, 2016 at 2:37
  • @EJP at first I've tagged it with networking with a mistake but after then, tagged the quiestion with network-programming and I don't think it has nothing to do with network-programming Commented Feb 28, 2016 at 13:20

2 Answers 2

8

My current code doesn't seem to works as it returns null as the value.

And it is pretty obvious why too!

} catch(Exception e) {
    return null;
}

That says "if something goes wrong, return null and don't tell me what the problem was"!

Catching and squashing exceptions like that is BAD PRACTICE. At the very least, you should try to print an exception stacktrace. In the writer code, you do this:

System.out.println(e.toString());

That is better than nothing, but it just prints the exception name and message. You should really be printing the full stacktrace ... like this:

e.printStackTrace(System.out);

And for production quality code, you should probably be LOGGING the exception, not just writing a message to standard output.

Once you have the stack trace, you can figure out what the real problem is and fix it.

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

Comments

-2

I managed to figure it out on my own, I changed my recieving code to this:

public String[][] readData() {
    try {
    InputStream is = socket.getInputStream();
    ObjectInputStream ois = new ObjectInputStream(is);
     return (String[][])ois.readObject();
    } catch(Exception e) {
        return null;
    }
}

Works like a charm now. Thanks all!

3 Comments

But you've still got the horrible exception squashing code in there. Did you bother to read my answer at all? (IMO - this answer deserves to be down-voted because it is an example of BAD coding practice.)
I'm not a fan of this style either. First, you're completely masking the specific exception you're getting if readObject() fails. Next, you're not returning anything meaningful if readData() fails - it would be somewhat better design to raise some sort of exception that denotes its failure, and gives you a condition to recover from outside of the method.
Also, I noticed that you are apparently sending a String[] and trying to cast it to a String[][] at the receiving end. That is going to end badly ...

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.