Android non blocking socket example

Android non blocking socket example

0 280

I am accepting a connection from a client and then passing that connected socket off to another object, however, that socket needs to be non-blocking. I’m trying to use getChannel().configureBlocking(false) but that does not seem to be working. It needs to be non-blocking because this the method below is called every 100ms. Is there some other way that I should be making this non-blocking? Thanks for any help!

public void checkForClients() {
DataOutputStream out;
DataInputStream in;
Socket connection;
InetAddress tempIP;
String IP;

try {

connection = serverSocket.accept();
connection.getChannel().configureBlocking(false);

System.err.println(“after connection made”);

in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(connection.getOutputStream());
tempIP = connection.getInetAddress();
IP = tempIP.toString();

System.err.println(“after ip string”);

// create a new user ex nihilo
connectedUsers.add(new ConnectedUser(IP, null, connection, in, out));

System.err.println(“after add user”);
} catch (SocketTimeoutException e) {
System.err.println(“accept timeout – continuing execution”);
} catch (IOException e) {
System.err.println(“socket accept failed”);
}

NO COMMENTS

Leave a Reply