本文整理匯總了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());
}
}