use of java.net.ServerSocket in project flink by apache.
the class SocketTextStreamFunctionTest method testSocketSourceOutputAcrossRetries.
@Test
public void testSocketSourceOutputAcrossRetries() throws Exception {
ServerSocket server = new ServerSocket(0);
Socket channel = null;
try {
SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 10, 100);
SocketSourceThread runner = new SocketSourceThread(source, "test1", "check1", "check2");
runner.start();
// first connection: nothing
channel = server.accept();
channel.close();
// second connection: first string
channel = server.accept();
OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream());
writer.write("te");
writer.close();
channel.close();
// third connection: nothing
channel = server.accept();
channel.close();
// forth connection: second string
channel = server.accept();
writer = new OutputStreamWriter(channel.getOutputStream());
writer.write("st1\n");
writer.write("check1\n");
writer.write("check2\n");
writer.flush();
runner.waitForNumElements(2);
runner.cancel();
runner.waitUntilDone();
} finally {
if (channel != null) {
IOUtils.closeQuietly(channel);
}
IOUtils.closeQuietly(server);
}
}
use of java.net.ServerSocket in project flink by apache.
the class SocketTextStreamFunctionTest method testSocketSourceSimpleOutput.
@Test
public void testSocketSourceSimpleOutput() throws Exception {
ServerSocket server = new ServerSocket(0);
Socket channel = null;
try {
SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 0);
SocketSourceThread runner = new SocketSourceThread(source, "test1", "check");
runner.start();
channel = server.accept();
OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream());
writer.write("test1\n");
writer.write("check\n");
writer.flush();
runner.waitForNumElements(2);
runner.cancel();
runner.interrupt();
runner.waitUntilDone();
channel.close();
} finally {
if (channel != null) {
IOUtils.closeQuietly(channel);
}
IOUtils.closeQuietly(server);
}
}
use of java.net.ServerSocket in project robovm by robovm.
the class FtpURLConnection method connectInternal.
private void connectInternal() throws IOException {
int port = url.getPort();
int connectTimeout = getConnectTimeout();
if (port <= 0) {
port = FTP_PORT;
}
if (currentProxy == null || Proxy.Type.HTTP == currentProxy.type()) {
controlSocket = new Socket();
} else {
controlSocket = new Socket(currentProxy);
}
InetSocketAddress addr = new InetSocketAddress(hostName, port);
controlSocket.connect(addr, connectTimeout);
connected = true;
ctrlOutput = controlSocket.getOutputStream();
ctrlInput = controlSocket.getInputStream();
login();
setType();
if (!getDoInput()) {
cd();
}
try {
acceptSocket = new ServerSocket(0);
dataPort = acceptSocket.getLocalPort();
/* Cannot set REUSEADDR so we need to send a PORT command */
port();
if (connectTimeout == 0) {
// set timeout rather than zero as before
connectTimeout = 3000;
}
acceptSocket.setSoTimeout(getConnectTimeout());
if (getDoInput()) {
getFile();
} else {
sendFile();
}
dataSocket = acceptSocket.accept();
dataSocket.setSoTimeout(getReadTimeout());
acceptSocket.close();
} catch (InterruptedIOException e) {
throw new IOException("Could not establish data connection");
}
if (getDoInput()) {
inputStream = new FtpURLInputStream(new BufferedInputStream(dataSocket.getInputStream()), controlSocket);
}
}
use of java.net.ServerSocket in project robovm by robovm.
the class NativeCryptoTest method test_SSL_do_handshake_client_timeout.
public void test_SSL_do_handshake_client_timeout() throws Exception {
// client timeout
final ServerSocket listener = new ServerSocket(0);
Socket serverSocket = null;
try {
Hooks cHooks = new Hooks();
Hooks sHooks = new ServerHooks(getServerPrivateKey(), getServerCertificates());
Future<TestSSLHandshakeCallbacks> client = handshake(listener, 1, true, cHooks, null, null);
Future<TestSSLHandshakeCallbacks> server = handshake(listener, -1, false, sHooks, null, null);
serverSocket = server.get(TIMEOUT_SECONDS, TimeUnit.SECONDS).getSocket();
client.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
fail();
} catch (ExecutionException expected) {
if (SocketTimeoutException.class != expected.getCause().getClass()) {
expected.printStackTrace();
}
assertEquals(SocketTimeoutException.class, expected.getCause().getClass());
} finally {
// Manually close peer socket when testing timeout
IoUtils.closeQuietly(serverSocket);
}
}
use of java.net.ServerSocket in project robovm by robovm.
the class NativeCryptoTest method handshake.
public static Future<TestSSLHandshakeCallbacks> handshake(final ServerSocket listener, final int timeout, final boolean client, final Hooks hooks, final byte[] npnProtocols, final byte[] alpnProtocols) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<TestSSLHandshakeCallbacks> future = executor.submit(new Callable<TestSSLHandshakeCallbacks>() {
@Override
public TestSSLHandshakeCallbacks call() throws Exception {
Socket socket = (client ? new Socket(listener.getInetAddress(), listener.getLocalPort()) : listener.accept());
if (timeout == -1) {
return new TestSSLHandshakeCallbacks(socket, 0, null);
}
FileDescriptor fd = socket.getFileDescriptor$();
long c = hooks.getContext();
long s = hooks.beforeHandshake(c);
TestSSLHandshakeCallbacks callback = new TestSSLHandshakeCallbacks(socket, s, hooks);
if (DEBUG) {
System.out.println("ssl=0x" + Long.toString(s, 16) + " handshake" + " context=0x" + Long.toString(c, 16) + " socket=" + socket + " fd=" + fd + " timeout=" + timeout + " client=" + client);
}
long session = NULL;
try {
session = NativeCrypto.SSL_do_handshake(s, fd, callback, timeout, client, npnProtocols, alpnProtocols);
if (DEBUG) {
System.out.println("ssl=0x" + Long.toString(s, 16) + " handshake" + " session=0x" + Long.toString(session, 16));
}
} finally {
// Ensure afterHandshake is called to free resources
hooks.afterHandshake(session, s, c, socket, fd, callback);
}
return callback;
}
});
executor.shutdown();
return future;
}
Aggregations