Example:
package javaechoserver; import java.io.IOException; import java.net.ServerSocket; import java.util.logging.Level; import java.util.logging.Logger; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaEchoServer { public static void main(String[] args) { ServerSocket serverSocket = null; try { //Get a available port by passing 0 serverSocket = new ServerSocket(0); int port = serverSocket.getLocalPort(); System.out.println("Port : " + port); } catch (IOException ex) { Logger.getLogger(JavaEchoServer.class.getName()) .log(Level.SEVERE, null, ex); } finally { if (serverSocket != null){ try { serverSocket.close(); } catch (IOException ex) { Logger.getLogger(JavaEchoServer.class.getName()) .log(Level.SEVERE, null, ex); } } } } }
No comments:
Post a Comment