10

How do you send Json object's through sockets preferable through ObjectOutputStream class in java this is what I got so far

    s = new Socket("192.168.0.100", 7777);
    ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
    JSONObject object = new JSONObject();
    object.put("type", "CONNECT");
    out.writeObject(object);

But this gives an java.io.streamcorruptedexception exception any suggestions?

4
  • 1
    Don't you think it would be relevant which exception you get? Commented Feb 22, 2014 at 11:56
  • 1
    @FelixKling sorry forget that updated the question Commented Feb 22, 2014 at 11:57
  • 2
    Why use JSON if you use native Java serialization. Why not send a Java object directly? And which JSONOnject class are you talking about? Commented Feb 22, 2014 at 12:09
  • This code does not throw that exception. You can only get StreamCorruptedException from the reading code, which you haven't shown. Commented Apr 29, 2022 at 6:33

2 Answers 2

18

Instead of using ObjectOutputStream, you should create an OutputStreamWriter, then use that to write the JSON text to the stream. You need to choose an encoding - I would suggest UTF-8. So for example:

JSONObject json = new JSONObject();
json.put("type", "CONNECT");
Socket s = new Socket("192.168.0.100", 7777);
try (OutputStreamWriter out = new OutputStreamWriter(
        s.getOutputStream(), StandardCharsets.UTF_8)) {
    out.write(json.toString());
}
Sign up to request clarification or add additional context in comments.

21 Comments

Thanks for the pointer to StandardCharsets; I'd not run across the constants (which, IMHO, are in a stupid location).
@HotLicks: Interesting - where's that defined, just for my future reference?
@HotLicks: that doesn't mean that these unicode code points must be encoded as UTF8. UTF8 is one of many ways to transform unicode code points to bytes. You could also use UTF16 or UTF32 for example. Isn't the encoding a property of the transport protocol (HTTP, most of the time)?
@Daniel: Why would you want to wrap a nice portable format (JSON) in a Java-specific binary serialization format?
@TomStambaugh: The documentation says to consider doing that, not that one should do that. In this case, there's only a single call to write, so the motivation of avoiding "frequent converter invocations" is irrelevant.
|
-2

An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconstituted on another host or in another process.

A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in." You don't need to write primitive data types in binary, so do not use DataOutputStream

An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

You need to create an OutputStreamWriter for sending json objects to the client

OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());
JSONObject obj = new JSONObject();
obj.put("name", "harris");
obj.put("age", 23);
out.write(obj.toString());

You can read the output data from the client using InputStreamReader by this way

InputStreamReader input = new InputStreamReader(socket.getInputStream());
int data = input.read();
while (data != -1) {
    System.out.print((char)data);
    data = input.read();
}

Comments

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.