☆网络通讯要素
IP地址:InetAddress
网络中设备的标识不易记忆,可用主机名
本地回环地址:127.0.0.1 主机名:localhost
端口号
用于标识进程的逻辑地址,不同进程的标识
有效端口:0~65535,其中0~1024系统使用或保留端口。
传输协议
通讯的规则
常见协议:TCP,UDP
☆网络资源定位指针——URL类
public final class URL implements Serializable{
public URL(String protocol, String host, int port, String file)
throws MalformedURLException
public String toString() //返回完整URL地址字符串
public String getProtocol() //返回协议名
public int getPort() //返回端口
public int getDefaultPort() //返回默认端口
public String getHost() //返回主机名
public String getFile() //返回完整文件名
//使用流获得URL资源内容
public final InputStream openStream() throws IOException
}
☆URL的通信链接——URLConnection类
public abstract class URLConnection{
public URL getURL() //返回当前连接的URL对象
public int getContentLength() //返回资源文件的长度
public String getContentType() //返回资源文件的类型
public long getLastModified() //返回资源文件的最后修改日期
}
URL类的openConnection()方法可创建一个URLConnection对象
public URLConnection openConnection() throws IOException
☆互联网协议(IP)地址——InetAddress类
public class InetAddress implements Serializable{
public static InetAddress getByName(String host)
public static InetAddress getByAddress(String host, byte[] addr)
public static InetAddress getLocalHost() //返回本地主机
public String getHostAddress() //返回IP地址字符串
public String getHostName() //返回主机名
}