简体   繁体   中英

Java: Writing to socket with DataOutputStream slows down, then stops

while (true) {
    loc = getLocationOnScreen();

    out.writeLong((long)loc.getX());
    out.writeLong((long)loc.getY());
    out.flush();

    i++;
    System.out.println(i);

}

This code will merrily go along for about 3 seconds, before it slows, then stops.

i ends at about 16491 , never to continue increasing. What's going on here?

It is slowing down because , at the receiving end you might be doing long task using the value sent by you , before reading the next value. So the rate at which the other side is reading the data is smaller than the rate at which you are sending data to that. This keeps filling the receiving buffer at the other side and sending buffer at sender side. Eventually when both buffers are filled completely the transmission of data is halted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM