當前位置: 首頁>>代碼示例>>Java>>正文


Java SocketException類代碼示例

本文整理匯總了Java中java.net.SocketException的典型用法代碼示例。如果您正苦於以下問題:Java SocketException類的具體用法?Java SocketException怎麽用?Java SocketException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


SocketException類屬於java.net包,在下文中一共展示了SocketException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getIPV4

import java.net.SocketException; //導入依賴的package包/類
/**
 * Get ipv4 address.
 *
 * @return
 */
@Nullable
public static String getIPV4() {
    try {
        Enumeration<NetworkInterface> net = NetworkInterface.getNetworkInterfaces();
        while (net.hasMoreElements()) {
            NetworkInterface networkInterface = net.nextElement();
            Enumeration<InetAddress> add = networkInterface.getInetAddresses();
            while (add.hasMoreElements()) {
                InetAddress a = add.nextElement();
                if (!a.isLoopbackAddress()
                        && !a.getHostAddress().contains(":")) {
                    if (Debug.debug) {
                        Log.d(TAG, "getIPV4 : " + a.getHostAddress());
                    }
                    return a.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}
 
開發者ID:xyhuangjinfu,項目名稱:MiniDownloader,代碼行數:29,代碼來源:NetworkUtil.java

示例2: getGPRSIP

import java.net.SocketException; //導入依賴的package包/類
public static String getGPRSIP() {
    String ip = null;
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            for (Enumeration<InetAddress> enumIpAddr = en.nextElement().getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    ip = inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
        ip = null;
    }
    return ip;
}
 
開發者ID:jeasinlee,項目名稱:AndroidBasicLibs,代碼行數:18,代碼來源:DeviceUtils.java

示例3: getMobileIP

import java.net.SocketException; //導入依賴的package包/類
/**
 * 獲取GSM網絡的IP地址
 */
public static String getMobileIP() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = (NetworkInterface) en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    String ipaddress = inetAddress.getHostAddress().toString();
                    return ipaddress;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("getMobileIP", "Exception in Get IP Address: " + ex.toString());
    }
    return "";
}
 
開發者ID:angcyo,項目名稱:RLibrary,代碼行數:21,代碼來源:Network.java

示例4: getLocalIpAddress

import java.net.SocketException; //導入依賴的package包/類
/**
 * 得到ip地址
 * 
 * @return
 */
public static String getLocalIpAddress() {
    String ret = "";
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    ret = inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return ret;
}
 
開發者ID:liu-xiao-dong,項目名稱:JD-Test,代碼行數:23,代碼來源:NetworkUtil.java

示例5: doBind

import java.net.SocketException; //導入依賴的package包/類
/**
 * Creates the datagram socket.
 */
@Override
protected void doBind()
    throws CommunicationException, InterruptedException {

    try {
        synchronized (this) {
            socket = new DatagramSocket(port, address) ;
        }
        dbgTag = makeDebugTag();
    } catch (SocketException e) {
        if (e.getMessage().equals(InterruptSysCallMsg))
            throw new InterruptedException(e.toString()) ;
        else {
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,
                    "doBind", "cannot bind on port " + port);
            }
            throw new CommunicationException(e) ;
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:25,代碼來源:SnmpAdaptorServer.java

示例6: getDevicesIp

import java.net.SocketException; //導入依賴的package包/類
/**
 * 獲取設備IP
 *
 * @return
 */
public String getDevicesIp() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                    .hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}
 
開發者ID:jopenbox,項目名稱:android-lite-utils,代碼行數:24,代碼來源:DevicesUtils.java

示例7: getAddresses

import java.net.SocketException; //導入依賴的package包/類
/**
 * Returns a list of all the addresses on the system.
 * @param  inclLoopback
 *         if {@code true}, include the loopback addresses
 * @param  ipv4Only
 *         it {@code true}, only IPv4 addresses will be included
 */
static List<InetAddress> getAddresses(boolean inclLoopback,
                                      boolean ipv4Only)
    throws SocketException {
    ArrayList<InetAddress> list = new ArrayList<InetAddress>();
    Enumeration<NetworkInterface> nets =
             NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netInf : Collections.list(nets)) {
        Enumeration<InetAddress> addrs = netInf.getInetAddresses();
        for (InetAddress addr : Collections.list(addrs)) {
            if (!list.contains(addr) &&
                    (inclLoopback ? true : !addr.isLoopbackAddress()) &&
                    (ipv4Only ? (addr instanceof Inet4Address) : true)) {
                list.add(addr);
            }
        }
    }

    return list;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:27,代碼來源:Util.java

示例8: getActiveNetworkInterfaceIP

import java.net.SocketException; //導入依賴的package包/類
private String getActiveNetworkInterfaceIP() throws SocketException {
    Enumeration<NetworkInterface>
            networkInterface = NetworkInterface.getNetworkInterfaces();
    String ipv6AddrStr = null;
    while (networkInterface.hasMoreElements()) {
        NetworkInterface nic = networkInterface.nextElement();
        if (nic.isUp() && !nic.isLoopback()) {
            Enumeration<InetAddress> inet = nic.getInetAddresses();
            while (inet.hasMoreElements()) {
                InetAddress addr = inet.nextElement();
                if (addr instanceof Inet4Address
                        && !addr.isLinkLocalAddress()) {
                    return addr.getHostAddress();
                }else if (addr instanceof Inet6Address
                        && !addr.isLinkLocalAddress()) {
                    /*
                    We save last seen IPv6 address which we will return
                    if we do not find any interface with IPv4 address.
                    */
                    ipv6AddrStr = addr.getHostAddress();
                }
            }
        }
    }
    return ipv6AddrStr;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:27,代碼來源:JMXServiceURL.java

示例9: getRealIpWithStaticCache

import java.net.SocketException; //導入依賴的package包/類
/**
 * 取得本機的IP,並把結果放到static變量中.
 *
 * @return 如果有多個IP地址返回外網的IP,多個外網IP返回第一個IP(在多網管等特殊情況下)
 * @throws SocketException
 */
public static String getRealIpWithStaticCache() {
  if (cachedIp == null) {
    synchronized (syncObject) {
      try {
        cachedIp = getRealIp();
      } catch (SocketException ex) {
        LOG.error("", ex);
        cachedIp = "127.0.0.1";
      }
    }
    return cachedIp;
  } else {
    return cachedIp;
  }
}
 
開發者ID:junzixiehui,項目名稱:godeye,代碼行數:22,代碼來源:IpUtils.java

示例10: testNetworkInterfaces

import java.net.SocketException; //導入依賴的package包/類
@Test
public void testNetworkInterfaces() throws SocketException {
    Supplier<Stream<NetworkInterface>> ss = () -> {
        try {
            return NetworkInterface.networkInterfaces()
                    .filter(ni -> isIncluded(ni));
        }
        catch (SocketException e) {
            throw new RuntimeException(e);
        }
    };

    Collection<NetworkInterface> enums = Collections.list(NetworkInterface.getNetworkInterfaces());
    Collection<NetworkInterface> expected = new ArrayList<>();
    enums.forEach(ni -> {
        if (isIncluded(ni)) {
            expected.add(ni);
        }
    });
    withData(TestData.Factory.ofSupplier("Top-level network interfaces", ss))
            .stream(s -> s)
            .expectedResult(expected)
            .exercise();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:NetworkInterfaceStreamTest.java

示例11: getLocalIpAddressV4

import java.net.SocketException; //導入依賴的package包/類
public static String getLocalIpAddressV4() {
        String ip = "";
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    //if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))  //這裏做了一步IPv4的判定
                    if (!inetAddress.isLoopbackAddress()
                            && inetAddress instanceof Inet4Address) {
                        ip = inetAddress.getHostAddress().toString();
                        return ip;
                    }
                }
            }
        } catch (SocketException e) {
//            Log.i("SocketException--->", ""+e.getLocalizedMessage());
            return "ip is error";
        }
        return ip;
    }
 
開發者ID:fengdongfei,項目名稱:CXJPadProject,代碼行數:22,代碼來源:NormalUtil.java

示例12: getIpAddress

import java.net.SocketException; //導入依賴的package包/類
public static String getIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) { // we get both ipv4 and ipv6, we want ipv4
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        Log.e(TAG,"getIpAddress", e);
    }

    return null;
}
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:18,代碼來源:ArchosUtils.java

