Java HTTP Port Find parsePort(String s)

Here you can find the source of parsePort(String s)

Description

Parse the PORT command

License

Open Source License

Parameter

Parameter Description
s - the command string

Return

the addr/port pair

Declaration

static InetSocketAddress parsePort(String s) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import java.util.regex.Pattern;

public class Main {
    /**/*from w  w  w .ja va 2s .  co m*/
     * Parse the PORT command
     * @param s - the command string
     * @return the addr/port pair
     */
    static InetSocketAddress parsePort(String s) {
        String[] toks = Pattern.compile(",").split(s);

        byte[] bAddr = new byte[4];
        for (int j = 0; j < 4; j++) {
            bAddr[j] = new Integer(toks[j]).byteValue();
        }

        InetAddress addr = null;
        try {
            addr = InetAddress.getByAddress(bAddr);
        } catch (UnknownHostException exn) {
            throw new RuntimeException("bad address");
        }

        int portOctet1 = Integer.parseInt(toks[4]);
        int portOctet2 = Integer.parseInt(toks[5]);
        if ((portOctet1 < 0) || (portOctet1 > 255) || (portOctet2 < 0) || (portOctet2 > 255)) {
            throw new RuntimeException("bad port");
        }
        int port = 256 * portOctet1 + portOctet2;

        return new InetSocketAddress(addr, port);
    }
}

Related

  1. loadStringSupportedType(String javaType)
  2. parse(final String desc, final int defaultPort)
  3. parse(String specs, int defaultPort)
  4. parseCommaDelimitedHosts2(String hosts, int port_range)
  5. parseHostPort(String s)
  6. ping(String domain, int port)
  7. pingServer(String host, Integer port)
  8. randomAvailablePort()
  9. randomFreePort()