Exemple #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 ConnectionImpl2(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
      conn.send("Client: Hello Server! Are you there?");
      conn.send("Client: Hi again!");
      // 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");
  }
Exemple #2
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(); // 78.91.16.205
      // addr = InetAddress.getByAddress(new byte[] { (byte) 78,(byte)  91, (byte) 23, (byte) 32 }
      // );
      conn.connect(addr, 5555);

      // send two messages to server

      String[] msgs = new String[20];

      for (int i = 0; i < msgs.length; i++) {
        msgs[i] = "m" + i;
      }

      for (String msg : msgs) {
        conn.send(msg);
      }

      // conn.send("Client: Hello Server! Are you there?");
      // conn.send("Client: Hi again!");
      // write a message in the log and close the connection
      Log.writeToLog("Client is now closing the connection!", "TestApplication");

      // System.out.println("##### close()");

      conn.close();

      // System.out.println("#### 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");
  }
Exemple #3
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");
  }