本文整理汇总了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;
}
示例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;
}
示例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 "";
}
示例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;
}
示例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) ;
}
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
}
示例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();
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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());
}
}