5

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");
  }
}
Feedforward
  • 4,047
  • 4
  • 20
  • 30
Benaiah
  • 51
  • 1
  • 1
  • 2
  • This question is answered in this thread https://stackoverflow.com/questions/15541804/creating-the-serversocket-in-a-separate-thread. The answer is to put the server socket in its own thread. – John Deverall Jan 24 '19 at 22:59

5 Answers5

10

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.

bubakazouba
  • 1,500
  • 2
  • 19
  • 32
cletus
  • 578,732
  • 155
  • 890
  • 933
  • this does not answer the question, but that is in most cases the way to go - if you really want non blocking IO go with the nio package mentioned in the other answers (but that also means no streams!) – rurouni Oct 30 '13 at 20:52
  • @rurouni There *isnt* an answer, as you can't do it without shifting over to NIO. – user207421 Dec 29 '17 at 22:00
1

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.

simon
  • 5,288
  • 7
  • 28
  • 35
1

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/

Seth
  • 5,156
  • 6
  • 38
  • 53
0

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.

Yishai
  • 84,976
  • 26
  • 176
  • 250
0

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)

reevesy
  • 3,354
  • 1
  • 24
  • 22
Schildmeijer
  • 19,921
  • 11
  • 59
  • 79