public HTTPSConnectSocket(String host, int port, String urlString) throws IOException {

    URL url = null;
    try {
      url = new URL(urlString);
    } catch (MalformedURLException me) {
      System.out.println("Malformed url");
      System.exit(1);
    }

    SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    ssl = (SSLSocket) ssf.createSocket(host, port);
    ssl.startHandshake();

    // Send the CONNECT request
    ssl.getOutputStream().write(("CONNECT " + url.getFile() + " HTTP/1.0\r\n\r\n").getBytes());

    // Read the first line of the response
    DataInputStream is = new DataInputStream(ssl.getInputStream());
    String str = is.readLine();

    // Check the HTTP error code -- it should be "200" on success
    if (!str.startsWith("HTTP/1.1 200 ")) {
      if (str.startsWith("HTTP/1.1 ")) str = str.substring(9);
      throw new IOException("Proxy reports \"" + str + "\"");
    }

    // Success -- skip remaining HTTP headers
    do {
      str = is.readLine();
    } while (str.length() != 0);
  }
Beispiel #2
0
  private int Load_Nodes(DataInputStream inStream) {
    // need to open file and load data
    int node_id;
    int x_cor;
    int y_cor;
    // int n_nodes, n_edges, node_cnt, arrow_status;
    Node n;
    String line;
    String item1, item2, item3, item4;

    node_id = 0;
    x_cor = 0;
    y_cor = 0;
    // n_nodes = 0;
    // n_edges = 0;
    // arrow_status = -1;

    try {

      if ((line = inStream.readLine()) != null) {
        StringTokenizer Data = new StringTokenizer(line, " ");
        item1 = Data.nextToken();
        n_nodes = Integer.parseInt(item1);
        item2 = Data.nextToken();
        n_edges = Integer.parseInt(item2);
        item3 = Data.nextToken();
        arrow_status = Integer.parseInt(item3);
        // item4 = Data.nextToken();
        // type = Integer.parseInt( item4 );
        // graph = new GraphClass( n_nodes, n_edges, arrow_status );
        nodes = new Node[n_nodes];
        edges = new Edge[n_edges];

        // ???

        while ((this.Node_Cnt() < n_nodes) && ((line = inStream.readLine()) != null)) {
          Data = new StringTokenizer(line, " ");
          item1 = Data.nextToken();
          item2 = Data.nextToken();
          item3 = Data.nextToken();
          node_id = Integer.parseInt(item1);
          x_cor = Integer.parseInt(item2);
          y_cor = Integer.parseInt(item3);
          n = new Node(node_id, x_cor, y_cor);
          this.Add_Node(n);
        }
        if (n_nodes != 0) {
          source_node = nodes[0];
        }
      }
    } catch (IOException e) {
      System.err.println("error in file" + e.toString());
      System.exit(1);
    }
    return this.Node_Cnt();
  }
Beispiel #3
0
  private int Load_Edges(DataInputStream inStream, int num) {
    String line;
    String item1, item2, item3;
    int source_node;
    int dest_node;
    int value;
    int n_nodes, edge_cnt;
    // Node nodes_array[] = new Node[num]; // Wil
    Edge edge;
    n_nodes = num;
    Node nodes_array[];
    nodes_array = new Node[n_nodes];
    nodes_array = this.Node_Array(); // Wil
    edge_cnt = 0;

    try {
      while ((line = inStream.readLine()) != null) {
        StringTokenizer Data = new StringTokenizer(line, " ");
        item1 = Data.nextToken();
        item2 = Data.nextToken();
        item3 = Data.nextToken();
        source_node = Integer.parseInt(item1);
        dest_node = Integer.parseInt(item2);
        value = Integer.parseInt(item3);
        edge = new Edge(nodes_array[source_node - 1], nodes_array[dest_node - 1], value);
        this.Add_Edge(edge);
        edge_cnt++;
      }
      // inFile.close();
    } catch (IOException e) {
      System.err.println("Error in accessing URL: " + e.toString());
      System.exit(1);
    }
    return edge_cnt;
  }
 // --------------------------------------------------
 public void run() {
   String inputLine;
   try {
     while ((inputLine = is.readLine()) != null) {
       responseArea.appendText(inputLine + "\n");
     }
   } catch (IOException e) {
     responseArea.appendText("IO Exception" + "\n");
   }
 }
 @SuppressWarnings("deprecation")
 private static String ReadInput() throws Exception {
   String zipCode;
   DataInputStream inData = new DataInputStream(System.in);
   zipCode = inData.readLine();
   int zip = Integer.parseInt(zipCode);
   if (zip < 501 || zip > 99950) throw new Exception("zip code not found");
   if (zipCode.length() != 5) throw new NumberFormatException();
   return zipCode;
 }
Beispiel #6
0
 public void run() {
   String response;
   try {
     while ((response = inputstream.readLine()) != null) {
       if (response.indexOf("offline") != -1) break;
       System.out.println(response);
     }
     flag = false;
   } catch (IOException e) {
     flag = false;
     System.err.println("IOException: " + e);
   }
 }
Beispiel #7
0
  /** Returns the path to the file obtained from parsing the HTML header. */
  private static String getPath(DataInputStream in) throws IOException {
    String line = in.readLine();
    String path = "";
    // extract class from GET line
    if (line.startsWith("GET /")) {
      line = line.substring(5, line.length() - 1).trim();
      int index = line.indexOf(' ');
      if (index != -1) {
        path = line.substring(0, index);
      }
    }

    // eat the rest of header
    do {
      line = in.readLine();
    } while ((line.length() != 0) && (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));

    if (path.length() != 0) {
      return path;
    } else {
      throw new IOException("Malformed Header");
    }
  }
Beispiel #8
0
 public void run() // loop utama, utk nerima user input n kirim ke server&client laen
     {
   while (thread != null) {
     try {
       String a = console.readLine();
       String b = cryptor.encrypt(a);
       streamOut.writeUTF(cryptor.encrypt(a));
       streamOut.flush();
     } catch (IOException ioe) {
       System.out.println("Sending error: " + ioe.getMessage());
       stop();
     }
   }
 }
Beispiel #9
0
 public static void main(String args[]) {
   // declaration section:
   // declare a server socket and a client socket for the server
   // declare an input and an output stream
   ServerSocket echoServer = null;
   String line;
   DataInputStream is;
   PrintStream os;
   Socket clientSocket = null;
   // Try to open a server socket on port 9999
   // Note that we can't choose a port less than 1023 if we are not
   // privileged users (root)
   try {
     echoServer = new ServerSocket(9999);
   } catch (IOException e) {
     System.out.println(e);
   }
   // Create a socket object from the ServerSocket to listen and accept
   // connections.
   // Open input and output streams
   try {
     clientSocket = echoServer.accept();
     is = new DataInputStream(clientSocket.getInputStream());
     os = new PrintStream(clientSocket.getOutputStream());
     // As long as we receive data, echo that data back to the client.
     while (true) {
       ObjectInputStream ois = new ObjectInputStream(is);
       test t = null;
       try {
         t = (test) ois.readObject();
       } catch (ClassNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
       System.out.println(t.getS());
       line = is.readLine();
       os.println(line);
     }
   } catch (IOException e) {
     System.out.println(e);
   }
 }