How to make an accepted socket non-blocking in jav

2020-03-03 09:37发布

I'm 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");
  }
}

5条回答
成全新的幸福
2楼-- · 2020-03-03 09:39

Two things:

  1. Why aren't you using a ServerSocket if you're listening for connections?
  2. If you want to accept multiple clients you want to use a loop.

The basic structure of a multi-client server is:

while (true) {
  // accept connections
  // spawn thread to deal with that connection
}

If the issue is blocking on the accept() call, well that's what accept() does: it blocks waiting for a connection. If that's an issue I suggest you have a separate thread to accept connections.

See Writing the Server Side of a Socket.

查看更多
何必那么认真
3楼-- · 2020-03-03 09:43

I would expect your code to block on the accept call, never getting to the configureBlocking call.

I typically spin off a separate thread for each socket connection, and let it block until a connection is actually made/accepted This allows the main thread to continue unblocked while it is waiting for client connections.

查看更多
家丑人穷心不美
4楼-- · 2020-03-03 09:46

If the typical blocking socket doesn't give you the availability you need (a connection every 100ms does seem tight). You should look at a non-blocking socket. Here is a tutorial. You can also look at Apache MINA to make this easier.

查看更多
唯我独甜
5楼-- · 2020-03-03 09:50

If you're looking for non-blocking sokets, my suggestion is to use Selectors and ServerSocketChannels with the NIO package.

http://java.sun.com/j2se/1.4.2/docs/guide/nio/

查看更多
Summer. ? 凉城
6楼-- · 2020-03-03 09:52

One approach is to use an I/O loop (event loop) in a single threaded environment. Take a look at Deft web server for inspiration. (Especially the start() method in IOLoop)

查看更多
登录 后发表回答