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


Java ServerSocketChannel.open方法代码示例

本文整理汇总了Java中java.nio.channels.ServerSocketChannel.open方法的典型用法代码示例。如果您正苦于以下问题:Java ServerSocketChannel.open方法的具体用法?Java ServerSocketChannel.open怎么用?Java ServerSocketChannel.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.ServerSocketChannel的用法示例。


在下文中一共展示了ServerSocketChannel.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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

示例2: 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

示例3: call

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public ByteBuffer call() throws Exception {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    buff = ByteBuffer.allocate(bufferSize);
    serverSocketChannel.socket().bind(new InetSocketAddress(port));
    while (!stop.isLocked()) {
        RandomAccessFile temp = new RandomAccessFile(tempName, "rw");
        SocketChannel socketChannel = serverSocketChannel.accept();
        socketChannel.read(buff);
        FileChannel channel = temp.getChannel();
        channel.write(buff);
        if (!pause.isLocked()) {
            MappedByteBuffer b = channel.map(MapMode.READ_WRITE, 0, (long) bufferSize);
            b.clear();
        }
        temp.close();
        buff.clear();
    }

    return null;
}
 
开发者ID:EventHorizon27,项目名称:dataset-lib,代码行数:21,代码来源:StreamThread.java

示例4: NioEchoServer

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
public NioEchoServer(ListenerName listenerName, SecurityProtocol securityProtocol, AbstractConfig config,
        String serverHost, ChannelBuilder channelBuilder) throws Exception {
    super("echoserver");
    setDaemon(true);
    serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.configureBlocking(false);
    serverSocketChannel.socket().bind(new InetSocketAddress(serverHost, 0));
    this.port = serverSocketChannel.socket().getLocalPort();
    this.socketChannels = Collections.synchronizedList(new ArrayList<SocketChannel>());
    this.newChannels = Collections.synchronizedList(new ArrayList<SocketChannel>());
    this.credentialCache = new CredentialCache();
    if (securityProtocol == SecurityProtocol.SASL_PLAINTEXT || securityProtocol == SecurityProtocol.SASL_SSL)
        ScramCredentialUtils.createCache(credentialCache, ScramMechanism.mechanismNames());
    if (channelBuilder == null)
        channelBuilder = ChannelBuilders.serverChannelBuilder(listenerName, securityProtocol, config, credentialCache);
    this.selector = new Selector(5000, new Metrics(), new MockTime(), "MetricGroup", channelBuilder);
    acceptorThread = new AcceptorThread();
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:19,代码来源:NioEchoServer.java

示例5: bind

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
protected void bind() throws IOException {
    // allocate an unbound server socket channel
    serverChannel = ServerSocketChannel.open();
    // Get the associated ServerSocket to bind it with
    ServerSocket serverSocket = serverChannel.socket();
    // create a new Selector for use below
    synchronized (Selector.class) {
        // Selector.open() isn't thread safe
        // http://bugs.sun.com/view_bug.do?bug_id=6427854
        // Affects 1.6.0_29, fixed in 1.7.0_01
        this.selector.set(Selector.open());
    }
    // set the port the server channel will listen to
    //serverSocket.bind(new InetSocketAddress(getBind(), getTcpListenPort()));
    bind(serverSocket,getPort(),getAutoBind());
    // set non-blocking mode for the listening socket
    serverChannel.configureBlocking(false);
    // register the ServerSocketChannel with the Selector
    serverChannel.register(this.selector.get(), SelectionKey.OP_ACCEPT);

    //set up the datagram channel
    if (this.getUdpPort()>0) {
        datagramChannel = DatagramChannel.open();
        configureDatagraChannel();
        //bind to the address to avoid security checks
        bindUdp(datagramChannel.socket(),getUdpPort(),getAutoBind());
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:29,代码来源:NioReceiver.java

示例6: 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

示例7: prepareSocket

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
private void prepareSocket(int port) throws IOException {
	server = ServerSocketChannel.open();
	server.configureBlocking(false);
	server.socket().bind(new InetSocketAddress(port));

	selector = Selector.open();
	server.register(selector, SelectionKey.OP_ACCEPT);
}
 
开发者ID:roscisz,项目名称:KernelHive,代码行数:9,代码来源:TCPServer.java

示例8: xferTest07

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
@Test
public void xferTest07() throws Exception { // for bug 5103988
    File source = File.createTempFile("source", null);
    source.deleteOnExit();

    FileChannel sourceChannel = new RandomAccessFile(source, "rw")
        .getChannel();
    sourceChannel.position(32000L)
        .write(ByteBuffer.wrap("The End".getBytes()));

    // The sink is a non-blocking socket channel
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(0));
    InetSocketAddress sa = new InetSocketAddress(
        InetAddress.getLocalHost(), ssc.socket().getLocalPort());
    SocketChannel sink = SocketChannel.open(sa);
    sink.configureBlocking(false);
    SocketChannel other = ssc.accept();

    long size = sourceChannel.size();

    // keep sending until congested
    long n;
    do {
        n = sourceChannel.transferTo(0, size, sink);
    } while (n > 0);

    sourceChannel.close();
    sink.close();
    other.close();
    ssc.close();
    source.delete();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:Transfer.java

示例9: bindToPort

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
@VisibleForTesting
ServerSocketChannel bindToPort() throws IOException {
    // Bind to the first available port in the range
    ServerSocketChannel channel = null;

    for (int port = ConnectionHandler.PORT_RANGE_START; port <= ConnectionHandler.PORT_RANGE_END; port++) {
        final InetSocketAddress tryAddress = new InetSocketAddress(InetAddress.getLocalHost(), port);


        try {
            channel = ServerSocketChannel.open();
            channel.socket().bind(tryAddress);
            channel.configureBlocking(false);
            break;
        } catch (final IOException ioe) {
            // Ignore, try next port
            logger.warn("Could not bind to {}, trying next port...", tryAddress);
            try {
                if (channel != null) channel.close();
            } catch (final IOException ignored) {
            }
        }
    }

    if (channel == null || !channel.socket().isBound()) {
        throw new IOException("No available port for the BitTorrent client!");
    }
    return channel;
}
 
开发者ID:anthonyraymond,项目名称:joal,代码行数:30,代码来源:ConnectionHandler.java

示例10: setUp

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
@Before
public void setUp() throws IOException {
    eventLoop = new NIOEventLoop();
    serverHandler = new TestHandler();
    acceptSocket = ServerSocketChannel.open();
    // Mac OS X: Must bind() before calling Selector.register, or you don't get accept() events
    acceptSocket.socket().bind(null);
    serverPort = acceptSocket.socket().getLocalPort();
}
 
开发者ID:s-store,项目名称:s-store,代码行数:10,代码来源:NIOEventLoopTest.java

示例11: MockVolt

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
MockVolt(int port) {
    try {
        network = new VoltNetwork();
        network.start();
        socket = ServerSocketChannel.open();
        socket.configureBlocking(false);
        socket.socket().bind(new InetSocketAddress(port));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
开发者ID:s-store,项目名称:s-store,代码行数:12,代码来源:TestDistributer.java

示例12: bind

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
protected void bind() throws IOException {
	// allocate an unbound server socket channel
	serverChannel = ServerSocketChannel.open();
	// Get the associated ServerSocket to bind it with
	ServerSocket serverSocket = serverChannel.socket();
	// create a new Selector for use below
	synchronized (Selector.class) {
		// Selector.open() isn't thread safe
		// http://bugs.sun.com/view_bug.do?bug_id=6427854
		// Affects 1.6.0_29, fixed in 1.7.0_01
		this.selector.set(Selector.open());
	}
	// set the port the server channel will listen to
	// serverSocket.bind(new InetSocketAddress(getBind(),
	// getTcpListenPort()));
	bind(serverSocket, getPort(), getAutoBind());
	// set non-blocking mode for the listening socket
	serverChannel.configureBlocking(false);
	// register the ServerSocketChannel with the Selector
	serverChannel.register(this.selector.get(), SelectionKey.OP_ACCEPT);

	// set up the datagram channel
	if (this.getUdpPort() > 0) {
		datagramChannel = DatagramChannel.open();
		configureDatagraChannel();
		// bind to the address to avoid security checks
		bindUdp(datagramChannel.socket(), getUdpPort(), getAutoBind());
	}
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:30,代码来源:NioReceiver.java

示例13: ClientAcceptor

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
ClientAcceptor(int port, VoltNetwork network) {
    m_network = network;
    m_port = port;
    ServerSocketChannel socket;
    try {
        socket = ServerSocketChannel.open();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    m_serverSocket = socket;
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:12,代码来源:ClientInterface.java

示例14: Reactor

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
Reactor(int port) throws IOException { // Reactor初始化
	selector = Selector.open();
	serverSocket = ServerSocketChannel.open();
	serverSocket.socket().bind(new InetSocketAddress(port));
	serverSocket.configureBlocking(false); // 非阻塞
	SelectionKey sk = serverSocket.register(selector, SelectionKey.OP_ACCEPT); // 分步处理,第一步,接收accept事件
	sk.attach(new Acceptor()); // attach callback object, Acceptor
}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:9,代码来源:Reactor.java

示例15: portInit

import java.nio.channels.ServerSocketChannel; //导入方法依赖的package包/类
/**
 * Reinit server on some port
 *
 * @param port port address
 * @throws IOException
 */
private void portInit(int port) throws IOException {

    this.port = port;
    sel = Selector.open();
    server = ServerSocketChannel.open();
    server.configureBlocking(false);
    if (serverConsole instanceof StupidConsole)
        //remote server side
        server.socket().bind(
                new InetSocketAddress(InetAddress.getLocalHost(), port)
        );
    else //localhost side
        server.socket().bind(
                new InetSocketAddress("localhost", port)
        );

    cancellsed.set(false);

    //if main chat haven't initialized yet
    String aDefault = chats.keySet()
            .stream()
            .filter(s -> s.equals("default"))
            .findAny()
            .orElse(null);
    if (aDefault == null) {
        chats.put("default", new StringBuilder(""));
    }

}
 
开发者ID:Sammers21,项目名称:Socket_Chat,代码行数:36,代码来源:Server.java


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