Code example for Socket

Methods: closeconnectisConnected

public class Knock { 
	public static boolean doTCPKnock (InetAddress host, int port, int timeout) {
		Socket s;
		try { 
			// s = new Socket(host, port); 
			s = new Socket();
			InetSocketAddress socket = new InetSocketAddress(host, port);
			// Use user-provided timeout 
			s.connect(socket, timeout);
			if (s.isConnected())
		} catch (UnknownHostException ex) {
			return false; 
		} catch (IllegalArgumentException ex) {
			return false; 
		} catch (ConnectException ex) {
			// We dismiss "connection refused" as knockd operates at link-layer 
		} catch (SocketTimeoutException ex) {
			// Ignore timeout -- went through all the above junk to send the 
			// packets within 15 seconds 
Contextual code suggestions in your IDE  Get Codota for Java