本文整理汇总了Java中java.nio.channels.SocketChannel.socket方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.socket方法的具体用法?Java SocketChannel.socket怎么用?Java SocketChannel.socket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.socket方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: acceptConnection
import java.nio.channels.SocketChannel; //导入方法依赖的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();
}
}
示例2: closeBlocking
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public void closeBlocking() throws InterruptedException {
super.closeBlocking();
try {
Field channelField = this.getClass().getSuperclass().getDeclaredField("channel");
channelField.setAccessible(true);
SocketChannel channel = (SocketChannel) channelField.get(this);
if (channel != null && channel.isOpen()) {
Socket socket = channel.socket();
if (socket != null)
socket.close();
}
}
catch (Exception ex) {
System.out.println("Exception in Websocket close hack.");
ex.printStackTrace();
}
}
示例3: NioTcpMessageChannel
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
protected NioTcpMessageChannel(NioTcpMessageProcessor nioTcpMessageProcessor,
SocketChannel socketChannel) throws IOException {
super(nioTcpMessageProcessor.getSIPStack());
super.myClientInputStream = socketChannel.socket().getInputStream();
try {
this.peerAddress = socketChannel.socket().getInetAddress();
this.peerPort = socketChannel.socket().getPort();
this.socketChannel = socketChannel;
super.mySock = socketChannel.socket();
// messages that we write out to him.
nioParser = new NioPipelineParser(sipStack, this,
this.sipStack.getMaxMessageSize());
this.peerProtocol = nioTcpMessageProcessor.transport;
lastActivityTimeStamp = System.currentTimeMillis();
super.key = MessageChannel.getKey(peerAddress, peerPort, nioTcpMessageProcessor.transport);
myAddress = nioTcpMessageProcessor.getIpAddress().getHostAddress();
myPort = nioTcpMessageProcessor.getPort();
} finally {
if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
logger.logDebug("Done creating NioTcpMessageChannel " + this + " socketChannel = " +socketChannel);
}
}
}
示例4: Connection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public Connection(SocketChannel channel, long lastContact) {
this.channel = channel;
this.lastContact = lastContact;
this.data = null;
this.dataLengthBuffer = ByteBuffer.allocate(4);
this.unwrappedData = null;
this.unwrappedDataLengthBuffer = ByteBuffer.allocate(4);
this.socket = channel.socket();
this.addr = socket.getInetAddress();
if (addr == null) {
this.hostAddress = "*Unknown*";
} else {
this.hostAddress = addr.getHostAddress();
}
this.remotePort = socket.getPort();
this.responseQueue = new LinkedList<Call>();
if (socketSendBufferSize != 0) {
try {
socket.setSendBufferSize(socketSendBufferSize);
} catch (IOException e) {
LOG.warn("Connection: unable to set socket send buffer size to " +
socketSendBufferSize);
}
}
}
示例5: getAddress
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public InetSocketAddress
getAddress()
{
SocketChannel channel = ep.getSocketChannel();
if ( channel != null ){
Socket socket = channel.socket();
if ( socket != null ){
return((InetSocketAddress)socket.getLocalSocketAddress());
}
}
return( null );
}
示例6: Connection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public Connection(SocketChannel channel, long lastContact) {
this.channel = channel;
this.lastContact = lastContact;
this.data = null;
this.dataLengthBuffer = ByteBuffer.allocate(4);
this.socket = channel.socket();
this.addr = socket.getInetAddress();
if (addr == null) {
this.hostAddress = "*Unknown*";
} else {
this.hostAddress = addr.getHostAddress();
}
this.remotePort = socket.getPort();
if (socketSendBufferSize != 0) {
try {
socket.setSendBufferSize(socketSendBufferSize);
} catch (IOException e) {
LOG.warn("Connection: unable to set socket send buffer size to " +
socketSendBufferSize);
}
}
}
示例7: detectSelfConnect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
boolean detectSelfConnect(SocketChannel socketChannel) {
Socket socket = socketChannel.socket();
SocketAddress localAddress = socket.getLocalSocketAddress();
SocketAddress remoteAddress = socket.getRemoteSocketAddress();
if (localAddress.equals(remoteAddress)) {
LOGGER.debug("Detected that socket was bound to {} while connecting to {}. This looks like the socket connected to itself.",
localAddress, remoteAddress);
return true;
}
return false;
}
示例8: processHandshake
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void processHandshake(SocketChannel accepted) throws IOException {
accepted.configureBlocking(true);
Socket sock = accepted.socket();
sock.setSoTimeout(HANDSHAKE_TIMEOUT);
ObjectInputStream is = new ObjectInputStream(sock.getInputStream());
String authorizationKey = is.readUTF();
LOG.log(Level.FINE, "Approaching agent with authorization key: {0}", authorizationKey);
ShellAgent agent;
synchronized (ShellLaunchManager.this) {
agent = registeredAgents.get(authorizationKey);
}
if (agent == null) {
LOG.log(Level.INFO, "Connection on Java Shell agent port with improper authorization ({0}) from {1}", new Object[] {
authorizationKey,
sock
});
return;
}
// read the port
int targetPort = is.readInt();
InetSocketAddress connectTo = new InetSocketAddress(
((InetSocketAddress)sock.getRemoteSocketAddress()).getAddress(), targetPort);
agent.target(connectTo);
}
示例9: Connection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public Connection(SocketChannel channel, long lastContact) {
this.channel = channel;
this.lastContact = lastContact;
this.data = null;
// the buffer is initialized to read the "hrpc" and after that to read
// the length of the Rpc-packet (i.e 4 bytes)
this.dataLengthBuffer = ByteBuffer.allocate(4);
this.unwrappedData = null;
this.unwrappedDataLengthBuffer = ByteBuffer.allocate(4);
this.socket = channel.socket();
this.addr = socket.getInetAddress();
if (addr == null) {
this.hostAddress = "*Unknown*";
} else {
this.hostAddress = addr.getHostAddress();
}
this.remotePort = socket.getPort();
this.responseQueue = new LinkedList<Call>();
if (socketSendBufferSize != 0) {
try {
socket.setSendBufferSize(socketSendBufferSize);
} catch (IOException e) {
LOG.warn("Connection: unable to set socket send buffer size to " +
socketSendBufferSize);
}
}
}
示例10: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
protected SocketChannel connect(HttpServletRequest request, String host, int port) throws IOException {
nbReceivedRequests.incrementAndGet();
if (httpProxyConfiguration == null) {
return super.connect(request, host, port);
} else {
SocketChannel channel = super.connect(request, httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort());
Socket socket = channel.socket();
establishConnectHandshake(host, port, socket.getOutputStream(), socket.getInputStream());
return channel;
}
}
示例11: HandshakeCompletedListenerImpl
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public HandshakeCompletedListenerImpl(NioTlsMessageChannel tlsMessageChannel, SocketChannel socket) {
tlsMessageChannel.setHandshakeCompletedListener(this);
sipStack = tlsMessageChannel.getSIPStack();
if(sipStack.getSslHandshakeTimeout() > 0) {
this.watchdog = new HandshakeWatchdog(socket.socket());
}
}
示例12: acceptConnection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private final void acceptConnection(SelectionKey key, MMOConnection<T> con)
{
final ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc;
try
{
while ((sc = ssc.accept()) != null)
{
if ((_acceptFilter == null) || _acceptFilter.accept(sc))
{
sc.configureBlocking(false);
final SelectionKey clientKey = sc.register(_selector, SelectionKey.OP_READ);
con = new MMOConnection<>(this, sc.socket(), clientKey, TCP_NODELAY);
con.setClient(_clientFactory.create(con));
clientKey.attach(con);
}
else
{
sc.socket().close();
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
示例13: AcceptableTask
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public AcceptableTask(final SocketChannel sc) throws IOException {
if (sc == null) {
throw new NullPointerException("SocketChannel cannot be null in AcceptableTask");
}
if (sc.socket() == null) {
throw new NullPointerException("Null Socket for SocketChannel in AcceptableTask");
}
this.sc = sc;
this.s = sc.socket();
this.s.setTcpNoDelay(true);
}
示例14: createConnection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* Creates a new Connection using the agent. Throws an IOException if the
* connection attempt is not successful or times out. If an old connection is
* still open, it is closed and event is fired.
*
* @return the Connection the new connection
* @throws IOException
*/
@NbBundle.Messages(value = {
"MSG_AgentConnectionBroken=Control connection with JShell VM is broken, could not connect to agent",
"MSG_AgentNotReady=The JShell VM is not ready for operation"
})
public JShellConnection createConnection() throws IOException {
JShellConnection old;
synchronized (this) {
if (closed) {
throw new IOException(Bundle.MSG_AgentConnectionBroken());
}
if (expectDebugger && debuggerMachine == null) {
return null;
}
// old = connection;
// connection = null;
}
/*
if (old != null) {
old.shutDown();
// notify about the old connection being trashed
ShellLaunchEvent ev = new ShellLaunchEvent(mgr, old, false);
mgr.fire((l) -> l.connectionClosed(ev));
}
*/
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(true);
Socket sock = sc.socket();
sock.connect(connectAddress, ShellLaunchManager.CONNECT_TIMEOUT);
// turn to nonblocking mode
sc.configureBlocking(false);
boolean notify = false;
JShellConnection con = new JShellConnection(this, sock.getChannel());
/*
synchronized (this) {
if (connection == null) {
connection = con;
notify = true;
} else {
con = connection;
}
}
*/
synchronized (this) {
connections.add(con);
}
if (notify) {
ShellLaunchEvent ev = new ShellLaunchEvent(mgr, con, false);
mgr.fire((l) -> l.connectionInitiated(ev));
}
return con;
}
示例15: accept
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void accept()
{
try {
SocketChannel socketChannel = null;
Socket socket = null;
if (serverSocketChannel == null) {
socket = serverSocket.accept();
} else {
socketChannel = serverSocketChannel.accept();
socket = socketChannel.socket();
}
orb.getORBData().getSocketFactory()
.setAcceptedSocketOptions(this, serverSocket, socket);
if (orb.transportDebugFlag) {
dprint(".accept: " +
(serverSocketChannel == null
? serverSocket.toString()
: serverSocketChannel.toString()));
}
CorbaConnection connection =
new SocketOrChannelConnectionImpl(orb, this, socket);
if (orb.transportDebugFlag) {
dprint(".accept: new: " + connection);
}
// NOTE: The connection MUST be put in the cache BEFORE being
// registered with the selector. Otherwise if the bytes
// are read on the connection it will attempt a time stamp
// but the cache will be null, resulting in NPE.
// A connection needs to be timestamped before putting to the cache.
// Otherwise the newly created connection (with 0 timestamp) could be
// incorrectly reclaimed by concurrent reclaim() call OR if there
// will be no events on this connection then it could be reclaimed
// by upcoming reclaim() call.
getConnectionCache().stampTime(connection);
getConnectionCache().put(this, connection);
if (connection.shouldRegisterServerReadEvent()) {
Selector selector = orb.getTransportManager().getSelector(0);
selector.registerForEvent(connection.getEventHandler());
}
getConnectionCache().reclaim();
} catch (IOException e) {
if (orb.transportDebugFlag) {
dprint(".accept:", e);
}
orb.getTransportManager().getSelector(0).unregisterForEvent(this);
// REVISIT - need to close - recreate - then register new one.
orb.getTransportManager().getSelector(0).registerForEvent(this);
// NOTE: if register cycling we do not want to shut down ORB
// since local beans will still work. Instead one will see
// a growing log file to alert admin of problem.
}
}