Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Advanced
Jan 28, 2009

Associate a ServerSocket with a ServerSocketChannel in an NIO App

This tip shows you how to create a ServerSocketChannel, to which you'll then associate a standard ServerSocket:

//create the ServerSocketChannel
try{
   serverNIO=ServerSocketChannel.open();
   serverNIO.configureBlocking(false);
   }catch(IOException e)
     {System.out.println("\nError: " + e.getMessage());}
//indicate the host and the port
try{
   InetAddress addr=InetAddress.getByName("localhost");
   ISA=new InetSocketAddress(addr,port);           
   }catch(UnknownHostException e)
      {System.out.println("\nError: " + e.getMessage());}
//connect a ServerSocket to a ServerSocketChannel
try{
   ServerSocket SS=serverNIO.socket();
   SS.bind(ISA);
   System.out.println("- Ready -");
   }catch(IOException e)
      {System.out.println("\nError: " + e.getMessage());}
Leonard Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap