ServerSocket: getLocalPort() : ServerSocket « java.net « Java by API






ServerSocket: getLocalPort()

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class MainClass extends Thread {
  private ServerSocket serverSocket;

  public MainClass(int port) throws IOException {
    serverSocket = new ServerSocket(port);
  }

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

        System.out.println("Just connected to " + server.getRemoteSocketAddress());
        DataInputStream in = new DataInputStream(server.getInputStream());
        System.out.println(in.readUTF());

        DataOutputStream out = new DataOutputStream(server.getOutputStream());
        out
            .writeUTF("Thank you for connecting to " + server.getLocalSocketAddress()
                + "\nGoodbye!");

        server.close();
      } catch (SocketTimeoutException s) {
        System.out.println("Socket timed out!");
        break;
      } catch (IOException e) {
        e.printStackTrace();
        break;
      }
    }
  }

  public static void main(String[] args) {
    int port = Integer.parseInt(args[0]);

    try {
      Thread t = new MainClass(port);
      t.start();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
           
       








Related examples in the same category

1.new ServerSocket(int port)
2.ServerSocket: accept()
3.ServerSocket: bind(SocketAddress endpoint)
4.ServerSocket: readUTF() and writeUTF(String str)
5.ServerSocket: setSoTimeout(int timeout)