下面列出了java.net.ServerSocket#setSoTimeout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void startPython(String tmpPath, String args) throws IOException {
String pythonBinaryPath = config.getString(PythonOptions.PYTHON_BINARY_PATH);
try {
Runtime.getRuntime().exec(pythonBinaryPath);
} catch (IOException ignored) {
throw new RuntimeException(pythonBinaryPath + " does not point to a valid python binary.");
}
process = Runtime.getRuntime().exec(pythonBinaryPath + " -B " + tmpPath + FLINK_PYTHON_PLAN_NAME + args);
new Thread(new StreamPrinter(process.getInputStream())).start();
new Thread(new StreamPrinter(process.getErrorStream())).start();
server = new ServerSocket(0);
server.setSoTimeout(50);
process.getOutputStream().write("plan\n".getBytes(ConfigConstants.DEFAULT_CHARSET));
process.getOutputStream().flush();
}
void test() throws IOException {
ss = new ServerSocket(0);
ss.setSoTimeout(ACCEPT_TIMEOUT);
int port = ss.getLocalPort();
(new Thread(this)).start();
try {
URL url = new URL("http://localhost:" + port + "/");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoOutput(true);
uc.setChunkedStreamingMode(4096);
OutputStream os = uc.getOutputStream();
os.write("Hello there".getBytes());
InputStream is = uc.getInputStream();
is.close();
} catch (IOException expected) {
//expected.printStackTrace();
} finally {
ss.close();
}
}
private boolean createServerSocket(DebugSession debugSession) {
synchronized (ServerThread.class) {
try {
myPort = debugSession.getOptions().getPort();
myServer = new ServerSocket(myPort);
myServer.setSoTimeout(TIMEOUT);
myServer.setReuseAddress(true);
} catch (IOException e) {
String mesg = NbBundle.getMessage(ServerThread.class, PORT_OCCUPIED);
mesg = MessageFormat.format(mesg, myPort);
NotifyDescriptor descriptor = new NotifyDescriptor.Confirmation(mesg, JOptionPane.YES_NO_OPTION);
Object choice = DialogDisplayer.getDefault().notify(descriptor);
if (choice.equals(JOptionPane.YES_OPTION)) {
Utils.openPhpOptionsDialog();
}
log(e);
return false;
}
return true;
}
}
public static void main(String [] args) throws IOException, FileNotFoundException {
//The bug 8021820 is a Mac specific and because of that test will pass on all
//other platforms
if (!System.getProperty("os.name").contains("OS X")) {
return;
}
//Create test directory with test files
prepareTestEnv();
//Consume FD ids for this java process to overflow the 1024
openFiles(FDTOOPEN,new File(TESTFILE));
//Wait for incoming connection and make the select() used in java.net
//classes fail the limitation on FDSET_SIZE
ServerSocket socket = new ServerSocket(0);
//Set the minimal timeout, no one is
//going to connect to this server socket
socket.setSoTimeout(1);
// The accept() call will throw SocketException if the
// select() has failed due to limitation on fds size,
// indicating test failure. A SocketTimeoutException
// is expected, so it is caught and ignored, and the test
// passes.
try {
socket.accept();
} catch (SocketTimeoutException e) { }
}
public static void main(String [] args) throws IOException, FileNotFoundException {
//The bug 8021820 is a Mac specific and because of that test will pass on all
//other platforms
if (!System.getProperty("os.name").contains("OS X")) {
return;
}
//Create test directory with test files
prepareTestEnv();
//Consume FD ids for this java process to overflow the 1024
openFiles(FDTOOPEN,new File(TESTFILE));
//Wait for incoming connection and make the select() used in java.net
//classes fail the limitation on FDSET_SIZE
ServerSocket socket = new ServerSocket(0);
//Set the minimal timeout, no one is
//going to connect to this server socket
socket.setSoTimeout(1);
// The accept() call will throw SocketException if the
// select() has failed due to limitation on fds size,
// indicating test failure. A SocketTimeoutException
// is expected, so it is caught and ignored, and the test
// passes.
try {
socket.accept();
} catch (SocketTimeoutException e) { }
}
protected static ServerSocket openServerSocket(final InetAddress address, final int port, final boolean reuseAddress, final int socketTimeout)
throws IOException
{
final ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(reuseAddress);
serverSocket.setSoTimeout(socketTimeout);
serverSocket.bind(new InetSocketAddress(address, port));
return serverSocket;
}
public static void main(String [] args) throws IOException, FileNotFoundException {
//The bug 8021820 is a Mac specific and because of that test will pass on all
//other platforms
if (!System.getProperty("os.name").contains("OS X")) {
return;
}
//Create test directory with test files
prepareTestEnv();
//Consume FD ids for this java process to overflow the 1024
openFiles(FDTOOPEN,new File(TESTFILE));
//Wait for incoming connection and make the select() used in java.net
//classes fail the limitation on FDSET_SIZE
ServerSocket socket = new ServerSocket(0);
//Set the minimal timeout, no one is
//going to connect to this server socket
socket.setSoTimeout(1);
// The accept() call will throw SocketException if the
// select() has failed due to limitation on fds size,
// indicating test failure. A SocketTimeoutException
// is expected, so it is caught and ignored, and the test
// passes.
try {
socket.accept();
} catch (SocketTimeoutException e) { }
}
public static void main(String [] args) throws IOException, FileNotFoundException {
//The bug 8021820 is a Mac specific and because of that test will pass on all
//other platforms
if (!System.getProperty("os.name").contains("OS X")) {
return;
}
//Create test directory with test files
prepareTestEnv();
//Consume FD ids for this java process to overflow the 1024
openFiles(FDTOOPEN,new File(TESTFILE));
//Wait for incoming connection and make the select() used in java.net
//classes fail the limitation on FDSET_SIZE
ServerSocket socket = new ServerSocket(0);
//Set the minimal timeout, no one is
//going to connect to this server socket
socket.setSoTimeout(1);
// The accept() call will throw SocketException if the
// select() has failed due to limitation on fds size,
// indicating test failure. A SocketTimeoutException
// is expected, so it is caught and ignored, and the test
// passes.
try {
socket.accept();
} catch (SocketTimeoutException e) { }
}
void test(String method) throws Exception {
ss = new ServerSocket(0);
ss.setSoTimeout(ACCEPT_TIMEOUT);
int port = ss.getLocalPort();
Thread otherThread = new Thread(this);
otherThread.start();
try {
URL url = new URL("http://localhost:" + port + "/");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoOutput(true);
if (method != null)
uc.setRequestMethod(method);
uc.setChunkedStreamingMode(4096);
OutputStream os = uc.getOutputStream();
os.write("Hello there".getBytes());
InputStream is = uc.getInputStream();
is.close();
} catch (IOException expected) {
//expected.printStackTrace();
} finally {
ss.close();
otherThread.join();
}
}
/**
* Checks that the certificate is compatible with the enabled cipher suites.
* If we don't check now, the JIoEndpoint can enter a nasty logging loop.
* See bug 45528.
*/
private void checkConfig() throws IOException {
// Create an unbound server socket
ServerSocket socket = sslProxy.createServerSocket();
initServerSocket(socket);
try {
// Set the timeout to 1ms as all we care about is if it throws an
// SSLException on accept.
socket.setSoTimeout(1);
socket.accept();
// Will never get here - no client can connect to an unbound port
} catch (SSLException ssle) {
// SSL configuration is invalid. Possibly cert doesn't match ciphers
IOException ioe = new IOException(sm.getString(
"jsse.invalid_ssl_conf", ssle.getMessage()));
ioe.initCause(ssle);
throw ioe;
} catch (Exception e) {
/*
* Possible ways of getting here
* socket.accept() throws a SecurityException
* socket.setSoTimeout() throws a SocketException
* socket.accept() throws some other exception (after a JDK change)
* In these cases the test won't work so carry on - essentially
* the behaviour before this patch
* socket.accept() throws a SocketTimeoutException
* In this case all is well so carry on
*/
} finally {
// Should be open here but just in case
if (!socket.isClosed()) {
socket.close();
}
}
}
public static void main(String [] args) throws IOException, FileNotFoundException {
//The bug 8021820 is a Mac specific and because of that test will pass on all
//other platforms
if (!System.getProperty("os.name").contains("OS X")) {
return;
}
//Create test directory with test files
prepareTestEnv();
//Consume FD ids for this java process to overflow the 1024
openFiles(FDTOOPEN,new File(TESTFILE));
//Wait for incoming connection and make the select() used in java.net
//classes fail the limitation on FDSET_SIZE
ServerSocket socket = new ServerSocket(0);
//Set the minimal timeout, no one is
//going to connect to this server socket
socket.setSoTimeout(1);
// The accept() call will throw SocketException if the
// select() has failed due to limitation on fds size,
// indicating test failure. A SocketTimeoutException
// is expected, so it is caught and ignored, and the test
// passes.
try {
socket.accept();
} catch (SocketTimeoutException e) { }
}
/**
Listen for connections on a particular port.
@param port The port to listen on.
@param registry The registry to install in new connections.
@param listener A ConnectionManagerListener that will be informed of new connections.
@throws IOException If there is a problem listening on the port.
*/
public void listen(int port, Registry registry, ConnectionManagerListener listener) throws IOException {
synchronized (lock) {
if (shutdown) {
throw new IOException("Connection manager has been shut down");
}
Logger.info("Listening for connections on port " + port);
ServerSocket socket = new ServerSocket(port);
socket.setSoTimeout(1000);
socket.setReuseAddress(true);
Reader r = new Reader(socket, registry, listener);
readers.add(r);
r.start();
}
}
public static void main(String [] args) throws IOException, FileNotFoundException {
//The bug 8021820 is a Mac specific and because of that test will pass on all
//other platforms
if (!System.getProperty("os.name").contains("OS X")) {
return;
}
//Create test directory with test files
prepareTestEnv();
//Consume FD ids for this java process to overflow the 1024
openFiles(FDTOOPEN,new File(TESTFILE));
//Wait for incoming connection and make the select() used in java.net
//classes fail the limitation on FDSET_SIZE
ServerSocket socket = new ServerSocket(0);
//Set the minimal timeout, no one is
//going to connect to this server socket
socket.setSoTimeout(1);
// The accept() call will throw SocketException if the
// select() has failed due to limitation on fds size,
// indicating test failure. A SocketTimeoutException
// is expected, so it is caught and ignored, and the test
// passes.
try {
socket.accept();
} catch (SocketTimeoutException e) { }
}
public void setProperties(ServerSocket socket) throws SocketException{
if (rxBufSize != null)
socket.setReceiveBufferSize(rxBufSize.intValue());
if (performanceConnectionTime != null && performanceLatency != null &&
performanceBandwidth != null)
socket.setPerformancePreferences(
performanceConnectionTime.intValue(),
performanceLatency.intValue(),
performanceBandwidth.intValue());
if (soReuseAddress != null)
socket.setReuseAddress(soReuseAddress.booleanValue());
if (soTimeout != null && soTimeout.intValue() >= 0)
socket.setSoTimeout(soTimeout.intValue());
}
public ServerConnection listen(int port, int timeout) throws IOException {
ServerSocket ss = new ServerSocket(port);
ss.setSoTimeout(timeout);
return new ServerSocketConnection(ss);
}
public ServerConnection listen(int port, int timeout) throws IOException {
ServerSocket serverSocket = sslServerSocketFactory.createServerSocket(port);
serverSocket.setSoTimeout(timeout);
return new ServerSocketConnection(serverSocket);
}
public ServerConnection listen(int port, int timeout) throws IOException {
ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
ServerSocket serverSocket = serverSocketFactory.createServerSocket(port);
serverSocket.setSoTimeout(timeout);
return new ServerSocketConnection(serverSocket);
}
private static ServerSocket createServerSocket(Callable<? extends ServerSocket> serverSocketCreator, long timeoutMs)
throws Exception {
ServerSocket s = serverSocketCreator.call();
s.setSoTimeout((int) timeoutMs);
return s;
}
private static ServerSocket createServerSocket(
Func0<? extends ServerSocket> serverSocketCreator, long timeoutMs) throws IOException {
ServerSocket s = serverSocketCreator.call();
s.setSoTimeout((int) timeoutMs);
return s;
}
/**
* Create a new Daemon with the specified port and target, using the
* specified class to handle individual requests.
*
* @param port the port to listen on.
* @param target the target which will receive the generated JMeter test
* components.
* @param proxyClass the proxy class to use to handle individual requests. This
* class must be the {@link Proxy} class or a subclass.
* @throws IOException if an I/O error occurs opening the socket
* @throws IllegalArgumentException if <code>port</code> is outside the allowed range from <code>0</code> to <code>65535</code>
* @throws SocketException when something is wrong on the underlying protocol layer
*/
public JMeterDaemon(int port, JMeterProxyControl target, Class<? extends JMeterProxy> proxyClass)
throws IOException
{
super("HTTP Proxy Daemon");
this.target = target;
this.daemonPort = port;
this.proxyClass = proxyClass;
LOG.info("Creating Daemon Socket on port: " + daemonPort);
mainSocket = new ServerSocket(daemonPort);
mainSocket.setSoTimeout(ACCEPT_TIMEOUT);
}