java.net.SocketException类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.5k)|赞(0)|评价(0)|浏览(126)

本文整理了Java中java.net.SocketException类的一些代码示例,展示了SocketException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SocketException类的具体详情如下:
包路径:java.net.SocketException
类名称:SocketException

SocketException介绍

[英]This SocketException may be thrown during socket creation or setting options, and is the superclass of all other socket related exceptions.
[中]此SocketException可能在套接字创建或设置选项期间抛出,它是所有其他与套接字相关的异常的超类。

代码示例

代码示例来源:origin: neo4j/neo4j

  1. @Override
  2. void dump( Logger logger )
  3. {
  4. try
  5. {
  6. Enumeration<NetworkInterface> networkInterfaces = getNetworkInterfaces();
  7. while ( networkInterfaces.hasMoreElements() )
  8. {
  9. NetworkInterface iface = networkInterfaces.nextElement();
  10. logger.log( String.format( "Interface %s:", iface.getDisplayName() ) );
  11. Enumeration<InetAddress> addresses = iface.getInetAddresses();
  12. while ( addresses.hasMoreElements() )
  13. {
  14. InetAddress address = addresses.nextElement();
  15. String hostAddress = address.getHostAddress();
  16. logger.log( " address: %s", hostAddress );
  17. }
  18. }
  19. }
  20. catch ( SocketException e )
  21. {
  22. logger.log( "ERROR: failed to inspect network interfaces and addresses: " + e.getMessage() );
  23. }
  24. }
  25. },

代码示例来源:origin: apache/hbase

  1. private static InetAddress getIpAddress(AddressSelectionCondition condition) throws
  2. SocketException {
  3. // Before we connect somewhere, we cannot be sure about what we'd be bound to; however,
  4. // we only connect when the message where client ID is, is long constructed. Thus,
  5. // just use whichever IP address we can find.
  6. Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
  7. while (interfaces.hasMoreElements()) {
  8. NetworkInterface current = interfaces.nextElement();
  9. if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue;
  10. Enumeration<InetAddress> addresses = current.getInetAddresses();
  11. while (addresses.hasMoreElements()) {
  12. InetAddress addr = addresses.nextElement();
  13. if (addr.isLoopbackAddress()) continue;
  14. if (condition.isAcceptableAddress(addr)) {
  15. return addr;
  16. }
  17. }
  18. }
  19. throw new SocketException("Can't get our ip address, interfaces are: " + interfaces);
  20. }

代码示例来源:origin: wildfly/wildfly

  1. public static Collection<InetAddress> getAllAvailableAddresses() {
  2. Set<InetAddress> retval=new HashSet<>();
  3. Enumeration en;
  4. try {
  5. en=NetworkInterface.getNetworkInterfaces();
  6. if(en == null)
  7. return retval;
  8. while(en.hasMoreElements()) {
  9. NetworkInterface intf=(NetworkInterface)en.nextElement();
  10. Enumeration<InetAddress> addrs=intf.getInetAddresses();
  11. while(addrs.hasMoreElements())
  12. retval.add(addrs.nextElement());
  13. }
  14. }
  15. catch(SocketException e) {
  16. e.printStackTrace();
  17. }
  18. return retval;
  19. }

代码示例来源:origin: netty/netty

  1. AnnotatedSocketException(SocketException exception, SocketAddress remoteAddress) {
  2. super(exception.getMessage() + ": " + remoteAddress);
  3. initCause(exception);
  4. setStackTrace(exception.getStackTrace());
  5. }

