Mega Code Archive

 
Categories / Java Tutorial / Network
 

Display Socket InetAddress, Port, LocalPort and Local address

import java.net.Socket; public class MainClass {   public static void main(String[] args) throws Exception {     Socket theSocket = new Socket("127.0.0.1", 80);     System.out.println("Connected to " + theSocket.getInetAddress() + " on port "         + theSocket.getPort() + " from port " + theSocket.getLocalPort() + " of "         + theSocket.getLocalAddress());   } }