import java.net.Socket; public class SocketExample { public static void main(String[] args) { Socket socket = new Socket(); if (socket.isConnected()) { System.out.println("Socket is connected"); } else { System.out.println("Socket is not connected"); } socket.close(); } }
Socket is not connected
import java.net.Socket; import java.io.IOException; public class SocketExample { public static void main(String[] args) { Socket socket = new Socket(); try { socket.connect(new InetSocketAddress("localhost", 8080)); } catch (IOException e) { e.printStackTrace(); } if (socket.isConnected()) { System.out.println("Socket is connected"); } else { System.out.println("Socket is not connected"); } socket.close(); } }
Socket is connectedPackage/library: java.net