java - Blocking read from an input TCP socket -


i working on project distributed systems course have implement small sensor network in java. sensors send temperature every x seconds admin node (that sensor), records them. upon request, user node connects admin node , receives mean of detected temperatures. 1 of issues admin node can "fail". before failing sends message user node, needs send special packet 1 of other regular nodes become new admin. user node communicates admin tcp connection trough sends "request" packet , receives packet containing temperature. using java.net.socket implement this.

the problem comes when admin fails. in normal condition, create new thread waits "fail" message server , creates new socket newly selected admin node. but, if admin fails before sends message in response user request, user node indefinitely waiting, because reading input stream.

in particular, using following lines create socket , input stream:

   this.socket = new socket(this.serveraddress,this.serverport);    this.in = new datainputstream(socket.getinputstream()); 

and following line in while loop read response temperature.

   string data = in.readutf(); 

how suggest handle situation?

also, thinking use same tcp connection receiving "fail" message admin node. how can support this?

just set read timeout via socket.setsotimeout().


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -