简体   繁体   English

如何在java中使接受的套接字无阻塞

[英]How to make an accepted socket non-blocking in java

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. 我正在尝试使用getChannel().configureBlocking(false)但这似乎不起作用。 It needs to be non-blocking because this the method below is called every 100ms. 它需要是非阻塞的,因为下面的方法每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");
  }
}

Two things: 两件事情:

  1. Why aren't you using a ServerSocket if you're listening for connections? 如果您正在侦听连接,为什么不使用ServerSocket
  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. 如果问题是在accept()调用上阻塞,那么这就是accept()所做的:它会阻塞等待连接。 If that's an issue I suggest you have a separate thread to accept connections. 如果这是一个问题,我建议你有一个单独的线程来接受连接。

See Writing the Server Side of a Socket . 请参阅编写套接字的服务器端

I would expect your code to block on the accept call, never getting to the configureBlocking call. 我希望你的代码阻止接受调用,永远不会进入configureBlocking调用。

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. 我通常为每个套接字连接分离一个单独的线程,并让它阻塞,直到实际建立/接受连接。这允许主线程在等待客户端连接时继续解除阻塞。

If you're looking for non-blocking sokets, my suggestion is to use Selectors and ServerSocketChannels with the NIO package. 如果你正在寻找非阻塞的sokets,我的建议是使用选择器和ServerSocketChannels与NIO包。

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

If the typical blocking socket doesn't give you the availability you need (a connection every 100ms does seem tight). 如果典型的阻塞套接字没有为您提供所需的可用性(每100毫秒的连接似乎很紧张)。 You should look at a non-blocking socket. 你应该看一个非阻塞套接字。 Here is a tutorial . 这是一个教程 You can also look at Apache MINA to make this easier. 您还可以查看Apache MINA简化这一过程。

One approach is to use an I/O loop (event loop) in a single threaded environment. 一种方法是在单线程环境中使用I / O循环(事件循环)。 Take a look at Deft web server for inspiration. 看看Deft网络服务器的灵感。 (Especially the start() method in IOLoop ) (尤其是IOLoop中的start()方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM