예제 #1
0
  /** Starts a Client application. */
  public static void main(String[] args) {
    if (args.length != 3) {
      System.out.println(USAGE);
      System.exit(0);
    }

    String name = args[0];
    InetAddress addr = null;
    int port = 0;
    Socket sock = null;

    // check args[1] - the IP-adress
    try {
      addr = InetAddress.getByName(args[1]);
    } catch (UnknownHostException e) {
      System.out.println(USAGE);
      System.out.println("ERROR: host " + args[1] + " unknown");
      System.exit(0);
    }

    // parse args[2] - the port
    try {
      port = Integer.parseInt(args[2]);
    } catch (NumberFormatException e) {
      System.out.println(USAGE);
      System.out.println("ERROR: port " + args[2] + " is not an integer");
      // System.exit(0);
    }

    // try to open a Socket to the server
    try {
      sock = new Socket(addr, port);
    } catch (IOException e) {
      System.out.println("ERROR: could not create a socket on " + addr + " and port " + port);
    }

    // create Peer object and start the two-way communication
    try {
      Peer server = new Peer(name, sock);
      (new Thread((Runnable) server)).start();
      server.handleTerminalInput();
      server.shutDown();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
예제 #2
0
파일: Server.java 프로젝트: tlbraams/SS
  /** Starts a Server-application. */
  public static void main(String[] args) {

    if (args.length != 2) {
      System.out.println(USAGE);
      System.exit(0);
    }

    String name = args[0];
    int port = 0;
    ServerSocket sock = null;
    Socket clientSock = null;

    try {
      port = Integer.parseInt(args[1]);
    } catch (NumberFormatException e) {
      System.out.println(USAGE);
      System.out.println("ERROR: port " + args[2] + " is not an integer");
      System.exit(0);
    }

    try {
      sock = new ServerSocket(port);
    } catch (IOException e) {
      System.out.println("Error: could not create a serverSocket on " + port);
    }

    try {
      while (true) {
        System.out.println("Waiting for new Client.");
        clientSock = sock.accept();
        System.out.println("Received new connection.");
        System.out.println("socket" + clientSock.getInetAddress());
        Peer server = new Peer(name, clientSock);
        Thread clientHandler = new Thread(server);
        clientHandler.start();
        server.handleTerminalInput();
        server.shutDown();
      }
    } catch (IOException e) {
      System.out.println(e);
    }
  }