本文整理匯總了Java中java.nio.channels.ServerSocketChannel.socket方法的典型用法代碼示例。如果您正苦於以下問題:Java ServerSocketChannel.socket方法的具體用法?Java ServerSocketChannel.socket怎麽用?Java ServerSocketChannel.socket使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.ServerSocketChannel
的用法示例。
在下文中一共展示了ServerSocketChannel.socket方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: init
import java.nio.channels.ServerSocketChannel; //導入方法依賴的package包/類
private void init() {
ServerSocketChannel socketChannel = processor.javaChannel();
ServerSocket javaSocket = socketChannel.socket();
try {
if (serverConfig.getReceiveBufferSize() != null) {
javaSocket.setReceiveBufferSize(serverConfig.getReceiveBufferSize());
}
if (serverConfig.getReuseAddress() != null) {
javaSocket.setReuseAddress(serverConfig.getReuseAddress());
}
} catch (SocketException e) {
throw new NioException("config channel error:" + e.getMessage(), e);
}
}
示例2: 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);
}
示例3: 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();
}
}
}
}
示例4: openServerSocket
import java.nio.channels.ServerSocketChannel; //導入方法依賴的package包/類
public final void openServerSocket(InetAddress address, int tcpPort) throws IOException
{
ServerSocketChannel selectable = ServerSocketChannel.open();
selectable.configureBlocking(false);
ServerSocket ss = selectable.socket();
if (address == null)
ss.bind(new InetSocketAddress(tcpPort));
else
ss.bind(new InetSocketAddress(address, tcpPort));
selectable.register(_selector, SelectionKey.OP_ACCEPT);
}
示例5: open
import java.nio.channels.ServerSocketChannel; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
protected ServerSocketChannel open(SocketAddress localAddress) throws Exception {
// Creates the listening ServerSocket
ServerSocketChannel channel = ServerSocketChannel.open();
boolean success = false;
try {
// This is a non blocking socket channel
channel.configureBlocking(false);
// Configure the server socket,
ServerSocket socket = channel.socket();
// Set the reuseAddress flag accordingly with the setting
socket.setReuseAddress(isReuseAddress());
// and bind.
socket.bind(localAddress, getBacklog());
// Register the channel within the selector for ACCEPT event
channel.register(selector, SelectionKey.OP_ACCEPT);
success = true;
} finally {
if (!success) {
close(channel);
}
}
return channel;
}
示例6: getLocalPort
import java.nio.channels.ServerSocketChannel; //導入方法依賴的package包/類
/**
* Port in use.
*/
@Override
public int getLocalPort() {
ServerSocketChannel ssc = serverSock;
if (ssc == null) {
return -1;
} else {
ServerSocket s = ssc.socket();
if (s == null) {
return -1;
} else {
return s.getLocalPort();
}
}
}
示例7: waitForTask
import java.nio.channels.ServerSocketChannel; //導入方法依賴的package包/類
private void waitForTask() throws Exception {
if (!DirectByteBufferPool.initInstance(config.getByteBufferSize(), Config.getMaxTakePollIter())) {
// this is really wrong ... It cannot be already initialized
throw new FDTProcolException("The buffer pool cannot be already initialized");
}
ExecutorService executor = null;
ServerSocketChannel ssc = null;
ServerSocket ss = null;
Selector sel = null;
try {
executor = Utils.getStandardExecService("[ Acceptable ServersThreadPool ] ",
2,
10,
new ArrayBlockingQueue<Runnable>(65500),
Thread.NORM_PRIORITY - 2);
ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ss = ssc.socket();
ss.bind(new InetSocketAddress(config.getPort()));
sel = Selector.open();
ssc.register(sel, SelectionKey.OP_ACCEPT);
System.out.println("READY");
Utils.waitAndWork(executor, ss, sel, config);
} finally {
logger.log(Level.INFO, "[FDT] [ waitForTask ] main loop FINISHED!");
// close all the stuff
Utils.closeIgnoringExceptions(ssc);
Utils.closeIgnoringExceptions(sel);
Utils.closeIgnoringExceptions(ss);
if (executor != null) {
executor.shutdown();
}
}
}
示例8: getLocalPort
import java.nio.channels.ServerSocketChannel; //導入方法依賴的package包/類
/**
* Port in use.
*/
@Override
public int getLocalPort() {
ServerSocketChannel ssc = serverSock;
if (ssc == null) {
return -1;
} else {
ServerSocket s = ssc.socket();
if (s == null) {
return -1;
} else {
return s.getLocalPort();
}
}
}
示例9: openForProject
import java.nio.channels.ServerSocketChannel; //導入方法依賴的package包/類
/**
* Registers the connection and initiates the listening socket.
* @param project the project which is being run / debugged
* @param debugger if true, the connection will pair with a debugger session.
*/
public ShellAgent openForProject(Project p, boolean debugger) throws IOException {
ServerSocket ss;
String encodedKey;
boolean shouldInit = false;
synchronized (this) {
shouldInit = usedKeys.isEmpty();
do {
BigInteger key = BigInteger.probablePrime(64, keyGenerator);
encodedKey = key.toString(Character.MAX_RADIX);
} while (!usedKeys.add(encodedKey));
}
if (shouldInit) {
init();
}
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
SocketAddress local = new InetSocketAddress(
// PENDING: choose something better for remote debugging!
InetAddress.getLoopbackAddress(),
0);
ssc.bind(local);
ssc.accept();
ss = ssc.socket();
LOG.log(Level.FINE, "Creating new server socket {0} for project: {1}", new Object[] {
ss, p
});
ShellAgent agent = new ShellAgent(this, p, ss, encodedKey, debugger);
synchronized (this) {
registeredAgents.put(encodedKey, agent);
}
synchronized (requests) {
servers.wakeup();
requests.add(agent);
}
return agent;
}
示例10: createServerSocketUsingPortRange
import java.nio.channels.ServerSocketChannel; //導入方法依賴的package包/類
/**
* Creates or bind server socket to a random port selected from tcp-port-range which is same as
* membership-port-range.
*
* @param ba
* @param backlog
* @param isBindAddress
* @param tcpBufferSize
* @param sslConnection whether to connect using SSL
*
* @return Returns the new server socket.
*
* @throws IOException
*/
public ServerSocket createServerSocketUsingPortRange(InetAddress ba, int backlog,
boolean isBindAddress, boolean useNIO, int tcpBufferSize, int[] tcpPortRange,
boolean sslConnection) throws IOException {
ServerSocket socket = null;
int localPort = 0;
int startingPort = 0;
// Get a random port from range.
Random rand = new SecureRandom();
int portLimit = tcpPortRange[1];
int randPort = tcpPortRange[0] + rand.nextInt(tcpPortRange[1] - tcpPortRange[0] + 1);
startingPort = randPort;
localPort = startingPort;
while (true) {
if (localPort > portLimit) {
if (startingPort != 0) {
localPort = tcpPortRange[0];
portLimit = startingPort - 1;
startingPort = 0;
} else {
throw new SystemConnectException(
LocalizedStrings.TCPConduit_UNABLE_TO_FIND_FREE_PORT.toLocalizedString());
}
}
try {
if (useNIO) {
ServerSocketChannel channl = ServerSocketChannel.open();
socket = channl.socket();
InetSocketAddress addr = new InetSocketAddress(isBindAddress ? ba : null, localPort);
socket.bind(addr, backlog);
} else {
socket = this.createServerSocket(localPort, backlog, isBindAddress ? ba : null,
tcpBufferSize, sslConnection);
}
break;
} catch (java.net.SocketException ex) {
if (useNIO || SocketCreator.treatAsBindException(ex)) {
localPort++;
} else {
throw ex;
}
}
}
return socket;
}