本文整理汇总了Java中java.nio.channels.SocketChannel.connect方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.connect方法的具体用法?Java SocketChannel.connect怎么用?Java SocketChannel.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void run() throws Exception
{
NioThread nt=new NioThread();
nt.start();
Thread.sleep(1000);
SocketChannel sc=SocketChannel.open();
sc.configureBlocking(false);
sc.connect(new InetSocketAddress("localhost", 9999));
ChannelProcessorMultiplexer m=new ChannelProcessorMultiplexer(nt, sc, true,
VideoServerTCPListener.clientID.getBytes(StandardCharsets.UTF_8),
VideoServerTCPListener.serverID.getBytes(StandardCharsets.UTF_8));
DuplexNioConnection conn=new DuplexNioConnection(m);
m.start();
CoolRMIClient client=new CoolRMIClient(getClass().getClassLoader(), new RMINioClientConnectionFactory(conn), false);
Iremote r= (Iremote)client.getService(Iremote.class, Iremote.class.getName());
System.out.println(""+r.getValue("Kitten"));
System.out.println(""+r.getValue("Kitten"));
System.out.println(""+r.getValue("Kitten"));
System.out.println(""+r.getValue("Kitten"));
}
示例2: openConnection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* out node try to connect
*
* @param node
*/
public void openConnection(Node node) {
SocketChannel channel = null;
try {
channel = SocketChannel.open();
channel.configureBlocking(false);
channel.socket().setReuseAddress(true);
InetSocketAddress socketAddress = new InetSocketAddress(node.getIp(), node.getPort());
channel.connect(socketAddress);
PendingConnect data = new PendingConnect(channel, node);
SelectionKey key = channel.register(selector, SelectionKey.OP_CONNECT);
key.attach(data);
selector.wakeup();
} catch (IOException e) {
e.printStackTrace();
if (channel != null) {
try {
channel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
node.destroy();
}
}
示例3: testStartConnection
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* testStartConnection
*/
@Test
public void testStartConnection() throws Exception {
System.err.println("testStartConnection()");
for (final HStoreCoordinator m : this.coordinators) {
// Check that the messenger state is correct
assert (m.isStarted());
// Check that the messenger's listener thread is running
assert (m.getListenerThread().isAlive());
// Check that we can connect to the messenger's listening port
int port = m.getLocalMessengerPort();
SocketChannel channel = SocketChannel.open();
channel.connect(new InetSocketAddress(port));
assert (channel.isConnected());
} // FOR
}
示例4: connect2
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void connect2(Socket socket,
SocketAddress endpoint,
int timeout) throws IOException {
SocketChannel socketChannel = socket.getChannel();
// ����Ϊ��������ʽ
socketChannel.configureBlocking(false);
// ��ѡ����
Selector selector = Selector.open();
// ע�����ӷ����socket����
socketChannel.register(selector, SelectionKey.OP_CONNECT);
// ����
socketChannel.connect(endpoint);
}
示例5: run
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void run() throws Exception
{
NioThread nt=new NioThread();
nt.start();
Thread.sleep(1000);
SocketChannel sc=SocketChannel.open();
sc.configureBlocking(false);
sc.connect(new InetSocketAddress("localhost", 9999));
ChannelProcessorMultiplexer m=new ChannelProcessorMultiplexer(nt, sc, true,
VideoServerTCPListener.clientID.getBytes(StandardCharsets.UTF_8),
VideoServerTCPListener.serverID.getBytes(StandardCharsets.UTF_8));
m.start();
new ConnectToStdout(m);
}
示例6: registerAndConnect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* register with the selection and connect
* @param sock the {@link SocketChannel}
* @param addr the address of remote host
* @throws IOException
*/
void registerAndConnect(SocketChannel sock, InetSocketAddress addr)
throws IOException {
sockKey = sock.register(selector, SelectionKey.OP_CONNECT);
boolean immediateConnect = sock.connect(addr);
if (immediateConnect) {
sendThread.primeConnection();
}
}
示例7: createChannel
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private SocketChannel createChannel() throws IOException {
logi(TAG, "Open");
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(getRewrittenDestination());
return socketChannel;
}
示例8: initClient
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void initClient(String ip, int port) throws IOException { // 获得一个Socket通道
SocketChannel channel = SocketChannel.open(); // 设置通道为非阻塞
channel.configureBlocking(false); // 获得一个通道管理器
this.selector = Selector.open(); // 客户端连接服务器,其实方法执行并没有实现连接,需要在listen()方法中调
channel.connect(new InetSocketAddress(ip, port));
channel.register(selector, SelectionKey.OP_CONNECT);
}
示例9: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void connect(NioThread nt, SocketAddress address, byte[] thisId, byte[] otherId) throws Exception
{
SocketChannel sc=SocketChannel.open();
sc.configureBlocking(false);
sc.connect(address);
connect(nt, sc, true, thisId, otherId);
}
示例10: testConnectCallback
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Test
public void testConnectCallback() throws IOException {
eventLoop.registerAccept(acceptSocket, serverHandler);
SocketChannel clientSocket = SocketChannel.open();
clientSocket.configureBlocking(false);
boolean connected = clientSocket.connect(
new InetSocketAddress(InetAddress.getLocalHost(), serverPort));
assertFalse(connected);
assertTrue(clientSocket.isConnectionPending());
eventLoop.registerConnect(clientSocket, serverHandler);
// registering it for reading as well is not permitted: causes problems on Linux
try {
eventLoop.registerRead(clientSocket, serverHandler);
fail("expected exception");
} catch (AssertionError e) {}
// The event loop will trigger the accept callback
assertNull(serverHandler.client);
eventLoop.runOnce();
assertNotNull(serverHandler.client);
assertTrue(clientSocket.isConnectionPending());
// The event loop will also have triggered the connect callback
assertTrue(serverHandler.connected);
connected = clientSocket.finishConnect();
assertTrue(connected);
assertTrue(clientSocket.isConnected());
// Registering some other handler in response to the connect event should work
eventLoop.registerRead(clientSocket, new TestHandler());
clientSocket.close();
}
示例11: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void connect(Selector selector) {
Connection c = null;
while ((c = connectQueue.poll()) != null) {
try {
SocketChannel channel = (SocketChannel) c.getChannel();
//注册OP_CONNECT监听与后端连接是否真正建立
channel.register(selector, SelectionKey.OP_CONNECT, c);
//主动连接
channel.connect(new InetSocketAddress(c.host, c.port));
} catch (Exception e) {
LOGGER.error("error:", e);
c.close("connect failed:" + e.toString());
}
}
}
示例12: run
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void run() throws Exception
{
NioThread nt=new NioThread();
nt.start();
Thread.sleep(1000);
SocketChannel sc=SocketChannel.open();
sc.configureBlocking(false);
sc.connect(new InetSocketAddress("localhost", 9999));
ChannelProcessorMultiplexer m=new ChannelProcessorMultiplexer(nt, sc, true,
VideoServerTCPListener.clientID.getBytes(StandardCharsets.UTF_8),
VideoServerTCPListener.serverID.getBytes(StandardCharsets.UTF_8));
m.start();
MainChannel mc=new MainChannel();
mc.register(m);
}
示例13: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void connect(Selector finalSelector) {
AbstractConnection c;
while ((c = connectQueue.poll()) != null) {
try {
SocketChannel channel = (SocketChannel) c.getChannel();
channel.register(finalSelector, SelectionKey.OP_CONNECT, c);
channel.connect(new InetSocketAddress(c.host, c.port));
} catch (Exception e) {
LOGGER.info("error:", e);
c.close(e.toString());
}
}
}
示例14: handleClients
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void handleClients() throws Exception {
int selectCount = 0;
while (true) {
int createdCount = 0;
synchronized (this) {
if (connectionsNeeded > 0) {
while (connectionsNeeded > 0 && createdCount < 20) {
connectionsNeeded--;
createdCount++;
totalCreated++;
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(address);
if (!channel.finishConnect()) {
channel.register(selector,
SelectionKey.OP_CONNECT);
}
}
log("Started total of " +
totalCreated + " client connections");
Thread.sleep(200);
}
}
if (createdCount > 0) {
selector.selectNow();
} else {
selectCount++;
long startTime = System.nanoTime();
selector.select();
long duration = durationMillis(startTime);
log("Exited clientSelector.select(), loop #"
+ selectCount + ", duration = " + duration + "ms");
}
int keyCount = -1;
Iterator<SelectionKey> keys =
selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
synchronized (key) {
keyCount++;
keys.remove();
if (!key.isValid()) {
log("Ignoring client key #" + keyCount);
continue;
}
int readyOps = key.readyOps();
if (readyOps == SelectionKey.OP_CONNECT) {
key.interestOps(0);
((SocketChannel) key.channel()).finishConnect();
} else {
log("readyOps() on client key #" + keyCount +
" returned " + readyOps);
}
}
}
}
}
示例15: connect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* Connect a Session to a server
*
* @param session
* @throws IOException
*/
void connect(Session session) throws IOException
{
SocketChannel sChannel = SocketChannel.open();
sChannel.configureBlocking(false);
sChannel.connect(new InetSocketAddress(session.getRequestedConnection().getHostName(), session.getRequestedConnection().getPort()));
sChannel.register(selector, sChannel.validOps());
Connection con = new Connection(this, sChannel, session);
session.setConnection(con);
socChanMap.put(sChannel, session);
}