File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes while(1) reading from socket interruption Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "while(1) reading from socket interruption" Watch "while(1) reading from socket interruption" New topic
Author

while(1) reading from socket interruption

Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Hello, I have a dedicated thread to read input btes from a socket but, well, i'm doing that on a loop like this:



The point is that this loop increases my CPU performance very bad. Are there some manner to interrupt the loop and read the bytes only when they're comming?


Thanks in advance
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

When there are no bytes available from the socket's input stream, then your code will block (i.e. stop and wait) at line 17 of your posted code. While it is blocked it will consume basically no CPU time. So if you observe that your code is using a lot of CPU time, that suggests that line 21 is to blame.

Or have you noticed your "erro ao ler do socket" error message being output in large quantities?
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Paul Clapham wrote:When there are no bytes available from the socket's input stream, then your code will block (i.e. stop and wait) at line 17 of your posted code. While it is blocked it will consume basically no CPU time. So if you observe that your code is using a lot of CPU time, that suggests that line 21 is to blame.

Or have you noticed your "erro ao ler do socket" error message being output in large quantities?


No its all ok that message (that by the wayit is Portuguese and means: error reading from socket). Well i'm reading from socket 3 or 4 times per second (depending on the number of times the socket has data to read). The line "A bunch of things" represents the way treat the recived message, update graphics and so on. I talk to one of my professors and he said: if you are receiving data from the socket you have to consume that, i would do the same way you are doing. Wich means that i have to be always reading. Meanwhile, in a second, the loop runs hundreds of times and i only need to run it when i have bytes to read. But, if you are sure you are right that the line 17 block the code so i'm really messed up. because my app is putting my CPU 20% higher or more... :-/////
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

It sounds like you need to time your code to see how long various parts of it take. The System.nanoTime() and System.currentTimeMillis() methods are useful for that.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Pedro Neves wrote:But, if you are sure you are right that the line 17 block the code...


That's what the API documentation for DataInputStream says. It also says what will happen if your "dyn_data" byte array is of length zero -- you might want to check out that possibility.
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Paul Clapham wrote:
Pedro Neves wrote:But, if you are sure you are right that the line 17 block the code...


That's what the API documentation for DataInputStream says. It also says what will happen if your "dyn_data" byte array is of length zero -- you might want to check out that possibility.



Well, correct me if i'm wrong but i did understand that if there are no bytes ("dyn_data.lenght" == 0) the function do not block. Am i right?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Pedro Neves wrote:Well, correct me if i'm wrong but i did understand that if there are no bytes ("dyn_data.lenght" == 0) the function do not block. Am i right?


Yes, that's right. You didn't actually do that, did you?
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Paul Clapham wrote:
Pedro Neves wrote:Well, correct me if i'm wrong but i did understand that if there are no bytes ("dyn_data.lenght" == 0) the function do not block. Am i right?


Yes, that's right. You didn't actually do that, did you?


No. Well my App is opening a TCP connection to another App (which i dont have the source code) that sends data to my app. As far as i know when it sends data to my app it is more than a byte. I'm recieving the data as you saw, no more precautions were taken
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while(1) reading from socket interruption