Code example for Socket

Methods: close, connect, isConnected

0
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())
				s.close();
		} 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 
		} catch (IOException ex) {