本文整理汇总了Java中java.nio.channels.SocketChannel.open方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.open方法的具体用法?Java SocketChannel.open怎么用?Java SocketChannel.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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
}
示例2: open
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* Open client socket and connect to given address and port.
*
* @param address Address to connect to.
* @param port Port to connect to.
* @return {@link TCPClientSocket}
* @throws IOException If an I/O error occurs.
*/
static TCPClientSocketImpl open(String address, int port) throws IOException {
TCPClientSocketImpl clientSocket = null;
try {
clientSocket = new TCPClientSocketImpl(SocketChannel.open());
clientSocket.socketChannel.socket().setSoTimeout(DEFAULT_SOCKET_TIMEOUT_IN_MS);
clientSocket.socketChannel.connect(new InetSocketAddress(address, port));
clientSocket.socketChannel.configureBlocking(false);
} catch(Exception e) {
if (clientSocket != null) {
clientSocket.close();
}
throw e;
}
return clientSocket;
}
示例3: testClientReconnect
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Test
public void testClientReconnect() throws IOException, InterruptedException {
HostProvider hostProvider = mock(HostProvider.class);
when(hostProvider.size()).thenReturn(1);
InetSocketAddress inaddr = new InetSocketAddress(1111);
when(hostProvider.next(anyLong())).thenReturn(inaddr);
ZooKeeper zk = mock(ZooKeeper.class);
sc = SocketChannel.open();
ClientCnxnSocketNIO nioCnxn = new MockCnxn();
ClientWatchManager watcher = mock(ClientWatchManager.class);
ClientCnxn clientCnxn = new ClientCnxn(
"tmp", hostProvider, 5000,
zk, watcher, nioCnxn, false);
clientCnxn.start();
countDownLatch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertTrue(countDownLatch.getCount() == 0);
clientCnxn.close();
}
示例4: main
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void main(String[] argv) throws Exception {
try (ByteServer server = new ByteServer();
SocketChannel sc = SocketChannel.open(server.address())) {
server.acceptConnection();
try (Selector sel = Selector.open()) {
sc.configureBlocking(false);
sc.register(sel, SelectionKey.OP_WRITE);
sel.select();
sel.selectedKeys().clear();
if (sel.select() == 0) {
throw new Exception("Select returned zero");
}
}
}
}
示例5: run
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void run(Consumer<HttpResponse> consumer) throws IOException {
System.out.println("Expecting a response after GET from: " + this.url.toString() + "\n\n");
try (SocketChannel socketChannel = SocketChannel.open(this.address)) {
socketChannel.configureBlocking(true);
writeGetRequestTo(socketChannel);
HttpResponseReader.fromChannel(socketChannel).findFirst().ifPresent(consumer);
}
System.out.println("Demo end");
}
示例6: 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);
}
示例7: FDTNetPerfClient
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
FDTNetPerfClient(String host, int port) throws Exception {
this();
this.sc = SocketChannel.open();
this.sc.configureBlocking(true);
this.sc.socket().connect(new InetSocketAddress(InetAddress.getByName(host), port));
shouldWrite = true;
}
示例8: Tunnel
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public Tunnel(InetSocketAddress serverAddress,Selector selector) throws IOException{
SocketChannel innerChannel=SocketChannel.open();
innerChannel.configureBlocking(false);
this.m_InnerChannel=innerChannel;
this.m_Selector=selector;
this.m_ServerEP=serverAddress;
SessionCount++;
}
示例9: get
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
@Override
public SocketChannel get(int retryNumber, Throwable lastError) {
if(lastError != null) {
logger.error(lastError.getMessage(), lastError);
}
try {
return SocketChannel.open(new InetSocketAddress(host, port));
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
示例10: main
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void main(String[] argv) throws Exception {
try (ByteServer server = new ByteServer();
SocketChannel sc = SocketChannel.open(server.address())) {
server.acceptConnection();
try (Selector sel = Selector.open()) {
sc.configureBlocking(false);
sc.register(sel, SelectionKey.OP_READ);
// Previously channel would get selected here, although there is nothing to read
if (sel.selectNow() != 0)
throw new Exception("Select returned nonzero value");
}
}
}
示例11: init
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public void init() throws Exception {
sel = Selector.open();
InetSocketAddress addr = new InetSocketAddress(serverHost, serverPort);
for (int i = 0; i < sockNum; i++) {
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
System.out.println("initiating connection");
sc.connect(addr);
// TODO ... for the moment there is a 1-1 mapping between "filling threads" and number of sockets ...
Thread t = new Thread(new FillingTask());
t.setDaemon(true);
t.start();
while (!sc.finishConnect()) {
// TODO - do something useful
try {
Thread.sleep(100);
} catch (Exception ex) {
}
;
}
System.out.println("connection established");
sc.register(sel, SelectionKey.OP_WRITE);
}
}
示例12: SocketChannelHelper
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public SocketChannelHelper(String address, int port)
{
this.address = address;
this.port = port;
try
{
this.channel = SocketChannel.open(new InetSocketAddress(address, port));
}
catch (IOException e)
{
e.printStackTrace();
}
}
示例13: main
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
InetAddress lh = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(lh, 12345);
System.setSecurityManager( new SecurityManager() );
for (int i=0; i<100000; i++) {
try {
SocketChannel.open(isa);
throw new RuntimeException("This should not happen");
} catch (SecurityException x) { }
}
}
示例14: 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);
}
示例15: main
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetAddress lh = InetAddress.getLocalHost();
final SocketChannel sc = SocketChannel.open();
final InetSocketAddress isa =
new InetSocketAddress(lh, ssc.socket().getLocalPort());
// establish connection in another thread
Runnable connector =
new Runnable() {
public void run() {
try {
sc.connect(isa);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
};
Thread thr = new Thread(connector);
thr.start();
// wait for connect to be established and for thread to
// terminate
do {
try {
thr.join();
} catch (InterruptedException x) { }
} while (thr.isAlive());
// check connection is established
if (!sc.isConnected()) {
throw new RuntimeException("SocketChannel not connected");
}
// close channel - this triggered the bug as it attempted to signal
// a thread that no longer exists
sc.close();
// clean-up
ssc.accept().close();
ssc.close();
}