Esempio n. 1
0
 // *********************************************************
 public Talker(Socket sock, String id) throws IOException {
   this.sock = sock;
   this.id = id;
   dos = new DataOutputStream(sock.getOutputStream());
   dis = new DataInputStream(sock.getInputStream());
   buffRdr = new BufferedReader(new InputStreamReader(dis));
 }
Esempio n. 2
0
  // **********************************************************************************
  //
  // Theoretically, you shouldn't have to alter anything below this point in this file
  //      unless you want to change the color of your agent
  //
  // **********************************************************************************
  public void getConnected(String args[]) {
    try {
      // initial connection
      int port = 3000 + Integer.parseInt(args[1]);
      s = new Socket(args[0], port);
      sout = new PrintWriter(s.getOutputStream(), true);
      sin = new BufferedReader(new InputStreamReader(s.getInputStream()));

      // read in the map of the world
      numNodes = Integer.parseInt(sin.readLine());
      int i, j;
      for (i = 0; i < numNodes; i++) {
        world[i] = new node();
        String[] buf = sin.readLine().split(" ");
        world[i].posx = Double.valueOf(buf[0]);
        world[i].posy = Double.valueOf(buf[1]);
        world[i].numLinks = Integer.parseInt(buf[2]);
        // System.out.println(world[i].posx + ", " + world[i].posy);
        for (j = 0; j < 4; j++) {
          if (j < world[i].numLinks) {
            world[i].links[j] = Integer.parseInt(buf[3 + j]);
            // System.out.println("Linked to: " + world[i].links[j]);
          } else world[i].links[j] = -1;
        }
      }
      currentNode = Integer.parseInt(sin.readLine());

      String myinfo =
          args[2] + "\n" + "0.7 0.45 0.45\n"; // name + rgb values; i think this is color is pink
      // send the agents name and color
      sout.println(myinfo);
    } catch (IOException e) {
      System.out.println(e);
    }
  }
Esempio n. 3
0
 // *********************************************************
 public Talker(String serverName, int port, String id) throws IOException {
   this.serverName = serverName;
   this.port = port;
   this.id = id;
   sock = new Socket(serverName, port);
   dos = new DataOutputStream(sock.getOutputStream());
   dis = new DataInputStream(sock.getInputStream());
   buffRdr = new BufferedReader(new InputStreamReader(dis));
 }
Esempio n. 4
0
 // --------------------------------------------------
 public BaseTConn(String host, String port, TextArea commandArea, TextArea responseArea) {
   this.commandArea = commandArea;
   this.responseArea = responseArea;
   try {
     socket = new Socket(host, Integer.parseInt(port));
     os = new PrintStream(socket.getOutputStream());
     is = new DataInputStream(socket.getInputStream());
     responseArea.appendText("***Connection established" + "\n");
     new Thread(this).start();
   } catch (Exception e) {
     responseArea.appendText("Exception" + "\n");
   }
 }
Esempio n. 5
0
  private void goGet(String host, int port, String contextPath) throws Exception {

    sock = new Socket(host, port);
    OutputStream os = sock.getOutputStream();

    System.out.println(("GET " + contextPath + " HTTP/1.0\n"));
    os.write(("GET " + contextPath + " HTTP/1.0\n").getBytes());
    os.write("\n".getBytes());

    InputStream is = null;
    BufferedReader bis = null;
    String line = null;
    boolean pass = false;
    try {
      is = sock.getInputStream();
      bis = new BufferedReader(new InputStreamReader(is));
      while ((line = bis.readLine()) != null) {
        System.out.println(line);
        // Check if the filter was invoked
        if (EXPECTED_RESPONSE.equals("LLiFFiSSi")) {
          pass = true;
          break;
        }
      }
    } finally {
      try {
        if (is != null) {
          is.close();
        }
      } catch (IOException ioe) {
        // ignore
      }
      try {
        if (bis != null) {
          bis.close();
        }
      } catch (IOException ioe) {
        // ignore
      }
    }

    if (pass) {
      System.out.println("security constraint processed");
      stat.addStatus(TEST_NAME + " PASSED", stat.PASS);
    } else {
      System.out.println("security constraint NOT processed");
      stat.addStatus(TEST_NAME + " FAILED", stat.FAIL);
    }
  }