示例13: formatFlags

import java.net.SocketException; //導入依賴的package包/類
/** format network interface flags */
private static String formatFlags(NetworkInterface nic) throws SocketException {
    StringBuilder flags = new StringBuilder();
    if (nic.isUp()) {
        flags.append("UP ");
    }
    if (nic.supportsMulticast()) {
        flags.append("MULTICAST ");
    }
    if (nic.isLoopback()) {
        flags.append("LOOPBACK ");
    }
    if (nic.isPointToPoint()) {
        flags.append("POINTOPOINT ");
    }
    if (nic.isVirtual()) {
        flags.append("VIRTUAL ");
    }
    flags.append("mtu:" + nic.getMTU());
    flags.append(" index:" + nic.getIndex());
    return flags.toString();
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:23,代碼來源:IfConfig.java

示例14: getGPRSIpAddress

import java.net.SocketException; //導入依賴的package包/類
/**
 * 獲取GPRS連接的情況的IP
 * 
 * @return
 */
private static String getGPRSIpAddress()
{
    
    String ip = "";
    try
    {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
        {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
            {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress() != null)
                {
                    ip = inetAddress.getHostAddress().toString();
                }
            }
        }
    }
    catch (SocketException ex)
    {
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return ip == null ? "" : ip;
}
 
開發者ID:zhuyu1022,項目名稱:amap,代碼行數:31,代碼來源:MIP_NetworkUtils.java

示例15: initSocket

import java.net.SocketException; //導入依賴的package包/類
public static void initSocket(final Socket s, final String host, final int port, final JSONObject error)
		throws SocketException, IOException {
	final SocketAddress endpoint = new InetSocketAddress(host, port);
	try {
		s.setSoTimeout(1000);
		s.connect(endpoint, 1000);
	} catch (SocketTimeoutException | ConnectException | UnknownHostException e) {
		error.put("class", e.getClass().getSimpleName());
		error.put("message", e.getMessage());
	}
}
 
開發者ID:coranos,項目名稱:neo-java,代碼行數:12,代碼來源:TestUtil.java


注:本文中的java.net.SocketException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。