• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading through socket problem

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I'm reading some data through socket like socket.readLine() .here the data is getting from flash client.The problem is the readline statement is hanging if the we didnt get the message from client side.I have to wait for 2 or 3 seconds and i have to skipp the readline statement if i didnt get the message from the client side.

Let me know if im not clear

Regards,
Bhagavan
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Socket does not have a readLine method; what exactly is the code doing?

Be sure to handle linebreaks properly: Don't println to a Socket
 
rajanikanth bhagavan kanth
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,we want to terminate the readline method after some interval when there is no data from client.
The code we are using is as follows :-
public void run() {
try {
this.socketIn = new BufferedReader(new InputStreamReader(
this.socket.getInputStream()));
this.socketOut = new PrintWriter(this.socket.getOutputStream(),
true);
conn = ConnectionHelper.getConnection();


String line = this.socketIn.readLine();
String tempString = GeneralConstants.EMPTY_STRING;
if (line != null)
tempString = line.trim();
String plr_info = null;
while (tempString != null
&& !GeneralConstants.EMPTY_STRING.equals(tempString.trim())) {
debug("client says" + tempString + "'");
String clientId = this.socket.getRemoteSocketAddress()
.toString();
if (tempString.startsWith(LoadProperties
.getPropertyByKey(GeneralConstants.SPLIT_TABLE_ID))) {
String returnTableStr = SplitGeneralStrings
.getTableDetailsByTblId(tempString, clientId);
write(returnTableStr);
debug("client says '" + returnTableStr + "'");
}
line = this.socketIn.readLine();
}

Kindly help us in resolving the issue ASAP
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's possible that readLine blocks if no terminated line is available, but the stream is not yet closed. If you were using raw streams you could check this with the "available" method.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what need to be done in this case:
1. the readline need to be run in a separate thread: reader thread
2. the main need to join on reader thread within a timeout readerThread.join (timeout). if there is nothing is read (use a flag in your reader thread to do this), throw an exception or some sort tell the caller.
3. close the socket. once the socket is closed, the reader thread will throw an exception. don't close the bufferedreder as the bufferedreader.readline has the lock which will prevent any thread from calling the close.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic