try{ Socket socket = new Socket("localhost", 8080); // perform some operations on socket socket.close(); }catch(IOException e){ e.printStackTrace(); }
try{ DatagramSocket socket = new DatagramSocket(8080); // perform some operations on socket socket.close(); }catch(IOException e){ e.printStackTrace(); }This code creates a UDP socket connection on port 8080. The socket is then used for communication and finally closed using the close() method. The java.net package library provides classes and interfaces for networking operations in Java.