javasocketssslsslsocketfactory

Data packet, when sent over my SSL Socket becomes extremely large


I have recently tried my hands over Socket Programming by creating an SSL Socket used to stream data live from a server with no success of course. When I analyze the data packets through Wireshark, I realize the size of request data has been magnified n number of times in the packet and hence the request reaches the server in fragments where as the actual JSON request is a handful of bytes and should reach the server in a single shot.

Any help would be appreciated.


Solution

  • Wrap a BufferedOutputStream around the SSLSocket's output stream, and don't flush it until you really gotta, which is usually not until you're about to read the reply. Otherwise you can be sending one byte at a time to the SSLSocket, which becomes one SSL message per byte, which can expand the data by more than 40x.

    However:

    the request reaches the server in fragments

    That can happen any time. The server has to be able to cope with receiving data as badly fragmented as one byte at a time.

    where as the actual JSON request is a handful of bytes and should reach the server in a single shot.

    There is no such guarantee in TCP.