Java Networking: InetAddress

The InetAddress is Java‘s representation of an IP address. Instances of this class are used together with UDP DatagramSockets and normal Socket‘s and ServerSocket‘s.

Creating an InetAddress Instance

InetAddress has no public contructor, so you must obtain instances via a set of static methods.

Here is how to get the InetAddress instance for a domain name:

InetAddress address = InetAddress.getByName("jenkov.com");

And here is how to get the InetAddress matching a String representation of an IP address:

InetAddress address = InetAddress.getByName("78.46.84.171");

And, here is how to obtain the IP address of the localhost (the computer the program is running on):

InetAddress address = InetAddress.getLocalHost();

Additional InetAddress Methods

The InetAddress class has a lot of additional methods you can use. For instance, you can obtain the IP address as a byte array by calling getAddress() etc. To learn more about these methods, it is easier to read the JavaDoc for the InetAddress class though.

时间: 2024-10-17 07:40:10

Java Networking: InetAddress的相关文章

Java Networking

1 Java Networking 2 Java Networking: Socket 3 Java Networking: ServerSocket 4 Java Networking: UDP DatagramSocket 5 Java Networking: URL + URLConnection 6 Java Networking: JarURLConnection 7 Java Networking: InetAddress 8 Java Networking: Protocol De

Java Networking: Socket

Java Networking 1 Java Networking 2 Java Networking: Socket 3 Java Networking: ServerSocket 4 Java Networking: UDP DatagramSocket 5 Java Networking: URL + URLConnection 6 Java Networking: JarURLConnection 7 Java Networking: InetAddress 8 Java Network

Java Networking: UDP DatagramSocket (翻译)

原文:http://tutorials.jenkov.com/java-networking/udp-datagram-sockets.html UDP vs. TCP Sending Data via a DatagramSocket Receiving Data via a DatagramSocket DatagramSocket's are Java's mechanism for network communication via UDP instead of TCP. UDP is

Android(java)学习笔记79:java中InetAddress类概述和使用

要想让网络中的计算机能够互相通信,必须为每台计算机指定一个标识号,通过这个标识号来指定要接受数据的计算机和识别发送的计算机. 在TCP/IP协议中,这个标识号就是IP地址. 那么,我们如果获取和操作IP地址呢? 为了方便我们对IP地址的获取和操作,java提供了一个类InetAddress 供我们使用. 1 package cn.itcast_01; 2 3 import java.net.InetAddress; 4 import java.net.UnknownHostException;

Java Socket应用(二)——java中InetAddress的应用

转载请注明:http://blog.csdn.net/uniquewonderq 1.InetAddress类 用于标识网络上的硬件资源,实际主要用来表示IP地址的相关信息. 关于该类的方法可以在API帮助文档查看. 要获取InetAdress类的实例,由于它没有构造方法,但是它有静态方法可以返回该类的实例,所以,可以这样子来获取. 下面简单举一个例子: package com.test; import java.net.InetAddress; import java.net.UnknownH

Java Networking and Proxies(译文)

 JavaNetworking and Proxies 比较早的文章,正好在研究java proxy的用法,就翻译了一下 原文地址: http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html 概述 在如今的网络环境下,尤其是合作项目,项目开发者不得不频繁的处理代理问题. 有时候项目使用系统默认参数即可,但有些情况下,希望能够详密掌控代理的使用情况. 很多项目会提供用户GUI来自己设定代理参数. 无论如何,一个

Java Networking Related (Java Examples in a Nutshell 3rd Edition)

Examples to: Use URL class to parse URLs and download the network resources specified by a URL Use URLConnection class to gain more cntrl over the downloading of network resources Write client programs that use the Socket class to communicate over th

at java.net.InetAddress.getLocalHost(InetAddress.java:1475)

今天在centos 安装hadoop安装完成后执行wordcount的时候报如下错误: 解决方法:vim /etc/hosts 再最下面追加一行:ip  hostname 比如我的机器名为:lvcentos 那就在下面追加:127.0.0.1    lvcentos

Java动态替换InetAddress中DNS的做法简单分析1

在java.net包描述中, 简要说明了一些关键的接口. 其中负责networking identifiers的是Addresses. 这个类的具体实现类是InetAddress, 底层封装了Inet4Address与Inet6Address的异同, 可以看成一个Facade工具类. A Low Level API, which deals with the following abstractions: Addresses, which are networking identifiers,