Code example for ServerSocket

Methods: bind

0
	public static ServerSocketChannel createServerSocketChannel(int port) throws IOException {
	    // Create a non-blocking socket channel 
	    ServerSocketChannel ssChannel = ServerSocketChannel.open();
	    ssChannel.configureBlocking(false);
	    ServerSocket serverSocket = ssChannel.socket();
	    serverSocket.bind(new InetSocketAddress(port));  // bind to the port to listen.
	    return ssChannel;
	} 
	 
	/** 
	 * Creates a non-blocking socket channel to connect to specified host name and port. 
	 * connect() is called on the new channel before it is returned. 
	 */ 
	public static SocketChannel createSocketChannel(String hostName, int port) throws IOException {
	    // Create a non-blocking socket channel 
	    SocketChannel sChannel = SocketChannel.open();
	    sChannel.configureBlocking(false);
 
	    // Send a connection request to the server; this method is non-blocking 
	    sChannel.connect(new InetSocketAddress(hostName, port));
	    return sChannel;