代码示例来源:origin: Red5/red5-server

  1. Enumeration<NetworkInterface> netInterfaces = null;
  2. try {
  3. netInterfaces = NetworkInterface.getNetworkInterfaces();
  4. } catch (SocketException e) {
  5. e.printStackTrace();
  6. while (netInterfaces.hasMoreElements()) {
  7. NetworkInterface ni = netInterfaces.nextElement();
  8. Enumeration<InetAddress> address = ni.getInetAddresses();
  9. while (address.hasMoreElements()) {
  10. InetAddress addr = address.nextElement();
  11. if (addr.isLoopbackAddress() && !addr.isSiteLocalAddress()) {
  12. return addr.getHostAddress();

代码示例来源:origin: bwssytems/ha-bridge

  1. Enumeration<NetworkInterface> ifs = null;
  2. InetSocketAddress socketAddress = new InetSocketAddress(Configuration.UPNP_MULTICAST_ADDRESS, Configuration.UPNP_DISCOVERY_PORT);
  3. try {
  4. ifs = NetworkInterface.getNetworkInterfaces();
  5. } catch (SocketException e) {
  6. log.error("Could not get network interfaces for this machine: " + e.getMessage());
  7. return false;
  8. while (addrs.hasMoreElements()) {
  9. if (InetAddressUtils.isIPv4Address(addr.getHostAddress())) {
  10. if(!useUpnpIface) {
  11. if(traceupnp)
  12. IPsPerNic++;
  13. else if(addr.getHostAddress().equals(responseAddress)) {
  14. if(traceupnp)
  15. log.info("Traceupnp: Interface: " + name + " matches upnp config address of IP address: " + addr);
  16. log.debug("Adding " + name + " to our interface set");
  17. } catch (IOException e) {
  18. log.warn("Multicast join failed for: " + socketAddress.getHostName() + " to interface: "
  19. sendUpnpNotify(socketAddress.getAddress());
  20. previous = Instant.now();

代码示例来源:origin: apache/zookeeper

  1. String bindAddress = null;
  2. Enumeration<NetworkInterface> intfs =
  3. NetworkInterface.getNetworkInterfaces();
  4. while(intfs.hasMoreElements()) {
  5. NetworkInterface i = intfs.nextElement();
  6. try {
  7. if (i.isLoopback()) {
  8. Enumeration<InetAddress> addrs = i.getInetAddresses();
  9. while (addrs.hasMoreElements()) {
  10. InetAddress a = addrs.nextElement();
  11. if(a.isLoopbackAddress()) {
  12. bindAddress = a.getHostAddress();
  13. if (a instanceof Inet6Address) {
  14. bindAddress = "[" + bindAddress + "]";
  15. LOG.warn("Couldn't find loopback interface: " + se.getMessage());
  16. new InetSocketAddress(bindAddress, PORT), -1);
  17. f.startup(zks);
  18. LOG.info("starting up the the server, waiting");

代码示例来源:origin: bwssytems/ha-bridge

  1. private String checkIpAddress(String ipAddress, boolean checkForLocalhost) {
  2. Enumeration<NetworkInterface> ifs = null;
  3. try {
  4. ifs = NetworkInterface.getNetworkInterfaces();
  5. } catch(SocketException e) {
  6. log.error("checkIpAddress cannot get ip address of this host, Exiting with message: " + e.getMessage(), e);
  7. return null;
  8. while (ifs.hasMoreElements() && addressString == null) {
  9. NetworkInterface xface = ifs.nextElement();
  10. Enumeration<InetAddress> addrs = xface.getInetAddresses();
  11. String name = xface.getName();
  12. int IPsPerNic = 0;
  13. while (addrs.hasMoreElements() && IPsPerNic == 0) {
  14. address = addrs.nextElement();
  15. if (InetAddressUtils.isIPv4Address(address.getHostAddress())) {
  16. log.debug(name + " ... has IPV4 addr " + address);
  17. if(checkForLocalhost && (!name.equalsIgnoreCase(Configuration.LOOP_BACK_INTERFACE) || !address.getHostAddress().equalsIgnoreCase(Configuration.LOOP_BACK_ADDRESS))) {
  18. IPsPerNic++;
  19. addressString = address.getHostAddress();
  20. log.debug("checkIpAddress found " + addressString + " from interface " + name);

代码示例来源:origin: eclipse/smarthome

  1. private @Nullable String getFirstLocalIPv4Address() {
  2. try {
  3. String hostAddress = null;
  4. final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
  5. while (interfaces.hasMoreElements()) {
  6. final NetworkInterface current = interfaces.nextElement();
  7. if (!current.isUp() || current.isLoopback() || current.isVirtual() || current.isPointToPoint()) {
  8. continue;
  9. }
  10. final Enumeration<InetAddress> addresses = current.getInetAddresses();
  11. while (addresses.hasMoreElements()) {
  12. final InetAddress currentAddr = addresses.nextElement();
  13. if (currentAddr.isLoopbackAddress() || (currentAddr instanceof Inet6Address)) {
  14. continue;
  15. }
  16. if (hostAddress != null) {
  17. LOGGER.warn("Found multiple local interfaces - ignoring {}", currentAddr.getHostAddress());
  18. } else {
  19. hostAddress = currentAddr.getHostAddress();
  20. }
  21. }
  22. }
  23. return hostAddress;
  24. } catch (SocketException ex) {
  25. LOGGER.error("Could not retrieve network interface: {}", ex.getMessage(), ex);
  26. return null;
  27. }
  28. }

代码示例来源:origin: eclipse/smarthome

  1. /**
  2. * Get all broadcast addresses on the current host
  3. *
  4. * @return list of broadcast addresses, empty list if no broadcast addresses found
  5. */
  6. public static List<String> getAllBroadcastAddresses() {
  7. List<String> broadcastAddresses = new LinkedList<String>();
  8. try {
  9. final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
  10. while (networkInterfaces.hasMoreElements()) {
  11. final NetworkInterface networkInterface = networkInterfaces.nextElement();
  12. final List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
  13. for (InterfaceAddress interfaceAddress : interfaceAddresses) {
  14. final InetAddress addr = interfaceAddress.getAddress();
  15. if (addr instanceof Inet4Address && !addr.isLinkLocalAddress() && !addr.isLoopbackAddress()) {
  16. InetAddress broadcast = interfaceAddress.getBroadcast();
  17. if (broadcast != null) {
  18. broadcastAddresses.add(broadcast.getHostAddress());
  19. }
  20. }
  21. }
  22. }
  23. } catch (SocketException ex) {
  24. LOGGER.error("Could not find broadcast address: {}", ex.getMessage(), ex);
  25. }
  26. return broadcastAddresses;
  27. }

代码示例来源:origin: ac-pm/Inspeckage

  1. public void loadInterfaces(){
  2. StringBuilder sb = new StringBuilder();
  3. sb.append("All interfaces,");
  4. try {
  5. for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
  6. NetworkInterface netInterface = en.nextElement();
  7. for (Enumeration<InetAddress> enumIpAddr = netInterface.getInetAddresses(); enumIpAddr.hasMoreElements();) {
  8. InetAddress inetAddress = enumIpAddr.nextElement();
  9. String address = inetAddress.getHostAddress();
  10. boolean isIPv4 = address.indexOf(':') < 0;
  11. if (isIPv4) {
  12. sb.append(address+",");
  13. }
  14. }
  15. }
  16. } catch (SocketException ex) {
  17. Log.e("Inspeckage_Error", ex.toString());
  18. }
  19. SharedPreferences.Editor edit = mPrefs.edit();
  20. edit.putString(Config.SP_SERVER_INTERFACES, sb.toString().substring(0,sb.length()-1));
  21. edit.apply();
  22. }
  23. @Override

代码示例来源:origin: sixt/ja-micro

  1. private void updateIpAddress() {
  2. try {
  3. Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
  4. ipAddress = null;
  5. while( b.hasMoreElements()){
  6. NetworkInterface iface = b.nextElement();
  7. if (iface.getName().startsWith("dock")) {
  8. continue;
  9. }
  10. for ( InterfaceAddress f : iface.getInterfaceAddresses()) {
  11. if (f.getAddress().isSiteLocalAddress()) {
  12. ipAddress = f.getAddress().getHostAddress();
  13. }
  14. }
  15. }
  16. } catch (SocketException e) {
  17. e.printStackTrace();
  18. }
  19. }

代码示例来源:origin: yuliskov/SmartYouTubeTV

  1. private boolean testIPv4() {
  2. try {
  3. Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
  4. while (networks.hasMoreElements()) {
  5. NetworkInterface network = networks.nextElement();
  6. for (InterfaceAddress address : network.getInterfaceAddresses()) {
  7. if (address.getAddress() instanceof Inet4Address) {
  8. // found IPv4 address
  9. // do any other validation of address you may need here
  10. return true;
  11. }
  12. }
  13. }
  14. return false;
  15. } catch (SocketException e) {
  16. e.printStackTrace();
  17. throw new IllegalStateException(e);
  18. }
  19. }
  20. }

代码示例来源:origin: robovm/robovm

  1. private void checkJoinOrLeave(SocketAddress groupAddress, NetworkInterface netInterface) throws IOException {
  2. checkOpen();
  3. if (groupAddress == null) {
  4. throw new IllegalArgumentException("groupAddress == null");
  5. }
  6. if (netInterface != null && !netInterface.getInetAddresses().hasMoreElements()) {
  7. throw new SocketException("No address associated with interface: " + netInterface);
  8. }
  9. if (!(groupAddress instanceof InetSocketAddress)) {
  10. throw new IllegalArgumentException("Group address not an InetSocketAddress: " +
  11. groupAddress.getClass());
  12. }
  13. InetAddress groupAddr = ((InetSocketAddress) groupAddress).getAddress();
  14. if (groupAddr == null) {
  15. throw new SocketException("Group address has no address: " + groupAddress);
  16. }
  17. if (!groupAddr.isMulticastAddress()) {
  18. throw new IOException("Not a multicast group: " + groupAddr);
  19. }
  20. }

代码示例来源:origin: alibaba/TProfiler

  1. public void run() {
  2. try {
  3. socket = new ServerSocket(Manager.PORT);
  4. while (true) {
  5. Socket child = socket.accept();
  6. child.setSoTimeout(5000);
  7. String command = read(child.getInputStream());
  8. write(child.getOutputStream());
  9. } else if (Manager.FLUSHMETHOD.equals(command)) {
  10. MethodCache.flushMethodData();
  11. e.printStackTrace();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. if (socket != null) {
  15. try {
  16. socket.close();
  17. } catch (IOException e) {
  18. e.printStackTrace();

代码示例来源:origin: qiujuer/Genius-Android

  1. @Override
  2. public void start() {
  3. Socket socket = null;
  4. try {
  5. Long startTime = System.currentTimeMillis();
  6. socket = new Socket();
  7. try {
  8. socket.setSoTimeout(TIME_OUT);
  9. } catch (SocketException e) {
  10. e.printStackTrace();
  11. }
  12. socket.connect(new InetSocketAddress(mHost, mPort), TIME_OUT);
  13. if (isConnected = socket.isConnected())
  14. mDelay = System.currentTimeMillis() - startTime;
  15. else
  16. mError = Cmd.TCP_LINK_ERROR;
  17. } catch (UnknownHostException e) {
  18. mError = Cmd.UNKNOWN_HOST_ERROR;
  19. } catch (IOException e) {
  20. mError = Cmd.TCP_LINK_ERROR;
  21. } finally {
  22. if (socket != null) {
  23. try {
  24. socket.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. }

代码示例来源:origin: square/okhttp

  1. socketPort = proxySocketAddress.getPort();
  2. throw new SocketException("No route to " + socketHost + ":" + socketPort
  3. + "; port is out of range");
  4. inetSocketAddresses.add(InetSocketAddress.createUnresolved(socketHost, socketPort));
  5. } else {
  6. eventListener.dnsStart(call, socketHost);
  7. inetSocketAddresses.add(new InetSocketAddress(inetAddress, socketPort));

代码示例来源:origin: zstackio/zstack

  1. Socket socket = null;
  2. socket = new Socket();
  3. try {
  4. socket.setReuseAddress(true);
  5. SocketAddress sa = new InetSocketAddress(ip, port);
  6. socket.connect(sa, timeout);
  7. return socket.isConnected();
  8. } catch (SocketException e) {
  9. logger.debug(String.format("unable to connect remote port[ip:%s, port:%s], %s", ip, port, e.getMessage()));
  10. return false;
  11. } catch (IOException e) {

代码示例来源:origin: android-hacker/VirtualXposed

  1. private static IPInfo getIPInfo() {
  2. try {
  3. List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
  4. for (NetworkInterface intf : interfaces) {
  5. List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
  6. for (InetAddress addr : addrs) {
  7. if (!addr.isLoopbackAddress()) {
  8. String sAddr = addr.getHostAddress().toUpperCase();
  9. boolean isIPv4 = isIPv4Address(sAddr);
  10. if (isIPv4) {
  11. IPInfo info = new IPInfo();
  12. info.addr = addr;
  13. info.intf = intf;
  14. info.ip = sAddr;
  15. info.ip_hex = InetAddress_to_hex(addr);
  16. info.netmask_hex = netmask_to_hex(intf.getInterfaceAddresses().get(0).getNetworkPrefixLength());
  17. return info;
  18. }
  19. }
  20. }
  21. }
  22. } catch (SocketException e) {
  23. e.printStackTrace();
  24. }
  25. return null;
  26. }

代码示例来源:origin: apache/incubator-gobblin

  1. @Override
  2. void handleClientSocket(Socket clientSocket) throws IOException {
  3. LOG.info("Writing to client");
  4. BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  5. PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
  6. try {
  7. out.println("Hello");
  8. out.flush();
  9. String line = in.readLine();
  10. while (line != null && isServerRunning()) {
  11. out.println(line + " " + line);
  12. out.flush();
  13. line = in.readLine();
  14. }
  15. }
  16. catch (SocketException se) {
  17. // don't bring down server when client disconnected abruptly
  18. if (!se.getMessage().contains("Connection reset")) {
  19. throw se;
  20. }
  21. }
  22. finally {
  23. clientSocket.close();
  24. }
  25. }
  26. }

相关文章