Esempio n. 6
0
 /**
  * Closes the connection.
  *
  * @exception IOException if an I/O error occurs when closing the socket
  */
 public void close() throws IOException {
   if (socket == null) return;
   socket.close();
   socket = null;
   in = null;
   out = null;
 }
  public ServerMessageGetterSender(Socket socket) {

    this.socket = socket;

    // Get input and output streams in reverse order of ClientMessageGetterSender class
    try {
      this.out = new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException ioe) {
      System.out.println("Could not get ObjectOutputStream on socket: " + socket.getLocalPort());
    }

    try {
      this.in = new ObjectInputStream(socket.getInputStream());
    } catch (IOException ioe) {
      System.out.println("Could not get ObjectInputStream on socket: " + socket.getLocalPort());
    }
  }
Esempio n. 8
0
 public void stop() {
   try {
     if (streamOut != null) streamOut.close();
     if (socket != null) socket.close();
   } catch (IOException ioe) {
     System.err.println("Error closing ...");
     System.exit(0);
   }
   loginThread.close();
 }
Esempio n. 9
0
 // --------------------------------------------------
 public void close() {
   try {
     is.close();
     os.close();
     socket.close();
     responseArea.appendText("***Connection closed" + "\n");
   } catch (IOException e) {
     responseArea.appendText("IO Exception" + "\n");
   }
 }
  public boolean sendMessage(Message messageToSend) {

    try {
      out.writeObject(messageToSend);
    } catch (IOException ioe) {
      System.out.println("Could not write to socket: " + socket.getLocalPort());
      return false;
    }

    return true;
  }
Esempio n. 11
0
  public Duder(String args[]) {

    getConnected(args);

    play();

    try {
      sin.close();
      sout.close();
      s.close();
    } catch (IOException e) {
      System.out.println(e);
    }
  }
Esempio n. 12
0
  private static void goGet(String host, int port, String result, String contextPath)
      throws Exception {
    long time = System.currentTimeMillis();
    Socket s = new Socket(host, port);
    s.setSoTimeout(5000);
    OutputStream os = s.getOutputStream();

    System.out.println(("GET " + contextPath + " HTTP/1.1\n"));
    os.write(("GET " + contextPath + " HTTP/1.1\n").getBytes());
    os.write(("Host: localhost\n").getBytes());
    os.write("\n".getBytes());

    InputStream is = s.getInputStream();
    System.out.println("Time: " + (System.currentTimeMillis() - time));
    BufferedReader bis = new BufferedReader(new InputStreamReader(is));
    String line = null;

    try {
      int index;
      while ((line = bis.readLine()) != null) {
        index = line.indexOf(result);
        System.out.println(line);
        if (index != -1) {
          index = line.indexOf(":");
          String status = line.substring(index + 1);

          if (status.equalsIgnoreCase("PASS")) {
            stat.addStatus("web-readerThreadsConfig: " + line.substring(0, index), stat.PASS);
          } else {
            stat.addStatus("web-readerThreadsConfig: " + line.substring(0, index), stat.FAIL);
          }
          count++;
        }
      }
    } catch (Exception ex) {
    }
  }
Esempio n. 13
0
  /** Creates new form Login */
  public Login() {
    initComponents();
    setTitle("Zlatna ribica");
    /*
    napravit konekciju sa serverom
    */
    String servername = "0.0.0.0"; /* pretpostavljen localhost */
    int port = 1000; /* pretpostavljeno da server slusa na portu 23456 */
    try {
      socket = new Socket(servername, port);
      streamOut = new DataOutputStream(socket.getOutputStream());
      streamIn = new DataInputStream(socket.getInputStream());

    } catch (UnknownHostException uhe) {
      System.err.println("Host unknown " + uhe.getMessage());
      System.exit(0);
    } catch (IOException ioe) {
      System.err.println("Unexpected exception; " + ioe.getMessage());
      System.exit(0);
    }

    loginThread = new LoginThread(this, socket);
    loginThread.start();
  }
