Java之InetSocketAddress和SocketAddress的區別
兩者區別:SocketAddress是一個抽象類,而InetSocketAddress 是SocketAddress的子類。
示例:
InetAddress address = InetAddress.getLocalHost();
String hostName = address.getHostName();
String hostAddr = address.getHostAddress();
System.out.println("主機名:" + hostNmae + ", ip地址:" + hostAddr;
InetAddress add = InetAddress.getByName("BOPZKQZ9SSY5ECY");
System.out.println(add.getHostAddress());
final Socket socket = new Socket();
SocketAddress address = new InetSocketAddress("www.fortify.net", 443);
try {
socket.connect(address);
} catch (IOException e) {}
// 連線遠端主機
Thread reader = new Thread() {
@Override
public void run() {
try {
byte[] buffer = new byte[512];
InputStream stream = socket.getInputStream();
socket.getInputStream().read(buffer);
} catch (Exception ex) {
}
}
};
reader.start();
相關文章
- Java之for(;;)和while(true)的區別JavaWhile
- Java之.class和.getClass()的區別Java
- Java之try-catch和throws的區別Java
- java複習之HashMap和Hashtable的區別JavaHashMap
- 【Java】equals 和 == 的區別Java
- Java和Javascript的區別JavaScript
- java和html的區別JavaHTML
- java複習之 Vector、ArrayList和LinkedList 的區別Java
- JAVA常見面試題之Forward和Redirect的區別Java面試題Forward
- JAVA之編譯期和執行期區別Java編譯
- java 中equals和==的區別Java
- java yield()和sleep()的區別Java
- ArrayList 和 Vector 的區別 -JAVAJava
- Java中equals和==的區別Java
- java堆和棧的區別Java
- JavaScript和Java的區別(轉)JavaScript
- java中的==和equal的區別Java
- Java集合面試題(03) Java中HashMap和HashTable之間區別Java面試題HashMap
- Java中Vector和ArrayList的區別Java
- 【Java面試】@Resource 和 @Autowired 的區別Java面試
- Java中 equals() 方法和 == 的區別Java
- 對於java中的"\"和"/" 區別Java
- 在java中“equals”和“==”的區別Java
- Java中throw和throws的區別Java
- Java中堆和棧的區別Java
- 敏捷和 Scrum 之間的區別敏捷Scrum
- Statement和PreparedStatement之間的區別
- java中==和equlas區別Java
- Python之“==”和“is”區別Python
- PHP, Python和Java的區別分享PHPPythonJava
- JAVA中介面和抽象類的區別Java抽象
- Java中Runnable和Thread的區別Javathread
- Java中空串和null串的區別JavaNull
- C++和java多型的區別C++Java多型
- Java NIO 和 IO 的區別詳解Java
- Java 中Thread 和Runnable的區別Javathread
- Java引數-Xms和-Xmx的區別Java
- JAVA 裡static 和volatile的區別Java