public static Collection<InetAddress> getAllAvailableAddresses() { Set<InetAddress> retval=new HashSet<>(); Enumeration en; try { en=NetworkInterface.getNetworkInterfaces(); if(en == null) return retval; while(en.hasMoreElements()) { NetworkInterface intf=(NetworkInterface)en.nextElement(); Enumeration<InetAddress> addrs=intf.getInetAddresses(); while(addrs.hasMoreElements()) retval.add(addrs.nextElement()); } } catch(SocketException e) { e.printStackTrace(); } return retval; }
@Override public void start() { Socket socket = null; try { Long startTime = System.currentTimeMillis(); socket = new Socket(); try { socket.setSoTimeout(TIME_OUT); } catch (SocketException e) { e.printStackTrace(); } socket.connect(new InetSocketAddress(mHost, mPort), TIME_OUT); if (isConnected = socket.isConnected()) mDelay = System.currentTimeMillis() - startTime; else mError = Cmd.TCP_LINK_ERROR; } catch (UnknownHostException e) { mError = Cmd.UNKNOWN_HOST_ERROR; } catch (IOException e) { mError = Cmd.TCP_LINK_ERROR; } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * 兼容Linux系统 * @return */ public static String getLocalIP() { String ip = ""; try { Enumeration<?> e1 = (Enumeration<?>) NetworkInterface .getNetworkInterfaces(); while (e1.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) e1.nextElement(); Enumeration<?> e2 = ni.getInetAddresses(); while (e2.hasMoreElements()) { InetAddress ia = (InetAddress) e2.nextElement(); if (ia instanceof Inet6Address) continue; if (!ia.isLoopbackAddress()) { ip = ia.getHostAddress(); break; } } } } catch (SocketException e) { e.printStackTrace(); return ""; } return ip; }
e.printStackTrace(); LogLog.error("Could not instantiate DatagramSocket to " + host + ". All logging will FAIL.", e);
private void starter(QuorumPeer self) { this.self = self; port = self.getVotingView().get(self.getId()).electionAddr.getPort(); proposedLeader = -1; proposedZxid = -1; try { mySocket = new DatagramSocket(port); // mySocket.setSoTimeout(20000); } catch (SocketException e1) { e1.printStackTrace(); throw new RuntimeException(); } sendqueue = new LinkedBlockingQueue<ToSend>(2 * self.getVotingView().size()); recvqueue = new LinkedBlockingQueue<Notification>(2 * self.getVotingView() .size()); new Messenger(self.getVotingView().size() * 2, mySocket); }
netInterfaces = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { e.printStackTrace();
public String getMacAddress(String netInterface) { String strMacAddr = ""; byte[] b; try { NetworkInterface NIC = NetworkInterface.getByName(netInterface); if (NIC == null) { NIC = NetworkInterface.getByName("rmnet0");//小米关掉Wifi后只剩下此网卡。 strMacAddr = "没有 " + netInterface + " 网卡"; } if (NIC != null) { b = NIC.getHardwareAddress(); if (b == null) { return strMacAddr; } StringBuffer buffer = new StringBuffer(); for (int i = 0; i < b.length; i++) { if (i != 0) { buffer.append(':'); } String str = Integer.toHexString(b[i] & 0xFF); buffer.append(str.length() == 1 ? 0 + str : str); } strMacAddr = buffer.toString().toUpperCase(); } } catch (SocketException e) { e.printStackTrace(); } return strMacAddr; }
public Acceptor(ServerSocket serverSocket, URI uri) { socket = serverSocket; target = uri; pause.set(new CountDownLatch(0)); try { socket.setSoTimeout(ACCEPT_TIMEOUT_MILLIS); } catch (SocketException e) { e.printStackTrace(); } }
private void starter(QuorumPeer self) { this.self = self; port = self.getVotingView().get(self.getId()).electionAddr.getPort(); proposedLeader = -1; proposedZxid = -1; try { mySocket = new DatagramSocket(port); // mySocket.setSoTimeout(20000); } catch (SocketException e1) { e1.printStackTrace(); throw new RuntimeException(); } sendqueue = new LinkedBlockingQueue<ToSend>(2 * self.getVotingView().size()); recvqueue = new LinkedBlockingQueue<Notification>(2 * self.getVotingView() .size()); new Messenger(self.getVotingView().size() * 2, mySocket); }
private static IPInfo getIPInfo() { try { List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); for (InetAddress addr : addrs) { if (!addr.isLoopbackAddress()) { String sAddr = addr.getHostAddress().toUpperCase(); boolean isIPv4 = isIPv4Address(sAddr); if (isIPv4) { IPInfo info = new IPInfo(); info.addr = addr; info.intf = intf; info.ip = sAddr; info.ip_hex = InetAddress_to_hex(addr); info.netmask_hex = netmask_to_hex(intf.getInterfaceAddresses().get(0).getNetworkPrefixLength()); return info; } } } } } catch (SocketException e) { e.printStackTrace(); } return null; }
} catch (SocketException e) { mError = Cmd.NETWORK_SOCKET_ERROR; e.printStackTrace(); } catch (IOException e) { mError = Cmd.NETWORK_IO_ERROR;
e.printStackTrace(); } catch (IOException e) { e.printStackTrace();
e.printStackTrace(); } catch (SocketException e) { e.printStackTrace();
socket.setSendBufferSize(size); } catch (SocketException e) { e.printStackTrace();
public void listen() throws TTransportException { // Make sure not to block on accept if (serverSocket_ != null) { try { serverSocket_.setSoTimeout(0); } catch (SocketException sx) { sx.printStackTrace(); } } }
public void setOSCInput(int port) { try { receiver = new OSCPortIn(port); } catch (SocketException e) { e.printStackTrace(); } }
public void setOSCInput(int port) { try { receiver = new OSCPortIn(port); } catch (SocketException e) { e.printStackTrace(); } }
private void updateIpAddress() { try { Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces(); ipAddress = null; while( b.hasMoreElements()){ NetworkInterface iface = b.nextElement(); if (iface.getName().startsWith("dock")) { continue; } for ( InterfaceAddress f : iface.getInterfaceAddresses()) { if (f.getAddress().isSiteLocalAddress()) { ipAddress = f.getAddress().getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } }
/** * Thread to accept data from remote host, and pass it to responseHandler */ public void run() { String resp; while(!Thread.interrupted() && !client.isClosed()) { try { if(inStream.ready() && (resp = bufIn.readLine()) != null) { responseMessageHandler.handleResponse(resp + "\n", comm); } } catch (SocketException e) { e.printStackTrace(); return; // terminate thread if disconnected //TODO: at some point, reconnecting should be considered } catch (IOException e) { e.printStackTrace(); return; // terminate thread if disconnected //TODO: at some point, reconnecting should be considered } } }
private boolean testIPv4() { try { Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces(); while (networks.hasMoreElements()) { NetworkInterface network = networks.nextElement(); for (InterfaceAddress address : network.getInterfaceAddresses()) { if (address.getAddress() instanceof Inet4Address) { // found IPv4 address // do any other validation of address you may need here return true; } } } return false; } catch (SocketException e) { e.printStackTrace(); throw new IllegalStateException(e); } } }