Java ServerSocket(int port, int backlog, InetAddress bindAddr) Constructor

Syntax

ServerSocket(int port, int backlog, InetAddress bindAddr) constructor from ServerSocket has the following syntax.

public ServerSocket(int port,   int backlog,    InetAddress bindAddr)    throws IOException

Example

In the following code shows how to use ServerSocket.ServerSocket(int port, int backlog, InetAddress bindAddr) constructor.


//from   w  ww .j  a v  a  2 s.c o  m
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class Main extends Thread {
  private ServerSocket serverSocket;

  public Main () throws IOException {
    serverSocket = new ServerSocket(8008,1000,InetAddress.getByName("java2s.com"));
    serverSocket.setSoTimeout(10000);    
    
  }

  public void run() {
    while (true) {
      try {
        System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
        Socket client = serverSocket.accept();

        System.out.println("Just connected to " + client.getRemoteSocketAddress());
        client.close();
      } catch (SocketTimeoutException s) {
        System.out.println("Socket timed out!");
        break;
      } catch (IOException e) {
        e.printStackTrace();
        break;
      }
    }
  }

  public static void main(String[] args) {
    try {
      Thread t = new Main ();
      t.start();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}




















Home »
  Java Tutorial »
    java.net »




CookieManager
CookiePolicy
CookieStore
DatagramPacket
DatagramSocket
HttpCookie
HttpURLConnection
InetAddress
JarURLConnection
MulticastSocket
ServerSocket
Socket
SocketAddress
URI
URL
URLConnection
URLDecoder
URLEncoder