ServerSocket: bind(SocketAddress endpoint) : ServerSocket : java.net : Java by API examples (example source code) Organized by topic

Java by API
C++
PHP


Java by API  »  java.net   » [  ServerSocket  ]   
 



ServerSocket: bind(SocketAddress endpoint)

import java.nio.CharBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class MainClass {
  public static void main(String[] argsthrows Exception{

    CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();

    ServerSocketChannel server = ServerSocketChannel.open();
    server.socket().bind(new java.net.InetSocketAddress(8000));

    for (;;) { // This server runs forever
      SocketChannel client = server.accept();
      String response = new java.util.Date().toString() "\r\n";
      client.write(encoder.encode(CharBuffer.wrap(response)));
      client.close();
    }
  }
}

           
       
Related examples in the same category
1.  new ServerSocket(int port)
2.  ServerSocket: accept()
3.  ServerSocket: getLocalPort()
4.  ServerSocket: readUTF() and writeUTF(String str)
5.  ServerSocket: setSoTimeout(int timeout)
























Home| Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.