My conclusion to the problem I'm having, which follows, is that InputStream�s read() method blocks for a lot longer than it has to when its waiting for something to read. So if that sounds right I guess you don�t have to read what follows, and hopefully you can tell me how to shorten this time or suggest a workaround. As in readAttempt � nothing to read � wait less time than apparent default before trying to read again.
I have a server and client and establish a socket between them. A thread of the server has a timer that writes to the clients socket with each tick. The client should then read this new data and move objects on an image accordingly. My problem is that the clients method to read this data is not reading it when its sent, although it all does get read and in the right order, and because of this the movement of the objects is very choppy. I�ll just post the two simplified relevant methods:
The first method is from server, the second from client. When run, the server prints, as expected(well close to): TICK! 1212444642057 TICK! 40 TICK! 40 TICK! 40 TICK! 40 TICK! 71 TICK! 20 TICK! 60 TICK! 60 TICK! 40 TICK! 40 TICK! 70 TICK! 40 TICK! 40 TICK! 40 Etc.
But the client prints this: 1212446081477 0 0 180 0 0 0 0 200 0 0 0 201 0 0 Etc.
Not at all what I expected. When I uncomment the �sleep(50);� I have commented out, I get: 1212446254976 50 51 50 50 50 50 50 50 50 50 50 50 50 50
Which is what we want to see. But I certainly don�t want to limit the code by forcing a sleep, I want it to function as soon as its possible for it to. Plus, with a sleep if even once it misses reading a taick when it should it will never catch up.
I also tried:
In clients code, but this produced the same exact output as the original.