/*
 add comment: purpose of this class?
*/

import java.net.*;
import java.io.*;

public class SensorServer {

  /* add comments */
  public static void main(String[] args) throws IOException {

    /* add comments */
    if (args.length != 1) {
      System.err.println("Usage: java SensorServer <port number>");
      System.exit(1);
    }

    /* add comments */
    int portNumber = Integer.parseInt(args[0]);

    try (
      /* add comments */
      ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[0]));
    ) {
      System.out.println("Server is listening on TCP port " + portNumber);

      /* add comments */
      Socket clientSocket = serverSocket.accept();
      System.out.println("Client connected from " + clientSocket.getInetAddress() + " port:" + clientSocket.getPort());

      /* add comments */
      PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
      BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
      String inputLine;

      /* add comments */
      while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
      }

    } catch (IOException e) { /* add comments */
      System.out.println("Exception caught when trying to listen on port " + portNumber + " or listening for a connection");
      System.out.println(e.getMessage());
    }
  }
}
