当前位置: 首页>>代码示例>>Java>>正文


Java ServerSocketChannel类代码示例

本文整理汇总了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 );
	}
}
 
开发者ID:BiglySoftware,项目名称:BiglyBT,代码行数:18,代码来源:NetworkAdminImpl.java

示例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;
		}

	}
}
 
开发者ID:Esri,项目名称:defense-solutions-proofs-of-concept,代码行数:25,代码来源:TcpSquirtOutboundTransport.java

示例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();
    }
}
 
开发者ID:L2jBrasil,项目名称:L2jBrasil,代码行数:27,代码来源:SelectorThread.java

示例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();
      }
    }  
  }
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:30,代码来源:TestIPv6NIOServerSocketChannel.java

示例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);
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:22,代码来源:NioSocketAcceptor.java

示例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);
}
 
开发者ID:maoling,项目名称:fuck_zookeeper,代码行数:24,代码来源:NIOServerCnxnFactory.java

示例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);
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:17,代码来源:TunnelServer.java

示例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 );

  }
 }
 
开发者ID:BiglySoftware,项目名称:BiglyBT,代码行数:17,代码来源:VirtualChannelSelector.java

示例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");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:24,代码来源:ShutdownInput.java

示例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
    }
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:24,代码来源:NioEchoServer.java

示例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);
}
 
开发者ID:rubenswagner,项目名称:L2J-Global,代码行数:19,代码来源:SelectorThread.java

示例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");
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:KeepAliveSockets.java

示例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);
    }
 
开发者ID:fast-data-transfer,项目名称:fdt,代码行数:26,代码来源:FDTSession.java

示例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();
            }
        }
    }
 
开发者ID:tomoncle,项目名称:JavaStudy,代码行数:41,代码来源:NIOSocketTest.java

示例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();
            }
        }
    }
 
开发者ID:tomoncle,项目名称:JavaStudy,代码行数:28,代码来源:HttpTest.java


注:本文中的java.nio.channels.ServerSocketChannel类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。