Creating InetAddress object in Java

I am trying to convert an address specified by an IP number or a name, both in String (i.e. localhost or 127.0.0.1), into an InetAdress object. There‘s no constructor but rather static methods that return an InetAddress. So if I get a host name it‘s not a problem, but what if I get the IP number? There‘s one method that getsbyte[] but I‘m not sure how that can help me. All other methods gets the host name.

You should be able to use getByName or getByAddress.

The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address

InetAddress addr = InetAddress.getByName("127.0.0.1");

The method that takes a byte array can be used like this:

byte[] ipAddr = new byte[]{127, 0, 0, 1};
InetAddress addr = InetAddress.getByAddress(ipAddr)
时间: 2024-10-13 04:58:21

Creating InetAddress object in Java的相关文章

Different ways to create an object in Java

Different ways to create an object in Java You must have used the “new” operator to create an Object of a Class. But is it the only way to create an Object? Simple Answers is NO, then in how many ways we can create Object of a Class. There are severa

NET:Error Creating Control -"Object Reference Not Set To An Instance Of Object"

这几天,竟遇见些奇葩问题,有的实在懒的写了,这个比较有意思,以前没见过,写个文章记录下: Error Creating Control -"Object Reference Not Set To An Instance Of Object" 在winforms页面中,所有page的所有服务器控件的位置出现一行红字"Error Creating Control - Object reference not set to an instance of an object"

Spark - A tiny Sinatra inspired framework for creating web applications in Java 8 with minimal effor

Spark - A tiny Sinatra inspired framework for creating web applications in Java 8 with minimal effort Quick start import static spark.Spark.*; public class HelloWorld { public static void main(String[] args) { get("/hello", (req, res) -> &quo

hp警告Creating default object from empty value 问题的解决方法

hp警告Creating default object from empty value 问题的解决方法 解决方法是找到报错的位置然后看哪个变量是没有初始化而直接使用的,将这个变量先实例化一个空类.如: 复制代码 代码如下: $ct = new stdClass(); 修改文件相应代码,如: 复制代码 代码如下: if ( ! isset( $themes[$current_theme] ) ) { delete_option( 'current_theme' ); $current_theme

Warning Creating default object from empty value in xxx.php

PHP 提示 Creating default object from empty value 的问题,一般是由于PHP版升级的原因,PHP 5.4 以上的版本一般会报这个错误: 我的解决方法:找到报错的地方,初始化对象 $_obj = new stdClass(); 同理,数组也需要初始化 $_arr = array(); 变量则不需要(我测试过). Warning Creating default object from empty value in xxx.php

Creating default object from empty value in PHP?

  Your new environment may have E_STRICT warnings enabled in error_reporting if it is PHP <= 5.3, or if simply have error_reporting set to at least E_WARNING with PHP 5.4+. . That error is triggered when $res is NULL or not yet initialized: $res = NU

Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 不支持的特性

mybatis插入数据时报错: Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 不支持的特性 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFacto

将Object转换成List&lt;?&gt;,避免Unchecked cast: &#39;java.lang.Object&#39; to &#39;java.util.List&lt;java.lang.String&gt;&#39;

java中如果需要将一个object转成list,大部分人会直接使用强制类型转换:(List<String>) obj这样.这样强制转换编译会提示Unchecked cast: 'java.lang.Object' to 'java.util.List<java.lang.String>',编译器的意思该强制类型转换并未做类型校验,强制转换并不安全,可能会抛出异常导致程序崩溃.在很多博客中有人会建议用@SuppressWarnings("unchecked")解

Android 执行 gson.toJson(object) 报java.lang.StackOverflowError异常

如下面的代码,执行后报java.lang.StackOverflowError错误: MusicSavedInfo musicSavedInfo=new MusicSavedInfo(currentStartTime,openOrCloseFlag,currentDayFlags,currentSelectMusicTitle,currentSelectMusicPath,"1"); String jsonData=gson.toJson(musicSavedInfo); 我手机4.1