Send a few very small packets or smaller than large packets?

I am currently working on a simple application that transfers screenshots over sockets. I am getting a screenshot by instantiating and using the Robot class like this:

private Robot robot;
public Robot getRobot(){
    if(robot == null){
        try{
            robot = new Robot();
        }catch(Exception e){}
    }
    return robot;
}

public BufferedImage screenshot(){
    return getRobot().createScreenCapture(getScreenRectangle());
}

public byte[] getBytes(BufferedImage image){
    byte[] data = null;
    try{
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "PNG", baos);
        data = baos.toByteArray();
        baos.close();
    }catch(IOException e){}
    return data;
}

      

I then use the method getBytes

above to convert BufferedImage

to a byte array, which is then written to the socket's output stream. The image is 500 KB on average. It would be more efficient to split this 500KB into smaller chunks, say 5KB, or it would be better to store it in larger chunks, say 30KB. My main goal here is speed and accuracy of delivery. I would also appreciate any arguments as to why either of these methods would be more efficient in these terms than the other.

+3


source to share


2 answers


Network packet size is limited by what is called MTU , you cannot send a packet larger than mtu. Mtu, as you can check from the link, is not that much (I believe 1500 bytes is the more common MTU for Ethernet). This is for the network side; on the java side that determines the segment size, it can depend on the number of images being transmitted at the same time (if you send 100 images at a time, you have 100 segments).

My suggestion is to try with a segment size slightly less than the MTU (I assume you are using TCP / IP, so you should consider the size of the TCP and IP header) and see what happens.



Edit : some comments indicate (correctly) that form the java point of view, MTU does not affect how the data should be split; this is true for a packet larger than the MTU, since the TCP / IP layer splits large chunks of data into smaller units; but the poster wants to know if there is a "best" buffer size; and the answer is that compared to MTU, there is no benefit (for network transmission) when increasing the buffer size from the java side

+2


source


Send a few very small packets or smaller than large packets?

This is a very common question in applications where QoS plays an important role. I think there is no right answer, it is only continuity that adapts better to your requests.



Several aspects you can consider:

  • Large packages reduce% of data overhead.
  • Large packets have a greater impact when an error occurs while receiving or sending data (corrupted data).
  • Smaller packages should be used for the application that provides the best response to the user.
  • In some applications, where traffic is important and necessary for fast processing of information, instead of sending the entire image for each frame, the parts of the image must be changed using the appropriate protocols.
+1


source







All Articles