基于非套接字的Java Server [英] Non socket based Java Server
问题描述
踢它老学校:我正在使用Java SE 5(或java v1.5)(请不要告诉我升级,因为我正在处理[私有]我需要使用这个版本的java)。
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).
我需要帮助设置Web客户端/服务器应用程序。我在网上寻找的每个变化都缩小到使用websockets / socket,但是我使用的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?
编辑:
好的,显然我的java版本确实有套接字。我会再研究一下。但是,现在我只是好奇,如何创建一个不使用套接字的java服务器?
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?
推荐答案
你需要套接字。 它们是连接的终点。虽然,您可以使用阻止 Socket
和 ServerSocket
实现的替代方法。
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(新IO)使用频道。它允许非阻塞I / O:
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)
您可以使用选择器
在读/写/接受之间切换,以便仅在没有任何操作时阻止阻塞(以消除多余的CPU使用)
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
支持通过 isAcceptable()
接受,通过读取isReadable()
并通过写入isWritable()
,因此您可以在同一个线程上处理所有3个。
SelectionKey
supports accepting via isAcceptable()
, reading via isReadable()
and writing via isWritable()
, so you can handle all 3 on the same thread.
这是使用NIO接受连接的基本示例。我强烈建议再深入研究一下,以便更好地了解事情的运作方式。
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.
这篇关于基于非套接字的Java Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!