예제 #1
0
  /** Program Entry Point. */
  public static void main(String args[]) {

    // Set up log
    Log log = new Log();
    log.setLogName("Client");

    // Connection object listening on 4001
    Connection conn = new ConnectionImpl(4001);
    InetAddress addr; // will hold address of host to connect to
    try {
      // get address of local host and connect
      addr = InetAddress.getLocalHost();
      conn.connect(addr, 5555);
      // send two messages to server
      for (int i = 0; i < 20; i++) {
        conn.send("Packet " + i);
      }
      int rec = 0;
      try {
        while (rec < 20) {
          String msg = conn.receive();
          Log.writeToLog("Message got through to client: " + msg, "TestClient");
          rec++;
        }
      } catch (EOFException e) {
        Log.writeToLog("Got close request (EOFException), closing.", "TestClient");
        conn.close();
      }

      // write a message in the log and close the connection
      Log.writeToLog("Client is now closing the connection!", "TestApplication");
      conn.close();
    } catch (ConnectException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    } catch (UnknownHostException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    } catch (IOException e) {
      Log.writeToLog(e.getMessage(), "TestApplication");
      e.printStackTrace();
    }

    System.out.println("CLIENT TEST FINISHED");
    Log.writeToLog("CLIENT TEST FINISHED", "TestApplication");
  }