Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading through socket problem  RSS feed

 
rajanikanth bhagavan kanth
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42970
73
  • Mark post as helpful
  • send pies
  • 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.
 
luri ron
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!