Non socket based Java Server [closed]

2019-01-08 01:40发布

Kicking it old school: I'm using Java SE 5 (or java v1.5) (please don't tell me to upgrade, because for what I'm working on [which is private] I need to use this version of java).

I need help setting up a web client/server application. Every variation I've looked for online has narrowed down to using websockets/sockets, however the version of java I'm using does not have sockets yet.

Does anyone have a tutorial for setting up a client/server model application not using sockets, or some sample code that can point me in the correct direction?

EDIT: Okay, so apparently my version of java does have sockets. I'll look into that more. However, now I'm just curious, how does one create a java server not using sockets?

2条回答
Fickle 薄情
2楼-- · 2019-01-08 01:58

You need sockets. They serve as the end-points to the connection. Although, you can use alternatives to the blocking Socket and ServerSocket implementations.

NIO (New IO) uses channels. It allows for non-blocking I/O:

class Server {
     public static void main(String[] args) throws Exception {
          ServerSocketChannel ssc = ServerSocketChannel.open();
          ssc.configureBlocking(false);

          while(true) {
               SocketChannel sc = ssc.accept();

               if(sc != null) {
                    //handle channel
               }
          }
     }
}

Using a Selector (optional, but recommended)

You could use a Selector to switch between reading/writing/accepting to allow blocking only when theres nothing to do (to remove excess CPU usage)

class Server {
     private static Selector selector;
     public static void main(String[] args) throws Exception {
          ServerSocketChannel ssc = ServerSocketChannel.open();
          ssc.configureBlocking(false);
          Selector selector = Selector.open();
          ssc.register(selector, SelectionKey.OP_ACCEPT);

          while(true) {
               selector.select();

               while(selector.iterator().hasNext()) {
                    SelectionKey key = selector.iterator().next();

                    if(key.isAcceptable()) {
                         accept(key);
                    }
               }
          }
     }

     private static void accept(SelectionKey key) throws Exception {
          ServerSocketChannel channel = (ServerSocketChannel) key.channel();
          SocketChannel sc = channel.accept();
          sc.register(selector, OP_READ);
     }
}

SelectionKey supports accepting via isAcceptable(), reading via isReadable() and writing via isWritable(), so you can handle all 3 on the same thread.

This is a basic example of accepting a connection using NIO. I highly suggest looking into it a bit more to get a better understanding of how things work.

查看更多
迷人小祖宗
3楼-- · 2019-01-08 02:12

Here's the API documentation of the class Socket from Java 5.

Sockets are such a basic abstraction of network communication that you'd be hard pushed to find an OS which doesn't support them, let alone a high level programming language.

查看更多
登录 后发表回答