本文整理汇总了Java中java.nio.channels.ServerSocketChannel类的典型用法代码示例。如果您正苦于以下问题:Java ServerSocketChannel类的具体用法?Java ServerSocketChannel怎么用?Java ServerSocketChannel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ServerSocketChannel类属于java.nio.channels包,在下文中一共展示了ServerSocketChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bind
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
protected void
bind(
ServerSocketChannel ssc,
InetAddress address,
int port )
throws IOException
{
if ( address == null ){
ssc.socket().bind( new InetSocketAddress( port ), 1024 );
}else{
ssc.socket().bind( new InetSocketAddress( address, port ), 1024 );
}
}
示例2: OpenSockets
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
private synchronized void OpenSockets() {
if (mode == CLIENT) {
try
{
attemptClientConnection();
receiving = true;
} catch (IOException e) {
log.error(e);
}
} else if (mode == SERVER) {
try {
selector = Selector.open();
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
receiving = true;
} catch (IOException ex) {
selector = null;
}
}
}
示例3: acceptConnection
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
private final void acceptConnection(final SelectionKey key, MMOConnection<T> con)
{
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc;
try
{
while ((sc = ssc.accept()) != null)
{
if (_acceptFilter == null || _acceptFilter.accept(sc))
{
sc.configureBlocking(false);
SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<T>(this, sc.socket(), clientKey);
con.setClient(_clientFactory.create(con));
clientKey.attach(con);
}
else
sc.socket().close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
示例4: bindNIOServerSocket
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
/**
* Creates a NIO ServerSocketChannel, and gets the ServerSocket from
* there. Then binds the obtained socket.
* This fails on Windows with Oracle JDK1.6.0u33, if the passed InetAddress is a
* IPv6 address. Works on Oracle JDK 1.7.
*/
private void bindNIOServerSocket(InetAddress inetAddr) throws IOException {
while (true) {
int port = HBaseTestingUtility.randomFreePort();
InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
ServerSocketChannel channel = null;
ServerSocket serverSocket = null;
try {
channel = ServerSocketChannel.open();
serverSocket = channel.socket();
serverSocket.bind(addr); // This does not work
break;
} catch (BindException ex) {
//continue
} finally {
if (serverSocket != null) {
serverSocket.close();
}
if (channel != null) {
channel.close();
}
}
}
}
示例5: accept
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
protected NioSession accept(IoProcessor<NioSession> processor, ServerSocketChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if ((key == null) || (!key.isValid()) || (!key.isAcceptable())) {
return null;
}
// accept the connection from the client
SocketChannel ch = handle.accept();
if (ch == null) {
return null;
}
return new NioSocketSession(this, processor, ch);
}
示例6: configure
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
@Override
public void configure(InetSocketAddress addr, int maxcc) throws IOException {
configureSaslLogin();
/**
* thread是ServerCnxnFactory的主线程先启动;然后再启动NIO服务器
* @see ServerCnxnFactory#run也已经被执行了
* 此时客户端可以访问zk的2181端口;但是还无法处理客户端的请求
*/
thread = new ZooKeeperThread(this, "NIOServerCxn.Factory:" + addr);
//线程后台运行
thread.setDaemon(true);
maxClientCnxns = maxcc;
//??
this.ss = ServerSocketChannel.open();
//??
ss.socket().setReuseAddress(true);
LOG.info("binding to port " + addr);
ss.socket().bind(addr);
//非阻塞
ss.configureBlocking(false);
//??
ss.register(selector, SelectionKey.OP_ACCEPT);
}
示例7: TunnelServer
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
public TunnelServer(int port, Selector selector) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
// ServerSocketChannel.bind() requires API 24
serverSocketChannel.socket().bind(new InetSocketAddress(Inet4Address.getLoopbackAddress(), port));
SelectionHandler socketChannelHandler = (selectionKey) -> {
try {
ServerSocketChannel channel = (ServerSocketChannel) selectionKey.channel();
acceptClient(selector, channel);
} catch (IOException e) {
Log.e(TAG, "Cannot accept client", e);
}
};
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT, socketChannelHandler);
}
示例8: selectFailure
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
public void
selectFailure(
VirtualAbstractSelectorListener listener,
AbstractSelectableChannel sc,
Object attachment,
Throwable msg)
{
if ( op == OP_ACCEPT ){
((VirtualAcceptSelectorListener)listener).selectFailure( VirtualChannelSelector.this, (ServerSocketChannel)sc, attachment, msg );
}else{
((VirtualSelectorListener)listener).selectFailure( VirtualChannelSelector.this, (SocketChannel)sc, attachment, msg );
}
}
示例9: main
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
public static void main(String args[]) throws Exception {
InetAddress iaddr = InetAddress.getLocalHost();
try ( ServerSocket ss = new ServerSocket(0);
Socket s1 = new Socket(iaddr, ss.getLocalPort());
Socket s2 = ss.accept() ) {
test(s1, s2, "Testing NET");
}
// check the NIO socket adapter
try (ServerSocketChannel sc = ServerSocketChannel.open().bind(null);
SocketChannel s1 = SocketChannel.open(
new InetSocketAddress(iaddr, sc.socket().getLocalPort()));
SocketChannel s2 = sc.accept() ) {
test(s1.socket(), s2.socket(), "Testing NIO");
}
if (failed) {
throw new RuntimeException("Failed: check output");
}
}
示例10: run
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
public void run() {
try {
java.nio.channels.Selector acceptSelector = java.nio.channels.Selector.open();
serverSocketChannel.register(acceptSelector, SelectionKey.OP_ACCEPT);
while (serverSocketChannel.isOpen()) {
if (acceptSelector.select(1000) > 0) {
Iterator<SelectionKey> it = acceptSelector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
if (key.isAcceptable()) {
SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();
socketChannel.configureBlocking(false);
newChannels.add(socketChannel);
selector.wakeup();
}
it.remove();
}
}
}
} catch (IOException e) {
// ignore
}
}
示例11: openServerSocket
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
public final void openServerSocket(InetAddress address, int tcpPort) throws IOException
{
final ServerSocketChannel selectable = ServerSocketChannel.open();
selectable.configureBlocking(false);
final ServerSocket ss = selectable.socket();
if (address != null)
{
ss.bind(new InetSocketAddress(address, tcpPort));
}
else
{
ss.bind(new InetSocketAddress(tcpPort));
}
selectable.register(_selector, SelectionKey.OP_ACCEPT);
}
示例12: main
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
boolean keepAlive = false;
String prop = System.getProperty("com.sun.CORBA.transport.enableTcpKeepAlive");
if (prop != null)
keepAlive = !"false".equalsIgnoreCase(prop);
DefaultSocketFactoryImpl sfImpl = new DefaultSocketFactoryImpl();
ORBImpl orb = new ORBImpl();
orb.set_parameters(null);
sfImpl.setORB(orb);
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetSocketAddress isa = new InetSocketAddress("localhost", ssc.socket().getLocalPort());
Socket s = sfImpl.createSocket("ignore", isa);
System.out.println("Received factory socket" + s);
if (keepAlive != s.getKeepAlive())
throw new RuntimeException("KeepAlive value not honoured in CORBA socket");
}
示例13: openSocketForTransferPort
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
private void openSocketForTransferPort(int port) throws IOException {
executor = Utils.getStandardExecService("[ Acceptable ServersThreadPool ] ",
2,
10,
new ArrayBlockingQueue<Runnable>(65500),
Thread.NORM_PRIORITY - 2);
ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
FDTSession sess = FDTSessionManager.getInstance().getSession(sessionID);
ss = ssc.socket();
String listenIP = config.getListenAddress();
if (listenIP == null) {
ss.bind(new InetSocketAddress(port));
}
else
{
ss.bind(new InetSocketAddress(InetAddress.getByName(listenIP), port));
}
sel = Selector.open();
ssc.register(sel, SelectionKey.OP_ACCEPT);
sc = ssc.accept();
config.setSessionSocket(ssc, ss, sc, s, port);
}
示例14: main
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
// 创建ServerSocketChannel,监听8080端口
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(8080));
// 设置为非阻塞模式
ssc.configureBlocking(false);
// 为ssc注册选择器
Selector selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
// 创建处理器
Handler handler = new Handler(1024);
while (true) {
// 等待请求,每次等待阻塞3s,超过3s后线程继续向下运行,如果传入0或者不传参数将一直阻塞
if (selector.select(3000) == 0) {
System.out.println("等待请求超时……");
continue;
}
System.out.println("处理请求……");
// 获取待处理的SelectionKey
Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator();
while (keyIter.hasNext()) {
SelectionKey key = keyIter.next();
try {
// 接收到连接请求时
if (key.isAcceptable()) {
handler.handleAccept(key);
}
// 读数据
if (key.isReadable()) {
handler.handleRead(key);
}
} catch (IOException ex) {
keyIter.remove();
continue;
}
// 处理完后,从待处理的SelectionKey迭代器中移除当前所使用的key
keyIter.remove();
}
}
}
示例15: main
import java.nio.channels.ServerSocketChannel; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
// 创建ServerSocketChannel,监听8080端口
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(8080));
// 设置为非阻塞模式
ssc.configureBlocking(false);
// 为ssc注册选择器
Selector selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
// 创建处理器
while (true) {
// 等待请求,每次等待阻塞3s,超过3s后线程继续向下运行,
// 如果传入0或者不传参数将一直阻塞
if (selector.select(3000) == 0) {
continue;
}
// 获取待处理的SelectionKey
Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator();
while (keyIter.hasNext()) {
SelectionKey key = keyIter.next();
// 启动新线程处理SelectionKey
new Thread(new HttpHandler(key)).run();
// 处理完后,从待处理的SelectionKey迭代器中移除当前所使用的key
keyIter.remove();
}
}
}