当前位置: 首页>>代码示例>>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;未经允许,请勿转载。