Error sending JSON by Socket

Asked

Viewed 202 times

3

When the JSON string is too large, there is loss of part of the string in the upload. I am sending like this:

private void btComunicarActionPerformed(java.awt.event.ActionEvent evt) {                                            
        List<PessoaMOD> pessoas = new ArrayList<PessoaMOD>();

        for (int i = 1; i <= 2000; i++) {
            pessoas.add(new PessoaMOD(i, "Pessoa " + i));
        }

        try {
            Socket cliente = new Socket("127.0.0.1", 12345);
            enviarMensagem(codificarListarDiretorio(pessoas), cliente);
        } catch (Exception e) {
            System.out.println("Erro: " + e.getMessage());
        } finally {
        }
    }  

public ByteArrayOutputStream codificarListarDiretorio(List<PessoaMOD> pessoas) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);

        Gson gson = new GsonBuilder().create();
        dos.write(gson.toJson(pessoas).getBytes());
        return bos;
    }

public void enviarMensagem(ByteArrayOutputStream mensagem, Socket socket) throws IOException {
        byte[] msg = mensagem.toByteArray();
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.writeInt(msg.length); //O tamanho da mensagem
        out.write(msg); //Os dados
        out.flush();
    }

and get on the server like this:

int tamanhoMsg = entrada.readInt();                   
byte[] bufferJson = new byte[tamanhoMsg];
entrada.read(bufferJson, 0, bufferJson.length);
String json = new String(bufferJson);

Only the Json String doesn’t come complete when it’s too big.

What happens is that the number of bytes is greater than the lenght support, so the message size does not send full.

I also tried sending by the method writeUTF();

But as the String is large, it generates this error: encoded string Too long: 677789 bytes

  • how is instantiated out ?

  • not "out", is "dos", had put wrong. You would have a light to give me in this problem? rsrs

1 answer

1

Tries to change the JSON reading part of your code on the DataInputStream for BufferedReader:

// ...
long tamanhoMsg = entrada.readLong();

BufferedReader reader = new BufferedReader(
        new InputStreamReader(
            // guava library - limita a leitura de um input stream
            ByteStreams.limit(        
                inputStream,                // input stream do socket
                tamanhoMsg                  // tamanho máximo a ser lido do input stream
            ),
        )
    );

JsonObject data = JsonParser.parse(reader)  // le o reader convertendo para  json
        getAsJsonObject();                  // retorna como JsonObject

// ...

You could also change the message size information from int to long.

  • So the problem is that the SG size variable is not coming right, because on the client I do the following to get the size: "msg.length". Lenght is int, and as the String is large, it is bursting the maximum integer size, and thus, the correct size is not coming.

  • Then try to change the delimitation of a message by the JSON protocol itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.