Esempio n. 14
0
  private static void goGet(String host, int port, String contextPath) throws Exception {
    Socket s = new Socket(host, port);
    OutputStream os = s.getOutputStream();

    System.out.println("GET " + contextPath + " HTTP/1.0");
    os.write(("GET " + contextPath + " HTTP/1.0\n").getBytes());
    os.write("\n".getBytes());

    InputStream is = s.getInputStream();
    BufferedReader bis = new BufferedReader(new InputStreamReader(is));
    String line = null;

    int count = 0;
    try {
      while ((line = bis.readLine()) != null) {
        if (line.trim().length() > 0) System.out.println(line);
        if (line.indexOf("xxBodyTagxx") >= 0) count++;
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new Exception("Test UNPREDICTED-FAILURE");
    }
    if (count == 1) pass = true;
  }
  public Message getMessage() {

    // TODO error handling if cannot read message

    Message message;

    try {
      message = (Message) in.readObject();
      return message;
    } catch (IOException ioe) {
      System.out.println("Could not read from socket: " + socket.getLocalPort());

      // TODO create error Message type, instantiate one here and send it back
      return new CommandMessage("error", "error");
    } catch (ClassNotFoundException cnfe) {
      System.out.println("Error: Class not found");
      return new CommandMessage("error", "error");
    }
  }
Esempio n. 16
0
 // =========================================================
 public void close() throws IOException {
   sock.close();
 }
Esempio n. 17
0
  public void run() {

    do {
      try {
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        File file = new File("time.txt");
        FileWriter fw = new FileWriter(file);
        Long start = 0l;
        Long end = 0l;
        BufferedWriter bw = new BufferedWriter(fw);
        System.out.println("Enter the preferred choice");
        System.out.println("1. REGISTER");
        System.out.println("2. LEAVE");
        System.out.println("3. SEARCH FOR RFC");
        System.out.println("4. KEEPALIVE");
        System.out.println("5. Do you want to EXIT");
        System.out.println("*********************************************");

        choice = Integer.parseInt(inFromUser.readLine());
        // System.out.println(" Client requesting for connection");
        clientSocket = new Socket("192.168.15.103", 6500);
        BufferedReader inFromServer =
            new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        PrintWriter outToServer =
            new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()), true);

        // outToServer.println("Peer Requesting Connection");

        switch (choice) {
          case 4:
            outToServer.println(
                "KEEP ALIVE P2P-DI Cookieno "
                    + cookie
                    + " OS: "
                    + System.getProperty("os.name")
                    + " "
                    + "v"
                    + System.getProperty("os.version")
                    + " USER: "******"user.name"));
            System.out.println(inFromServer.readLine());
            System.out.println("*********************************************");
            break;
          case 1:
            try {
              // System.out.println("Case 1 entered"); // testing
              // statement
              System.out.println("Please enter your IP addres");
              ip = inFromUser.readLine();
              outToServer.println(
                  "REG P2P-DI/1.1 -1 Portno 6789 Hostname "
                      + ip
                      + " OS: "
                      + System.getProperty("os.name")
                      + " "
                      + "v"
                      + System.getProperty("os.version")
                      + " USER: "******"user.name"));
              cookie = Integer.parseInt(inFromServer.readLine());
              TTL = 7200;

              System.out.println(inFromServer.readLine());
              System.out.print("You are registered at time : ");
              ct.currenttime();
              System.out.println("Peer " + cookie); // peer cookie value
              System.out.println("TTL Value :" + TTL);
              System.out.println("*********************************************");

              break;
            } catch (Exception e) {
            }
          case 2:

            // System.out.println("Case 2 entered"); testing statement
            outToServer.println(
                "LEAVE P2P-DI Cookieno "
                    + cookie
                    + " OS: "
                    + System.getProperty("os.name")
                    + " "
                    + "v"
                    + System.getProperty("os.version")
                    + " USER: "******"user.name"));
            // System.out.println("I am in peer"); testing statement
            System.out.println(inFromServer.readLine());
            System.out.println("*********************************************");
            break;

          case 3:
            outToServer.println(
                "PQUERY P2P-DI Cookieno "
                    + cookie
                    + " Portno 6789"
                    + " OS: "
                    + System.getProperty("os.name")
                    + " "
                    + "v"
                    + System.getProperty("os.version")
                    + " USER: "******"user.name"));
            System.out.println("Which RFC number do you wish to have ?");
            reqrfc = Integer.parseInt(inFromUser.readLine());

            // System.out.println("Entered peer again");
            // outToServer.println("KEEP ALIVE cookieno "+cookie);

            String details = inFromServer.readLine();

            String[] parray = details.split(" ");
            int inactive = Integer.parseInt(parray[(parray.length - 1)]);
            // System.out.println(darray.length);
            if ((parray.length == 3) && (ip.equals(parray[1]))) {
              System.out.println("P2P-DI No Active Peers available");
              System.out.println("*********************************************");
            } else {
              System.out.println("****<POPULATING THE ACTIVE PEER LIST>****");
              System.out.println();
              System.out.println("The active peer list is as follows:");
              System.out.println();

              String[] darray = details.split(" ");

              // System.out.println("Array length"+darray.length);
              for (int i = 0; i < (darray.length - 2); i = i + 2) {

                acthostname[j] = darray[i + 1];
                System.out.println("Hostname :" + acthostname[j]);

                actportno[j] = Integer.parseInt(darray[i + 2]);
                System.out.println("Portno :" + actportno[j]);

                System.out.println("*****************************");
                j = j + 1;
              }

              System.out.println("Connecting to the active peers for its RFC Index");
              for (int x = 0; x < j; x++) {
                // System.out.println(ip);
                if (!(acthostname[x].equals(ip))) {
                  System.out.println("Connecting to " + acthostname[x]);

                  Socket peersocket = new Socket(acthostname[x], 6791); // implement
                  // a for
                  // loop

                  BufferedReader inFromPeer =
                      new BufferedReader(new InputStreamReader(peersocket.getInputStream()));
                  PrintWriter outToPeer =
                      new PrintWriter(new OutputStreamWriter(peersocket.getOutputStream()), true);

                  outToPeer.println("RFCIndex");
                  // System.out.println(inFromServer.readLine());
                  // int searchrfc=Integer.parseInt(inFromUser.readLine());
                  // outToServer.println(searchrfc);

                  String rfcindex = inFromPeer.readLine(); // tell server to
                  // send rfc in
                  // string
                  String rfcarray[] = rfcindex.split(" ");
                  //  System.out.println(rfcindex);
                  for (int i = 1; i < rfcarray.length; i = i + 4) {
                    trfcno[z] = Integer.parseInt(rfcarray[i]);
                    //	System.out.println("RFC number " + trfcno[z]);
                    trfctitle[z] = rfcarray[i + 1];
                    //	System.out.println("RFC Title " + trfctitle[z]);
                    tpeername[z] = rfcarray[i + 2];
                    //	System.out.println("Peer Ip Address " + tpeername[z]);
                    tpTTL[z] = Integer.parseInt(rfcarray[i + 3]);
                    //	System.out.println("TTL value :" + tpTTL[z]);

                    counter1 = counter1 + 1;

                    z = z + 1;
                  }
                  z = 0;
                  //         if(arraybound==0)
                  //         {
                  System.arraycopy(trfcno, 0, rfcno, counter2, trfcno.length);
                  System.arraycopy(trfctitle, 0, rfctitle, counter2, trfctitle.length);
                  System.arraycopy(tpeername, 0, peername, counter2, tpeername.length);
                  System.arraycopy(tpTTL, 0, pTTL, counter2, tpTTL.length);
                  z = 0;
                  counter2 = counter1;
                  counter1 = 0;
                  //         arraybound=arraybound+1;
                  //        }

                  System.out.println();
                  System.out.println();
                  System.out.println("*************************************************");
                  System.out.println("RFC Index received from the Peer");
                  //		System.out.println();
                  System.out.println("\n-----------------------------------------");
                  System.out.println("RFC Index System - Display RFC Idex");
                  System.out.println("-------------------------------------------");
                  System.out.format(
                      "%10s%15s%15s%10s", "RFC No", "RFC Title", "Peer Name", "TTL Value");
                  System.out.println();
                  // StudentNode current = top;
                  // while (current != null){
                  // Student read = current.getStudentNode();
                  for (int i = 0; i < 60; i++) {
                    System.out.format(
                        "%10s%15s%15s%10s",
                        " " + rfcno[i], rfctitle[i], peername[i], " " + pTTL[i]);
                    System.out.println();
                  }
                  // This will output with a set number of character spaces
                  // per field, giving the list a table-like quality
                  // }

                  peersocket.close();
                } // end of if

                for (int i = 0; i < rfcno.length; i++) {

                  if (rfcno[i] == reqrfc) {
                    String taddress = InetAddress.getByName(peername[i]).toString();
                    String[] taddr = taddress.split("/");
                    InetAddress tproperaddress = InetAddress.getByName(taddr[1]);
                    //	System.out.println("Inetaddress" + tproperaddress);

                    Socket peersocket1 = new Socket(tproperaddress, 6791); // implement
                    // a
                    // for
                    // loop
                    System.out.println("The connection to the Active Peer is establshed");
                    BufferedReader inFromP2P =
                        new BufferedReader(new InputStreamReader(peersocket1.getInputStream()));
                    PrintWriter outToP2P =
                        new PrintWriter(
                            new OutputStreamWriter(peersocket1.getOutputStream()), true);

                    System.out.println("Requested the RFC to the Active Peer Server");

                    start = System.currentTimeMillis();

                    outToP2P.println("GETRFC " + reqrfc);

                    // Socket socket = ;

                    try {

                      // Socket socket = null;
                      InputStream is = null;
                      FileOutputStream fos = null;
                      BufferedOutputStream bos = null;
                      int bufferSize = 0;

                      try {
                        is = peersocket1.getInputStream();

                        bufferSize = 64;
                        // System.out.println("Buffer size: " + bufferSize);
                      } catch (IOException ex) {
                        System.out.println("Can't get socket input stream. ");
                      }

                      try {
                        fos = new FileOutputStream("E:\\rfc" + reqrfc + "copy.txt");
                        bos = new BufferedOutputStream(fos);

                      } catch (FileNotFoundException ex) {
                        System.out.println("File not found. ");
                      }

                      byte[] bytes = new byte[bufferSize];

                      int count;

                      while ((count = is.read(bytes)) > 0) {
                        //	System.out.println(count);
                        bos.write(bytes, 0, count);
                      }
                      System.out.println("P2P-DI 200 OK The RFC is copied");
                      end = System.currentTimeMillis();
                      System.out.println(
                          "Total Time to download file   " + (end - start) + " milliseconds");

                      bos.flush();

                      bos.close();
                      is.close();
                      peersocket1.close();
                      break;

                    } catch (SocketException e) {
                      System.out.println("Socket exception");
                    }

                  } // end of if
                  else {
                    //  	System.out.println("No Peer with the required RFC could be found");

                  }
                  clientSocket.close();
                  //	System.out.println("Connection closed");
                  bw.close();
                  fw.close();
                } // end of inner for
              } // end of outer for
              System.out.println("Connection closed");
            } // end of switch
        } // end of else which checks the inactive conditions
      } // end of try
      catch (IOException ioe) {
        System.out.println("IOException on socket listen: " + ioe);
        ioe.printStackTrace();
      }

    } // end of do
    while (choice != 5);
  } // end of run
Esempio n. 18
0
 protected void init() throws IOException {
   if (address != null) socket = new Socket(address, port);
   else socket = new Socket(host, port);
   in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   out = socket.getOutputStream();
 }