コード例 #1
0
ファイル: TCPClient.java プロジェクト: sivasrinivas/NetSec
  public static void main(String a[]) throws Exception {

    boolean bool = false;

    try {
      InputStreamReader in = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(in);

      do {
        Socket clientSocket = new Socket("localhost", 6789);
        TCPClient client = new TCPClient(clientSocket);
        client.talkOnSocket();

        System.out.println("Do you want tranfer another file : Y/N");
        String choise = br.readLine().trim();
        if (choise.equalsIgnoreCase("y")) {
          bool = true;
        } else if (choise.equalsIgnoreCase("n")) {
          bool = false;
        } else {
          bool = false;
          System.out.println("Invalid entry. Clsoing.");
        }
      } while (bool);
      in.close();
      br.close();

    } catch (Exception e) {
      System.out.println("Server might not be up and running....");
      System.exit(0);
    }
  }
コード例 #2
0
 static byte[] sendrecv(SocketAddress local, SocketAddress remote, byte[] data, long endTime)
     throws IOException {
   TCPClient client = new TCPClient(endTime);
   try {
     if (local != null) client.bind(local);
     client.connect(remote);
     client.send(data);
     return client.recv();
   } finally {
     client.cleanup();
   }
 }
コード例 #3
0
 public static void main(String[] args) throws IOException {
   TCPClient client = new TCPClient();
   client.parseArgs(args);
   client.sync();
 }
コード例 #4
0
  /**
   * Sends a message to a single server and waits for a response. No checking is done to ensure that
   * the response is associated with the query.
   *
   * @param query The query to send.
   * @return The response.
   * @throws IOException An error occurred while sending or receiving.
   */
  public Message send(Message query) throws IOException {
    if (Options.check("verbose"))
      System.err.println(
          "Sending to " + address.getAddress().getHostAddress() + ":" + address.getPort());

    if (query.getHeader().getOpcode() == Opcode.QUERY) {
      Record question = query.getQuestion();
      if (question != null && question.getType() == Type.AXFR) return sendAXFR(query);
    }

    query = (Message) query.clone();
    applyEDNS(query);
    if (tsig != null) {
      tsig.apply(query, null);
    }

    byte[] out = query.toWire(Message.MAXLENGTH);
    int udpSize = maxUDPSize(query);
    boolean tcp = false;
    long endTime = System.currentTimeMillis() + timeoutValue;
    do {
      byte[] b_in;

      if (useTCP || out.length > udpSize) {
        tcp = true;
      }
      if (tcp) {
        b_in = TCPClient.sendrecv(localAddress, address, out, endTime);
      } else {
        b_in = UDPClient.sendrecv(localAddress, address, out, udpSize, endTime);
      }

      /*
       * Check that the response is long enough.
       */
      if (b_in.length < Header.LENGTH) {
        throw new WireParseException("invalid DNS header - " + "too short");
      }
      /*
       * Check that the response ID matches the query ID.  We want
       * to check this before actually parsing the message, so that
       * if there's a malformed response that's not ours, it
       * doesn't confuse us.
       */
      int id = ((b_in[0] & 0xFF) << 8) + (b_in[1] & 0xFF);
      int qid = query.getHeader().getID();
      if (id != qid) {
        String error = "invalid message id: expected " + qid + "; got id " + id;
        if (tcp) {
          throw new WireParseException(error);
        } else {
          if (Options.check("verbose")) {
            System.err.println(error);
          }
          continue;
        }
      }
      Message response = parseMessage(b_in);
      verifyTSIG(query, response, b_in, tsig);
      if (!tcp && !ignoreTruncation && response.getHeader().getFlag(Flags.TC)) {
        tcp = true;
        continue;
      }
      return response;
    } while (true);
  }