简体   繁体   中英

InputStream Non Blocking read operation

I have the scenario where i need to confirm that either the server side socket closed the connection or not.So for this i am checking if(in.read() ==-1) if true,it means that server side closed the connection. But problem is that if it does not it blocks here as it is blocking call.i need such a solution where i can read non blocking in perticular time or cancel the reading if server end closed the connection. i am creating like

socket = new Socket();
                 // if server is not available 3 seconds blocking call other wise Exception
                 socket.connect(new InetSocketAddress(serverAddress , serverPort),3000);

Any help would be appreciated.

Regards, Aamir

But problem is that if it does not it blocks here as it is blocking call.

That's not the only problem. There is a miuch bigger problem: it will throw away a byte of data if any data ever arrives. You certainly cannot do that.

What you need is a timed read. See Socket.setSoTimeout() .

Assuming this is for TCP, have you tried using myClientSocket.isClosed() ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM