我正在接受来自客户端的连接,然后将该连接的套接字传递给另一个对象,但是,该套接字需要是非阻塞的。我正在尝试使用getChannel().configureBlocking(false)
,但这似乎不起作用。它需要是非阻塞的,因为下面的方法每 100 毫秒调用一次。还有其他方法可以让我做到这一点吗?谢谢你的帮助!
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");
}
}