aspose file tools
The moose likes Sockets and Internet Protocols and the fly likes Reading through socket problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Reading through socket problem" Watch "Reading through socket problem" New topic
Author

Reading through socket problem

rajanikanth bhagavan kanth
Ranch Hand

Joined: May 20, 2008
Posts: 78
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
Marshal

Joined: Mar 22, 2005
Posts: 35254
    
    7
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


Android appsImageJ pluginsJava web charts
rajanikanth bhagavan kanth
Ranch Hand

Joined: May 20, 2008
Posts: 78
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
Marshal

Joined: Mar 22, 2005
Posts: 35254
    
    7
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

Joined: Dec 11, 2008
Posts: 86
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Reading through socket problem
 
Similar Threads
ServerSocket load issue?
Brainy Upload Bean.
client hangs after getting data from server
Socket Programing using TCP Protocol
BufferedReader problem