use of java.nio.channels.ServerSocketChannel in project jetty.project by eclipse.
the class IOTest method testHalfCloseClientServer.
@Test
public void testHalfCloseClientServer() throws Exception {
ServerSocketChannel connector = ServerSocketChannel.open();
connector.socket().bind(null);
Socket client = SocketChannel.open(connector.socket().getLocalSocketAddress()).socket();
client.setSoTimeout(1000);
client.setSoLinger(false, -1);
Socket server = connector.accept().socket();
server.setSoTimeout(1000);
server.setSoLinger(false, -1);
// Write from client to server
client.getOutputStream().write(1);
// Server reads
assertEquals(1, server.getInputStream().read());
// Write from server to client with oshut
server.getOutputStream().write(1);
// System.err.println("OSHUT "+server);
server.shutdownOutput();
// Client reads response
assertEquals(1, client.getInputStream().read());
try {
// Client reads -1 and does ishut
assertEquals(-1, client.getInputStream().read());
assertFalse(client.isInputShutdown());
//System.err.println("ISHUT "+client);
client.shutdownInput();
// Client ???
//System.err.println("OSHUT "+client);
client.shutdownOutput();
//System.err.println("CLOSE "+client);
client.close();
// Server reads -1, does ishut and then close
assertEquals(-1, server.getInputStream().read());
assertFalse(server.isInputShutdown());
try {
server.shutdownInput();
} catch (SocketException e) {
// System.err.println(e);
}
//System.err.println("CLOSE "+server);
server.close();
} catch (Exception e) {
System.err.println(e);
assertTrue(OS.IS_OSX);
}
}
use of java.nio.channels.ServerSocketChannel in project jetty.project by eclipse.
the class ServerConnector method accept.
@Override
public void accept(int acceptorID) throws IOException {
ServerSocketChannel serverChannel = _acceptChannel;
if (serverChannel != null && serverChannel.isOpen()) {
SocketChannel channel = serverChannel.accept();
accepted(channel);
}
}
use of java.nio.channels.ServerSocketChannel in project cryptomator by cryptomator.
the class SingleInstanceManager method startLocalInstance.
/**
* Creates a server socket on a free port and saves the port in
* {@link Preferences#userNodeForPackage(Class)} for {@link Cryptomator} under the
* given applicationKey.
*
* @param applicationKey
* key used to save the port and identify upon connection.
* @param exec
* the task which is submitted is interruptable.
* @return
* @throws IOException
*/
public static LocalInstance startLocalInstance(String applicationKey, ExecutorService exec) throws IOException {
final ServerSocketChannel channel = ServerSocketChannel.open();
boolean success = false;
try {
channel.configureBlocking(false);
channel.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
final int port = ((InetSocketAddress) channel.getLocalAddress()).getPort();
Preferences.userNodeForPackage(Cryptomator.class).putInt(applicationKey, port);
LOG.debug("InstanceManager bound to port {}", port);
Selector selector = Selector.open();
channel.register(selector, SelectionKey.OP_ACCEPT);
LocalInstance instance = new LocalInstance(applicationKey, channel, selector, port);
exec.submit(instance::selectionLoop);
success = true;
return instance;
} finally {
if (!success) {
channel.close();
}
}
}
use of java.nio.channels.ServerSocketChannel in project neo4j by neo4j.
the class NettyServerTest method shouldGivePortConflictErrorWithPortNumberInIt.
@Test
public void shouldGivePortConflictErrorWithPortNumberInIt() throws Throwable {
// Given an occupied port
int port = 16000;
try (ServerSocketChannel ignore = ServerSocketChannel.open().bind(new InetSocketAddress("localhost", port))) {
final ListenSocketAddress address = new ListenSocketAddress("localhost", port);
// Expect
exception.expect(PortBindException.class);
exception.expectMessage("Address localhost:16000 is already in use");
// When
new NettyServer(new NamedThreadFactory("mythreads"), asList(protocolOnAddress(address))).start();
}
}
use of java.nio.channels.ServerSocketChannel in project voltdb by VoltDB.
the class SocketJoiner method doBind.
/*
* Bind to the internal interface if one was specified,
* otherwise bind on all interfaces. The leader won't invoke this.
*/
private void doBind() throws Exception {
LOG.debug("Creating listener socket");
try {
m_selector = Selector.open();
} catch (IOException e) {
throw new RuntimeException(e);
}
ServerSocketChannel listenerSocket = ServerSocketChannel.open();
InetSocketAddress inetsockaddr;
if ((m_internalInterface == null) || (m_internalInterface.length() == 0)) {
inetsockaddr = new InetSocketAddress(m_internalPort);
} else {
inetsockaddr = new InetSocketAddress(m_internalInterface, m_internalPort);
}
try {
hostLog.info("Attempting to bind to internal ip " + inetsockaddr);
listenerSocket.socket().bind(inetsockaddr);
listenerSocket.configureBlocking(false);
m_listenerSockets.add(listenerSocket);
} catch (Exception e) {
/*
* If we bound to the leader address, the internal interface address might not
* bind if it is all interfaces
*/
if (m_listenerSockets.isEmpty()) {
LOG.fatal("Failed to bind to " + inetsockaddr);
CoreUtils.printPortsInUse(hostLog);
throw e;
}
}
for (ServerSocketChannel ssc : m_listenerSockets) {
ssc.register(m_selector, SelectionKey.OP_ACCEPT);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Non-Primary Listening on:" + inetsockaddr.toString());
}
}
Aggregations