How to send Json object through java sockets?
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());}