InerAddress:
IP地址:在网络上唯一标示一台计算机
- 端口号:标示计算机上不同的应用程序
- java.net.InetAddress类:此类表示互联网协议 (IP) 地址。
- 常用方法:
- getByName(String host) 在给定主机名的情况下确定主机的 IP 地址。
- getHostName() 获取此 IP地址的主机名。
- getHostAddress()返回 IP 地址字符串(以文本表现形式)。
- getLocalHost() 返回本地主机。
- getAllByName(String host) 在给定主机名的情况下,根据系统上配置的名称服务返回其 IP 地址所组成的数组。
public class TestInetAddress {
public static void main(String[] args) {
try {
//在给定主机名的情况下确定主机的 IP 地址。
// InetAddress inetAddress = InetAddress.getByName("P-PC");
InetAddress inetAddress = InetAddress.getLocalHost();//获取本地主机
System.out.println(inetAddress);
String hostName = inetAddress.getHostName();
System.out.println("主机名:"+hostName);
String ip = inetAddress.getHostAddress();
System.out.println("IP地址:"+ip);
//根据主机名或域名获取其IP地址
InetAddress[] ids = InetAddress.getAllByName("www.baidu.com");
for (InetAddress inetAddress2 : ids) {
System.out.println(inetAddress2);
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
InetSocketAddress:
- java.net.InetSocketAddress类:此类实现 IP 套接字地址(IP 地址 + 端口号)。
- 构造方法
- InetSocketAddress(InetAddress addr, int port)根据 IP 地址和端口号创建套接字地址。
- InetSocketAddress(String hostname, int port) 根据主机名和端口号创建套接字地址。
- 常用的方法:
- getAddress():获取 InetAddress。
- getPort() 获取端口号。
- toString() 构造此 InetSocketAddress 的字符串表示形式。(主机名/Ip:端口号)
- getHostName()获取 主机名。
public class TestInetSocketAddress {
public static void main(String[] args) {
// InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",3306);
try {
InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 3306);
System.out.println(socketAddress);
InetAddress inetAddress = socketAddress.getAddress();
System.out.println("主机信息:"+inetAddress);
int port = socketAddress.getPort();
System.out.println("端口号:"+port);
String hostName = socketAddress.getHostName();
System.out.println("主机名:"+hostName);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
URL:
- URL:统一资源定位符
- 组成部分:协议,主机名或IP,端口号,资源路径
- java.net.URL类:代表一个统一资源定位符,它是指向互联网“资源”的指针
- 常用的构造方法
- URL(String spec) 根据 String 表示形式创建 URL 对象。
- URL(String protocol, String host, int port, String file) 根据指定 protocol、host、port 号和 file 创建 URL 对象。
- 常用的方法:
- getProtocol()获取此 URL 的协议名称。
- getHost()获取此 URL 的主机名(如果适用)。
- getPort() 获取此 URL 的端口号。
- getFile()获取此 URL 的文件名。
- getDefaultPort()获取与此 URL 关联协议的默认端口号。
- getPath()获取此 URL 的路径部分。
public class TestURL {
public static void main(String[] args) {
try {
URL url = new URL("http://www.baidu.com/index.html#aa?cansu=bjsxt");
String protocol = url.getProtocol();
System.out.println("协议:"+protocol);
String host = url.getHost();
System.out.println("主机名:"+host);
int port = url.getPort();
System.out.println("端口号:"+port);
int defualtPort = url.getDefaultPort();
System.out.println("默认端口:"+defualtPort);
String file = url.getFile();
System.out.println("资源路径:"+file);
String path = url.getPath();
System.out.println("资源路径:"+path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
URL类
- InputStream openStream() 打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class WebSpider {
public static void main(String[] args) {
try {
URL url = new URL("https://channel.jd.com/men.html");
InputStream ips = url.openStream();
InputStreamReader isr = new InputStreamReader(ips);//将字节流转换为字符流
BufferedReader br = new BufferedReader(isr);
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TCP与UDP的区别:
- TCP与UDP基本区别
- 基于连接与无连接,TCP是面向连接,UDP是不面向连接;
- UDP程序结构较简单,TCP点到点的通信,UDP可以广播发送;
- 流模式(TCP)与数据报模式(UDP);
-
UDP应用场景:
- 面向数据报方式
- 网络数据大多为短消息
- 拥有大量Client
- 对数据安全性无特殊要求
- 网络负担非常重,但对响应速度要求高
-
具体编程时的区别
socket()的参数不同
UDP Server不需要调用listen和accept
UDP收发数据用sendto/recvfrom函数
TCP:地址信息在connect/accept时确定
UDP:在sendto/recvfrom函数中每次均 需指定地址信息
UDP:shutdown函数无效