概述:
开始上硬菜的了,现在我们可以学习如何编写自己的套接字应用程序了。我们首先通过使用InetAddress类和SocketAddress类来示范Java应用程序如何识别网络主机。然后,举了一个使用TCP协议的客户端和服务器端例子来展示Socket类和ServerSocket类的用法。同样,我们举了一个使用UDP协议的客户端和服务器端例子来展示DatagramSocket类的用法。对于每个类对应的网络抽象,列举出了各个类中最重要的方法,根据这些方法的不同用途进行了分组,并简要描述了它们的功能
2.1 套接字地址
在java中提供地址类InetAddress 其下有两个子类,Inet4Address 和 Inet6Address两个类对应IPv4 和IPv6
例子:获取本机的地址
package com.tcp.ip.chapter2;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
/**
* 获取本地地址
* @author Administrator
*
*/
public class InetAddressExample {
public static void main(String[] args){
try {
//得到网络接口和关联这个主机的地址
Enumeration<NetworkInterface> interfaceList = NetworkInterface.getNetworkInterfaces();
if(interfaceList == null) {
System.out.println("------没有接口发现----");
} else {
int count=1;
//遍历得到接口
while (interfaceList.hasMoreElements()) {
NetworkInterface iface = interfaceList.nextElement();
//打印接口的名字
System.out.println("接口"+(count++)+"的名称为:" + iface.getName());
//获取地址
Enumeration<InetAddress> addrList = iface.getInetAddresses();
if(!addrList.hasMoreElements()) {
//打印对于这个接口没有地址
System.out.println("对于这个接口没有地址");
} else {
while (addrList.hasMoreElements()) {
InetAddress address = addrList.nextElement();
System.out.println("这个地址是:" +(address instanceof Inet4Address ? "(v4)"
:(address instanceof Inet6Address ? "(v6)" :"未知")));
System.out.println(": " + address.getHostAddress());
}
}
}
}
} catch (SocketException e) {
System.out.println("得到网络接口异常");
e.printStackTrace();
}
//从给入参数获取名称或者地址
for (String host : args){
System.out.println(host + ":");
try {
InetAddress[] addressList = InetAddress.getAllByName(host);
for (InetAddress address : addressList) {
System.out.println("\t" + address.getHostName()
+ "/" + address.getHostAddress());
}
} catch (UnknownHostException e) {
System.out.println("不能找到对应的地址:" + host);
e.printStackTrace();
}
}
}
}
效果:
接口1的名称为:lo
这个地址是:(v4)
: 127.0.0.1
这个地址是:(v6)
: 0:0:0:0:0:0:0:1
接口2的名称为:net0
对于这个接口没有地址
接口3的名称为:net1
对于这个接口没有地址
接口4的名称为:net2
对于这个接口没有地址
接口5的名称为:ppp0
对于这个接口没有地址
接口6的名称为:eth0
对于这个接口没有地址
接口7的名称为:eth1
对于这个接口没有地址
接口8的名称为:eth2
对于这个接口没有地址
接口9的名称为:ppp1
对于这个接口没有地址
接口10的名称为:net3
对于这个接口没有地址
接口11的名称为:eth3
这个地址是:(v4)
: 192.168.9.157
这个地址是:(v6)
: fe80:0:0:0:3029:dc45:54ae:1e3d%eth3
接口12的名称为:net4
这个地址是:(v6)
: fe80:0:0:0:0:5efe:c0a8:99d%net4
接口13的名称为:net5
这个地址是:(v6)
: 2001:0:9d38:6ab8:83a:ea3:3f57:f662
这个地址是:(v6)
: fe80:0:0:0:83a:ea3:3f57:f662%net5
接口14的名称为:eth4
对于这个接口没有地址
接口15的名称为:eth5
对于这个接口没有地址
接口16的名称为:eth6
对于这个接口没有地址
接口17的名称为:eth7
对于这个接口没有地址
接口18的名称为:eth8
对于这个接口没有地址
www.mkp.com:
www.mkp.com/207.24.42.235
www.mkp.com/129.35.69.7:
不能找到对应的地址:www.mkp.com/129.35.69.7
java.net.UnknownHostException: www.mkp.com/129.35.69.7
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at com.tcp.ip.chapter2.InetAddressExample.main(InetAddressExample.java:56)
总结:
1.理清思路--一个主机有多个接口,而每个接口会关联0个以上地址
2.NetworkInterface(获取网络接口) 其实它本是一个类
3.InetAddress 得到地址
4.IPv6的本地链接地址由fe8开头。