Java
Chilkat2 Java
Poll a Socket for Available Data
See more Socket/SSL/TLS Examples
Demonstrates Socket.PollDataAvailable, which performs a nonblocking check for readable activity and returns whether data or another read-ready condition is present.
Background. This lets an application check for incoming data without blocking, for example in an event loop that services multiple tasks.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Socket;
public class ChilkatExample {
public static void main(String[] args) {
Socket socket = new Socket();
// Connect to the server using TLS.
boolean bTls = true;
int maxWaitMs = 5000;
try {
socket.connect("example.com", 5000, bTls, maxWaitMs);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Perform a nonblocking check for readable data. Returns true when data or another read-ready
// condition is present, and false when nothing is immediately available.
boolean bAvailable = socket.pollDataAvailable();
if (!bAvailable) {
System.out.println("No data is immediately available.");
} else {
System.out.println("Data is available to read.");
}
}
}