List of usage examples for java.net Socket setSoTimeout
public synchronized void setSoTimeout(int timeout) throws SocketException
From source file:org.opennms.netmgt.poller.monitors.SmtpMonitor.java
/** * {@inheritDoc}//from w w w . j a va 2 s . c o m * * <P> * Poll the specified address for SMTP service availability. * </P> * * <P> * During the poll an attempt is made to connect on the specified port (by * default TCP port 25). If the connection request is successful, the banner * line generated by the interface is parsed and if the extracted return * code indicates that we are talking to an SMTP server we continue. Next, * an SMTP 'HELO' command is sent to the interface. Again the response is * parsed and a return code extracted and verified. Finally, an SMTP 'QUIT' * command is sent. Provided that the interface's response is valid we set * the service status to SERVICE_AVAILABLE and return. * </P> */ @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); // Get interface address from NetworkInterface // if (iface.getType() != NetworkInterface.TYPE_INET) { throw new NetworkInterfaceNotSupportedException( "Unsupported interface type, only TYPE_INET currently supported"); } TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); int port = ParameterMap.getKeyedInteger(parameters, "port", DEFAULT_PORT); // Get interface address from NetworkInterface // InetAddress ipAddr = iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipAddr); LOG.debug("poll: address = {}, port = {}, {}", hostAddress, port, tracker); PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { Socket socket = null; try { // create a connected socket // tracker.startAttempt(); socket = new Socket(); socket.connect(new InetSocketAddress(ipAddr, port), tracker.getConnectionTimeout()); socket.setSoTimeout(tracker.getSoTimeout()); LOG.debug("SmtpMonitor: connected to host: {} on port: {}", ipAddr, port); // We're connected, so upgrade status to unresponsive serviceStatus = PollStatus.unresponsive(); // Forcing to check for CRLF instead of any other line terminator as per RFC specification CRLFLineReader rdr = new CRLFLineReader(new InputStreamReader(socket.getInputStream(), "ASCII")); // // Tokenize the Banner Line, and check the first // line for a valid return. // String banner = sendMessage(socket, rdr, null); LOG.debug("poll: banner = {}", banner); StringTokenizer t = new StringTokenizer(banner); int rc = Integer.parseInt(t.nextToken()); if (rc == 220) { // // Send the HELO command // String cmd = "HELO " + LOCALHOST_NAME + "\r\n"; socket.getOutputStream().write(cmd.getBytes()); // // get the returned string, tokenize, and // verify the correct output. // String response = rdr.readLine(); double responseTime = tracker.elapsedTimeInMillis(); if (response == null) { continue; } if (MULTILINE.matcher(response).find()) { // Ok we have a multi-line response...first three // chars of the response line are the return code. // The last line of the response will start with // return code followed by a space. String multiLineRC = new String(response.getBytes("ASCII"), 0, 3, "ASCII"); // Create new regExp to look for last line // of this multi line response Pattern endMultiline = null; try { endMultiline = Pattern.compile(multiLineRC); } catch (PatternSyntaxException ex) { throw new java.lang.reflect.UndeclaredThrowableException(ex); } // read until we hit the last line of the multi-line // response do { response = rdr.readLine(); } while (response != null && !endMultiline.matcher(response).find()); if (response == null) { continue; } } t = new StringTokenizer(response); rc = Integer.parseInt(t.nextToken()); if (rc == 250) { response = sendMessage(socket, rdr, "QUIT\r\n"); t = new StringTokenizer(response); rc = Integer.parseInt(t.nextToken()); if (rc == 221) { serviceStatus = PollStatus.available(responseTime); } } } else if (rc == 554) { String response = sendMessage(socket, rdr, "QUIT\r\n"); serviceStatus = PollStatus.unavailable("Server rejecting transactions with 554"); } // If we get this far and the status has not been set to // available, then something didn't verify during the banner // checking or HELO/QUIT comand process. if (!serviceStatus.isAvailable()) { serviceStatus = PollStatus.unavailable(serviceStatus.getReason()); } } catch (NumberFormatException e) { String reason = "NumberFormatException while polling address " + hostAddress; LOG.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (NoRouteToHostException e) { String reason = "No route to host exception for address " + hostAddress; LOG.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); break; // Break out of for(;;) } catch (InterruptedIOException e) { String reason = "Did not receive expected response within timeout " + tracker; LOG.debug(reason); serviceStatus = PollStatus.unavailable(reason); } catch (ConnectException e) { String reason = "Unable to connect to address " + hostAddress; LOG.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } catch (IOException e) { String reason = "IOException while polling address " + hostAddress; LOG.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); } finally { try { // Close the socket if (socket != null) { socket.close(); } } catch (IOException e) { e.fillInStackTrace(); LOG.debug("poll: Error closing socket.", e); } } } // // return the status of the service // return serviceStatus; }
From source file:com.raddle.tools.ClipboardTransferMain.java
private void shutdown() { Socket socket = null; try {/*from ww w. j av a 2 s .c o m*/ tostop = true; socket = new Socket("127.0.0.1", Integer.parseInt(portTxt.getText())); socket.setSoTimeout(2000); ClipCommand cmd = new ClipCommand(); cmd.setCmdCode(ClipCommand.CMD_SHUTDOWN); // ?? ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); out.writeObject(cmd); } catch (Exception e) { updateMessage("??:" + e.getMessage()); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { } } } }
From source file:org.apache.nutch.protocol.htmlunit.HttpResponse.java
/** * Default public constructor.//from ww w . jav a 2 s .co m * @param http * @param url * @param datum * @throws ProtocolException * @throws IOException */ public HttpResponse(HttpBase http, URL url, CrawlDatum datum) throws ProtocolException, IOException { this.http = http; this.url = url; this.orig = url.toString(); this.base = url.toString(); if (!"http".equals(url.getProtocol()) || !!"https".equals(url.getProtocol())) throw new HttpException("Not an HTTP url:" + url); if (Http.LOG.isTraceEnabled()) { Http.LOG.trace("fetching " + url); } String path = "".equals(url.getFile()) ? "/" : url.getFile(); // some servers will redirect a request with a host line like // "Host: <hostname>:80" to "http://<hpstname>/<orig_path>"- they // don't want the :80... String host = url.getHost(); int port; String portString; if (url.getPort() == -1) { port = 80; portString = ""; } else { port = url.getPort(); portString = ":" + port; } Socket socket = null; try { socket = new Socket(); // create the socket socket.setSoTimeout(http.getTimeout()); // connect String sockHost = http.useProxy() ? http.getProxyHost() : host; int sockPort = http.useProxy() ? http.getProxyPort() : port; InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort); socket.connect(sockAddr, http.getTimeout()); this.conf = http.getConf(); this.htmlParseFilters = (HtmlParseFilter[]) PluginRepository.get(conf).getOrderedPlugins( HtmlParseFilter.class, HtmlParseFilter.X_POINT_ID, HtmlParseFilters.HTMLPARSEFILTER_ORDER); if (sockAddr != null && conf.getBoolean("store.ip.address", false) == true) { headers.add("_ip_", sockAddr.getAddress().getHostAddress()); } // make request OutputStream req = socket.getOutputStream(); StringBuffer reqStr = new StringBuffer("GET "); if (http.useProxy()) { reqStr.append(url.getProtocol() + "://" + host + portString + path); } else { reqStr.append(path); } reqStr.append(" HTTP/1.0\r\n"); reqStr.append("Host: "); reqStr.append(host); reqStr.append(portString); reqStr.append("\r\n"); reqStr.append("Accept-Encoding: x-gzip, gzip, deflate\r\n"); String userAgent = http.getUserAgent(); if ((userAgent == null) || (userAgent.length() == 0)) { if (Http.LOG.isErrorEnabled()) { Http.LOG.error("User-agent is not set!"); } } else { reqStr.append("User-Agent: "); reqStr.append(userAgent); reqStr.append("\r\n"); } reqStr.append("Accept-Language: "); reqStr.append(this.http.getAcceptLanguage()); reqStr.append("\r\n"); reqStr.append("Accept: "); reqStr.append(this.http.getAccept()); reqStr.append("\r\n"); if (datum.getModifiedTime() > 0) { reqStr.append("If-Modified-Since: " + HttpDateFormat.toString(datum.getModifiedTime())); reqStr.append("\r\n"); } reqStr.append("\r\n"); byte[] reqBytes = reqStr.toString().getBytes(); req.write(reqBytes); req.flush(); PushbackInputStream in = // process response new PushbackInputStream(new BufferedInputStream(socket.getInputStream(), Http.BUFFER_SIZE), Http.BUFFER_SIZE); StringBuffer line = new StringBuffer(); boolean haveSeenNonContinueStatus = false; while (!haveSeenNonContinueStatus) { // parse status code line this.code = parseStatusLine(in, line); // parse headers parseHeaders(in, line); haveSeenNonContinueStatus = code != 100; // 100 is "Continue" } if (this.code == 200 && !url.toString().endsWith("robots.txt")) { readPlainContent(url); } else { readPlainContent(in); } try { byte[] decodeContent = null; String contentEncoding = getHeader(Response.CONTENT_ENCODING); if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) { decodeContent = http.processGzipEncoded(content, url); } else if ("deflate".equals(contentEncoding)) { decodeContent = http.processDeflateEncoded(content, url); } else { if (Http.LOG.isTraceEnabled()) { Http.LOG.trace("fetched " + content.length + " bytes from " + url); } } if (decodeContent != null) { content = decodeContent; } } catch (Exception e) { headers.remove(Response.CONTENT_ENCODING); } } finally { if (socket != null) socket.close(); } }
From source file:org.apache.isis.objectstore.nosql.db.file.server.FileServer.java
private void startService() { final String serviceHost = config.getString("fileserver.host", DEFAULT_HOST); final int servicePort = config.getInt("fileserver.port", DEFAULT_SERVICE_PORT); final int connectionTimeout = config.getInt("fileserver.connection.timeout", 5000); final int readTimeout = config.getInt("fileserver.read.timeout", 5000); ServerSocket socket = null;// w w w .ja v a2 s . c o m try { LOG.debug("setting up service socket on " + serviceHost + ":" + servicePort); final InetAddress address = InetAddress.getByName(serviceHost); socket = new ServerSocket(servicePort, BACKLOG, address); socket.setSoTimeout(connectionTimeout); LOG.info("file service listenting on " + socket.getInetAddress().getHostAddress() + " port " + socket.getLocalPort()); LOG.debug("file service listenting on " + socket); final LogRange logFileRange = Util.logFileRange(); if (!logFileRange.noLogFile()) { final long lastRecoveryFile = logFileRange.getLast(); final File file = Util.logFile(lastRecoveryFile); LOG.info("replaying last recovery file: " + file.getAbsolutePath()); recover(file); } server.startup(); } catch (final UnknownHostException e) { LOG.error("Unknown host " + serviceHost, e); System.exit(0); } catch (final IOException e) { LOG.error("start failure - networking not set up for " + serviceHost, e); System.exit(0); } catch (final RuntimeException e) { LOG.error("start failure", e); System.exit(0); } do { try { while (isQuiescent) { try { Thread.sleep(300); } catch (final InterruptedException ignore) { } } final Socket connection = socket.accept(); LOG.debug("connection from " + connection); connection.setSoTimeout(readTimeout); serviceConnection(connection, readTimeout); } catch (final SocketTimeoutException expected) { } catch (final IOException e) { LOG.error("networking problem", e); } } while (awaitConnections); }
From source file:com.fluffypeople.managesieve.ManageSieveClient.java
private void setupAfterConnect(Socket sock) throws IOException { sock.setSoTimeout(socketTimeout); byteStream = new BufferedInputStream(sock.getInputStream()); in = new StreamTokenizer(new InputStreamReader(byteStream, UTF8)); setupTokenizer();//from www . ja v a2 s . c o m out = new PrintWriter(new OutputStreamWriter(sock.getOutputStream())); }
From source file:org.apache.tomcat.util.net.PoolTcpEndpoint.java
void setSocketOptions(Socket socket) throws SocketException { if (linger >= 0) socket.setSoLinger(true, linger); if (tcpNoDelay) socket.setTcpNoDelay(tcpNoDelay); if (socketTimeout > 0) socket.setSoTimeout(socketTimeout); }
From source file:org.hyperic.hq.agent.client.AgentConnection.java
protected Socket getSocket() throws IOException { Socket s; // Connect to the remote agent try {//from w ww . j a va 2 s . co m s = new Socket(_agentAddress, _agentPort); } catch (IOException exc) { String msg = "Error connecting to agent @ (" + _agentAddress + ":" + _agentPort + "): " + exc.getMessage(); IOException toThrow = new IOException(msg); // call initCause instead of constructor to be java 1.5 compat toThrow.initCause(exc); throw toThrow; } s.setSoTimeout(SOCKET_TIMEOUT); return s; }
From source file:org.mycard.net.network.AndroidHttpClientConnection.java
/*** * Bind socket and set HttpParams to AndroidHttpClientConnection * @param socket outgoing socket/*w w w. j ava 2 s .c o m*/ * @param params HttpParams * @throws IOException */ public void bind(final Socket socket, final HttpParams params) throws IOException { if (socket == null) { throw new IllegalArgumentException("Socket may not be null"); } if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } assertNotOpen(); socket.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params)); socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params)); int linger = HttpConnectionParams.getLinger(params); if (linger >= 0) { socket.setSoLinger(linger > 0, linger); } this.socket = socket; int buffersize = HttpConnectionParams.getSocketBufferSize(params); this.inbuffer = new SocketInputBuffer(socket, buffersize, params); this.outbuffer = new SocketOutputBuffer(socket, buffersize, params); maxHeaderCount = params.getIntParameter(CoreConnectionPNames.MAX_HEADER_COUNT, -1); maxLineLength = params.getIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, -1); this.requestWriter = new HttpRequestWriter(outbuffer, null, params); this.metrics = new HttpConnectionMetricsImpl(inbuffer.getMetrics(), outbuffer.getMetrics()); this.open = true; }
From source file:org.dstadler.commons.http.NanoHTTPD.java
/** * Starts a HTTP server to given port and only binds on the given name if specifed non-null.<p> * * @param port The port to listen for HTTP connections * @param bindHost If non-null, the hostanme/address to bind to. * @param sessionTimeout Timeout in milliseconds after which reading from the HTTP client side is terminated * with a timeout-error. * * @throws IOException if the socket is already in use *//*from w w w . java 2s.co m*/ public NanoHTTPD(int port, InetAddress bindHost, final int sessionTimeout) throws IOException { myServerSocket = new ServerSocket(port, 50, bindHost); myThread = new Thread("NanoHTTPD Micro Webserver Thread") { @Override public void run() { try { //noinspection InfiniteLoopStatement while (true) { Socket socket = myServerSocket.accept(); if (sessionTimeout > 0) { socket.setSoTimeout(sessionTimeout); } HTTPSession httpSession = new HTTPSession(socket); httpSession.start(); } } catch (IOException e) { if (stopping) { logger.log(Level.INFO, "Stopping socket connections: " + e); } else { logger.log(Level.WARNING, "Failed while accepting socket connections.", e); } } } }; myThread.setDaemon(true); myThread.start(); }
From source file:org.apache.nutch.protocol.http.HttpResponse.java
/** * Default public constructor.//from ww w.j a v a 2 s .c o m * * @param http * @param url * @param datum * @throws ProtocolException * @throws IOException */ public HttpResponse(HttpBase http, URL url, CrawlDatum datum) throws ProtocolException, IOException { this.http = http; this.url = url; this.orig = url.toString(); this.base = url.toString(); Scheme scheme = null; if ("http".equals(url.getProtocol())) { scheme = Scheme.HTTP; } else if ("https".equals(url.getProtocol())) { scheme = Scheme.HTTPS; } else { throw new HttpException("Unknown scheme (not http/https) for url:" + url); } if (Http.LOG.isTraceEnabled()) { Http.LOG.trace("fetching " + url); } String path = "".equals(url.getFile()) ? "/" : url.getFile(); // some servers will redirect a request with a host line like // "Host: <hostname>:80" to "http://<hpstname>/<orig_path>"- they // don't want the :80... LOG.info("Fetching " + url.toString()); String host = url.getHost(); int port; String portString; if (url.getPort() == -1) { if (scheme == Scheme.HTTP) { port = 80; } else { port = 443; } portString = ""; } else { port = url.getPort(); portString = ":" + port; } Socket socket = null; try { socket = new Socket(); // create the socket socket.setSoTimeout(http.getTimeout()); // connect String sockHost = http.useProxy(url) ? http.getProxyHost() : host; int sockPort = http.useProxy(url) ? http.getProxyPort() : port; InetSocketAddress sockAddr = new InetSocketAddress(sockHost, sockPort); socket.connect(sockAddr, http.getTimeout()); if (scheme == Scheme.HTTPS) { SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket sslsocket = (SSLSocket) factory.createSocket(socket, sockHost, sockPort, true); sslsocket.setUseClientMode(true); // Get the protocols and ciphers supported by this JVM Set<String> protocols = new HashSet<String>(Arrays.asList(sslsocket.getSupportedProtocols())); Set<String> ciphers = new HashSet<String>(Arrays.asList(sslsocket.getSupportedCipherSuites())); // Intersect with preferred protocols and ciphers protocols.retainAll(http.getTlsPreferredProtocols()); ciphers.retainAll(http.getTlsPreferredCipherSuites()); sslsocket.setEnabledProtocols(protocols.toArray(new String[protocols.size()])); sslsocket.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()])); sslsocket.startHandshake(); socket = sslsocket; } this.conf = http.getConf(); if (sockAddr != null && conf.getBoolean("store.ip.address", false) == true) { headers.add("_ip_", sockAddr.getAddress().getHostAddress()); } // make request OutputStream req = socket.getOutputStream(); StringBuffer reqStr = new StringBuffer("GET "); if (http.useProxy(url)) { reqStr.append(url.getProtocol() + "://" + host + portString + path); } else { reqStr.append(path); } reqStr.append(" HTTP/1.0\r\n"); reqStr.append("Host: "); reqStr.append(host); reqStr.append(portString); reqStr.append("\r\n"); reqStr.append("Accept-Encoding: x-gzip, gzip, deflate\r\n"); String userAgent = http.getUserAgent(); if ((userAgent == null) || (userAgent.length() == 0)) { if (Http.LOG.isErrorEnabled()) { Http.LOG.error("User-agent is not set!"); } } else { reqStr.append("User-Agent: "); reqStr.append(userAgent); reqStr.append("\r\n"); } reqStr.append("Accept-Language: "); reqStr.append(this.http.getAcceptLanguage()); reqStr.append("\r\n"); reqStr.append("Accept: "); reqStr.append(this.http.getAccept()); reqStr.append("\r\n"); if (http.isIfModifiedSinceEnabled() && datum.getModifiedTime() > 0) { reqStr.append("If-Modified-Since: " + HttpDateFormat.toString(datum.getModifiedTime())); reqStr.append("\r\n"); } reqStr.append("\r\n"); // store the request in the metadata? if (conf.getBoolean("store.http.request", false) == true) { headers.add("_request_", reqStr.toString()); } byte[] reqBytes = reqStr.toString().getBytes(); req.write(reqBytes); req.flush(); LOG.info("Processing response.."); PushbackInputStream in = // process response new PushbackInputStream(new BufferedInputStream(socket.getInputStream(), Http.BUFFER_SIZE), Http.BUFFER_SIZE); StringBuffer line = new StringBuffer(); // store the http headers verbatim if (conf.getBoolean("store.http.headers", false) == true) { httpHeaders = new StringBuffer(); } headers.add("nutch.fetch.time", Long.toString(System.currentTimeMillis())); boolean haveSeenNonContinueStatus = false; while (!haveSeenNonContinueStatus) { // parse status code line this.code = parseStatusLine(in, line); if (httpHeaders != null) httpHeaders.append(line).append("\n"); // parse headers parseHeaders(in, line, httpHeaders); haveSeenNonContinueStatus = code != 100; // 100 is "Continue" } if (httpHeaders != null) { headers.add("_response.headers_", httpHeaders.toString()); } String transferEncoding = getHeader(Response.TRANSFER_ENCODING); LOG.info("Transfer Encoding for " + url + ":" + transferEncoding); if (transferEncoding != null && "chunked".equalsIgnoreCase(transferEncoding.trim())) { readChunkedContent(in, line); } else { readPlainContent(in); } String contentEncoding = getHeader(Response.CONTENT_ENCODING); if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) { content = http.processGzipEncoded(content, url); } else if ("deflate".equals(contentEncoding)) { content = http.processDeflateEncoded(content, url); } else { if (Http.LOG.isTraceEnabled()) { Http.LOG.trace("fetched " + content.length + " bytes from " + url); } } LOG.info("Checking URL:" + url.toString()); //check if url contains google drive string if (url.toString().toLowerCase().contains("https://drive.google.com/")) { //split into two string separated by '=' to get the article id LOG.info("Google Drive URL Detected!"); String[] parts = url.toString().split("="); url = new URL("http://drive.google.com/uc?export=download&id=" + parts[1]); LOG.info("New URL:" + url.toString()); this.http = http; this.url = url; this.orig = url.toString(); this.base = url.toString(); HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url.toString()); int statusCode = client.executeMethod(method); content = method.getResponseBody(); LOG.info("File Size on Drive: " + content.length); // return; } LOG.info("Fetch Bytes: " + content.length + " bytes from " + url); } finally { if (socket != null) socket.close(); } }