网络编程
(一)概述
网络编程概述
- Java是 Internet 上的语言,它从语言级上提供了对网络应用程序的支持,程序员能够很容易开发常见的网络应用程序。
- Java提供的网络类库,可以实现无痛的网络连接,联网的底层细节被隐藏在 Java 的本机安装系统里,由 JVM 进行控制。并且 Java 实现了一个跨平台的网络库,程序员面对的是一个统一的网络编程环境。
网络基础
-
计算机网络:
把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规模大,功能强的网络系统,从而使众多的计算机可以方便地互相传递信息、共享硬件、软件、数据信息等资源。
-
网络编程的目的就是指直接或间接地通过网络协议与其它计算机进行通讯。
-
网络编程中有两个主要的问题:
- 如何准确地定位网络上一台或多台主机
- 找到主机后如何可靠高效地进行数据传输
-
要想让处于网络中的主机互相通信,只是知道通信双方地址还是不够的,还必须遵循一定的规则。有两套参考模型:
- OSI参考模型:模型过于理想化,未能在因特网上进行广泛推广
- TCP/IP参考模型(或TCP/IP协议):事实上的国际标准。
网络通信协议
IP和端口号
- IP 地址标识 Internet 上的计算机,端口号标识正在计算机上运行的进程(程序)。
- 端口号与IP地址的组合得出一个网络套接字。
- 端口号被规定为一个 16 位的整数 0 ~ 65535。其中,0 ~ 1023被预先定义的服务通信占用(如telnet占用端口23,http占用端口80等)。除非我们需要访问这些特定服务,否则,就应该使用 1024~65535 这些端口中的某一个进行通信,以免发生端口冲突。
(二)InetAddress
- Internet上的主机有两种方式表示地址:
- 域名(hostName):www.atguigu.com
- IP 地址(hostAddress):202.108.35.210
- InetAddress类主要表示IP地址,两个子类:Inet4Address、Inet6Address。
- InetAddress 类对象含有一个 Internet 主机地址的域名和IP地址:www.atguigu.com 和 202.108.35.210。
- 域名容易记忆,当在连接网络时输入一个主机的域名后,域名服务器(DNS)负责将域名转化成IP地址,这样才能和主机建立连接。 -------域名解析
- InetAddress类没有提供公共的构造器,而是提供了两个静态方法来获取InetAddress实例
- getByName(String host):在给定主机名的情况下确定主机的IP地址
- getByAddress(byte[] addr):在给定原始IP地址的情况下,返回InetAddress对象
- InetAddress提供了如下几个常用的方法
- getHostAddress():返回IP地址字符串(以文本表现形式)
- getHostName():获取此IP地址的主机名
- getLocalHost():返回本地主机
- isReachable(int timeout):测试是否可以达到该地址
- getCanonicalHostName():获取此IP地址的完全限定域名
/**
* 网络通信的第一个要素:IP地址,通过IP地址,唯一的定位互联网上的一台主机
* InetAddress:位于java.net包
* 1.InetAddress用来代表IP地址,一个InetAddress的对象就代表着一个IP地址
* 2.创建InetAddress的对象:InetAddress.getByName(String host);
* 3.getHostName():获取IP地址对应的域名
* getHostAddress():获取IP地址
*/
public class TestInetAddress {
public static void main(String[] args) throws UnknownHostException {
//创建一个InetAddress对象:getByName()
InetAddress inetAddress = InetAddress.getByName("www.atguigu.com");
System.out.println(inetAddress);
System.out.println(inetAddress.getHostName());
System.out.println(inetAddress.getHostAddress());
System.out.println("------------------------");
//获取本机的IP:getLocalHost()
InetAddress inetAddress1 = InetAddress.getLocalHost();
System.out.println(inetAddress1);
System.out.println(inetAddress1.getHostName());
System.out.println(inetAddress1.getHostAddress());
}
}
---------运行结果---------
www.atguigu.com/59.63.247.230
www.atguigu.com
59.63.247.230
------------------------
rzh/192.168.116.1
rzh
192.168.116.1
(三)网络通信协议
-
网络通信协议
计算机网络中实现通信必须有一些约定,即通信协议,对速率、传输代码、代码结构、传输控制步骤、出错控制等制定标准。
-
**通信协议分层的思想 **
由于结点之间联系很复杂,在制定协议时,把复杂成份分解成一些简单的成份,再将它们复合起来。最常用的复合方式是层次方式,即同层间可以通信、上一层可以调用下一层,而与再下一层不发生关系。各层互不影响,利于系统的开发和扩展。
TCP/IP协议簇
- 传输层协议中有两个非常重要的协议:
- 传输控制协议TCP(Transmission Control Protocol)
- 用户数据报协议UDP(User Datagram Protocol)。
- TCP/IP 以其两个主要协议:传输控制协议(TCP)和网络互联协议(IP)而得名,实际上是一组协议,包括多个具有不同功能且互为关联的协议。
- IP(Internet Protocol)协议是网络层的主要协议,支持网间互连的数据通信。
- TCP/IP协议模型从更实用的角度出发,形成了高效的四层体系结构,即物理链路层、IP层、传输层和应用层。
TCP 和 UDP
- TCP协议:
- 使用TCP协议前,须先建立TCP连接,形成传输数据通道
- 传输前,采用“三次握手”方式,是可靠的
- TCP协议进行通信的两个应用进程:客户端、服务端
- 在连接中可进行大数据量的传输
- 传输完毕,需释放已建立的连接,效率低
- UDP协议:
- 将数据、源、目的封装成数据包,不需要建立连接
- 每个数据报的大小限制在64K内
- 因无需连接,故是不可靠的
- 发送数据结束时无需释放资源,速度快
Socket
- 利用套接字(Socket)开发网络应用程序早已被广泛的采用,以至于成为事实上的标准。
- 通信的两端都要有Socket,是两台机器间通信的端点网络
- 通信其实就是Socket间的通信。
- Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输。
- 一般主动发起通信的应用程序属客户端,等待通信请求的为服务端
(四)TCP网络通信
-
Java语言的基于套接字编程分为服务端编程和客户端编程,其通信模型如图所示:
Socket类的常用方法
方法 | 功能 |
---|---|
InetAddress getLocalAddress() | 返回对方Socket中的IP的InetAddress对象 |
int getLocalPort() | 返回本地Socket中的端口号 |
InetAddress getInetAddress() | 返回对方Socket中IP地址 |
int getPort() | 返回对方Socket中的端口号 |
void close() throws IOException | 关闭Socket,不可在以后的网络连接中使用,除非创建新的套接字 |
InputStream getInputStream() throws IOException | 获取与Socket相关联的字节输入流,用于从Socket中读数据。 |
OutputStream getOutputStream() throws IOException | 获取与Socket相关联的字节输出流,用于向Socket中写数据。 |
ServerSocket类的常用方法
方 法 | 功 能 |
---|---|
Socket accept() throws IOException | 等待客户端的连接请求,返回与该客户端进行通信用的Socket对象 |
void setSoTimeout(int timeout) throws SocketException | 设置accept()方法等待连接的时间为timeout毫秒。若时间已到,还没有客户端连接,则抛出InterruptedIOException异常,accept()方法不再阻塞,该倾听Socket可继续使用。若timeout值为0,则表示accept()永远等待。该方法必须在倾听Socket创建后,在accept()之前调用才有效 |
void close() throws IOException | 关闭监听Socket |
InetAddress getInetAddress() | 返回此服务器套接字的本地地址 |
int getLocalPort() | 返回此套接字在其上监听的端口号 |
SocketAddress getLocalSocketAddress() | 返回此套接字绑定的端点的地址 |
-
客户端Socket的工作过程包含以下四个基本的步骤:
- 创建 Socket:根据指定服务端的 IP 地址或端口号构造 Socket 类对象。若服务器端响应,则建立客户端到服务器的通信线路。若连接失败,会出现异常
- 打开连接到 Socket 的输入/出流: 使用 getInputStream()方法获得输入流,使用 getOutputStream()方法获得输出流,进行数据传输
- 按照一定的协议对 Socket 进行读/写操作:通过输入流读取服务器放入线路的信息(但不能读取自己放入线路的信息),通过输出流将信息写入线程
- 关闭 Socket:断开客户端到服务器的连接,释放线路
-
客户端程序可以使用Socket类创建对象,创建的同时会自动向服务器方发起连接。Socket的构造方法是:
- Socket(String host,int port)throws UnknownHostException,IOException:向服务器(域名是host。端口号为port)发起TCP连接,若成功,则创建Socket对象,否则抛出异常。
- Socket(InetAddress address,int port)throws IOException:根据InetAddress对象所表示的IP地址以及端口号port发起连接。
-
客户端建立socketAtClient对象的过程就是向服务器发出套接字连接请求
-
服务器程序的工作过程包含以下四个基本的步骤:
- 调用 ServerSocket(int port) :创建一个服务器端套接字,并绑定到指定端口上。用于监听客户端的请求。
- 调用 accept():监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字对象。
- 调用 该Socket类对象的 getOutputStream() 和 getInputStream ():获取输出流和输入流,开始网络数据的发送和接收。
- 关闭ServerSocket和Socket对象:客户端访问结束,关闭通信套接字。
-
ServerSocket 对象负责等待客户端请求建立套接字连接,类似邮局某个窗口中的业务员。也就是说,服务器必须事先建立一个等待客户请求建立套接字连接的ServerSocket对象。
-
所谓“接收”客户的套接字请求,就是accept()方法会返回一个 Socket 对象
客户端给服务端发送信息,服务端输出此信息在控制台上
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//客户端给服务端发送信息,服务端输出此信息在控制台上
public class TestTcp {
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
try {
//1.创建一个Socket对象,通过构造器指明服务端的IP地址,以及其接受程序的端口号
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
//2.getOutputStream():发送数据,返回OutputStream对象
outputStream = socket.getOutputStream();
//3.具体的输出过程
outputStream.write("我是客户端,请多关照!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭相应的流和Socket对象
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
try {
//1.创建一个ServerSocket对象,通过构造器指明自身的端口号
serverSocket = new ServerSocket(9090);
//2.调用accept()方法,返回一个Socket对象
socket = serverSocket.accept();
//3.调用Socket对象的getInputStream()获取一个从客户端发送过来的数据流
inputStream = socket.getInputStream();
//4.对获取的输入流进行的操作
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, len));
}
System.out.println("\n收到来自于" + socket.getInetAddress().getHostAddress()
+ "的连接");
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭相应的流,以及Socket、ServerSocket对象
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端给服务端发送信息,服务端输出此信息在控制台上,同时发送反馈信息给客户端
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//客户端给服务端发送信息,服务端输出此信息在控制台上,同时发送反馈信息给客户端
public class TestTcp2 {
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
outputStream = socket.getOutputStream();
outputStream.write("我是客户端,请多关照!".getBytes());
socket.shutdownOutput();//此方法显示的告诉服务端发送完毕
inputStream = socket.getInputStream();
byte[] buf = new byte[60];
int len;
while ((len = inputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(9090);
socket = serverSocket.accept();
inputStream = socket.getInputStream();
byte[] buf = new byte[60];
int len;
while ((len = inputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, len));
}
System.out.println("\n收到来自于" + socket.getInetAddress().getHostAddress()
+ "的连接");
outputStream = socket.getOutputStream();
outputStream.write("我已收到你的信息".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
客户端给服务端发送信息,服务端保存在本地。并返回"发送成功"给客户端,并关闭相应的连接
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//客户端给服务端发送信息,服务端保存在本地。并返回"发送成功"给客户端,并关闭相应的连接
public class TestTcp3 {
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
FileInputStream fileInputStream = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
fileInputStream = new FileInputStream(new File("1.jpg"));
outputStream = socket.getOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = fileInputStream.read(b)) != -1) {
outputStream.write(b, 0, len);
}
socket.shutdownOutput();//此方法显示的告诉服务端发送完毕
inputStream = socket.getInputStream();
byte[] buf = new byte[60];
int len1;
while ((len1 = inputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, len1));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
FileOutputStream fileOutputStream = null;
try {
serverSocket = new ServerSocket(9090);
socket = serverSocket.accept();
inputStream = socket.getInputStream();
fileOutputStream = new FileOutputStream(new File("2.jpg"));
byte[] buf = new byte[60];
int len;
while ((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
outputStream = socket.getOutputStream();
outputStream.write("发送成功".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream !=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(五)UDP网络通信
- 类 DatagramSocket 和 DatagramPacket 实现了基于 UDP 协议网络程序。
- UDP数据报通过数据报套接字 DatagramSocket 发送和接收,系统不保证UDP数据报一定能够安全送到目的地,也不能确定什么时候可以抵达。
- DatagramPacket 对象封装了UDP数据报,在数据报中包含了发送端的IP地址和端口号以及接收端的IP地址和端口号。
- UDP协议中每个数据报都给出了完整的地址信息,因此无须建立发送方和接收方的连接
- 流程:
- DatagramSocket与DatagramPacket
- 建立发送端,接收端
- 建立数据包
- 调用Socket的发送、接收方法
- 关闭Socket
- 发送端与接收端是两个独立的运行程序
UDP编程实现
import org.junit.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class TestUdp {
@Test
public void send() {
try (DatagramSocket datagramSocket = new DatagramSocket()) {
byte[] b = "我是要发送的数据".getBytes();
//创建一个数据报:每个数据报不能大于64K。
//都记录着数据信息,发送端IP、端口号、以及接受端的IP和端口号
DatagramPacket datagramPacket = new DatagramPacket(b, 0, b.length,
InetAddress.getByName("127.0.0.1"), 9090);
datagramSocket.send(datagramPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void receive() {
try (DatagramSocket datagramSocket = new DatagramSocket(9090)) {
byte[] b = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(b, 0, b.length);
datagramSocket.receive(datagramPacket);
String str = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
(六)URL编程
URL(Uniform Resource Locator)
:统一资源定位符,它表示 Internet 上某一资源
的地址。通过 URL 我们可以访问 Internet 上的各种网络资源,比如最常见的 www,ftp 站点。浏览器通过解析给定的 URL 可以在网络上查找相应的文件或其他资源。- URL的基本结构由5部分组成:
- <传输协议>://<主机名>:<端口号>/<文件名>
- 例如: http://192.168.1.100:8080/helloworld/index.jsp
- 为了表示URL,java.net 中实现了类 URL。我们可以通过下面的构造器来初始化一个 URL 对象:
- public URL (String spec):通过一个表示URL地址的字符串可以构造一个URL对象。
- 例如:URL url = new URL ("http://www. atguigu.com/");
- public URL(URL context, String spec):通过基 URL 和相对 URL 构造一个 URL 对象。
- 例如:URL downloadUrl = new URL(url, “download.html")
- public URL(String protocol, String host, String file);
- 例如:new URL("http", "www.atguigu.com", “download. html");
- public URL(String protocol, String host, int port, String file);
- 例如: URL gamelan = new URL("http", "www.atguigu.com", 80, “download.html");
- public URL (String spec):通过一个表示URL地址的字符串可以构造一个URL对象。
- 类URL的构造方法都声明抛出非运行时异常,必须要对这一异常进行处理,通常是用 try-catch 语句进行捕获。
- 一个URL对象生成后,其属性是不能被改变的,但可以通过它给定的方法来获取这些属性:
- public String getProtocol( ) 获取该URL的协议名
- public String getHost( ) 获取该URL的主机名
- public String getPort( ) 获取该URL的端口号
- public String getPath( ) 获取该URL的文件路径
- public String getFile( ) 获取该URL的文件名
- public String getRef( ) 获取该URL在文件中的相对位置
- public String getQuery( ) 获取该URL的查询名
import java.net.URL;
public class Url {
public static void main(String[] args) throws Exception {
URL url = new URL("http://127.0.0.1:8080/examples/hello.txt");
System.out.println("协议:" + url.getProtocol());
System.out.println("主机名:" + url.getHost());
System.out.println("端口号:" + url.getPort());
System.out.println("文件路径:" + url.getPath());
System.out.println("文件名:" + url.getFile());
System.out.println("相对位置:" + url.getRef());
System.out.println("查询名:" + url.getQuery());
}
}
--------运行结果----------
协议:http
主机名:127.0.0.1
端口号:8080
文件路径:/examples/hello.txt
文件名:/examples/hello.txt
相对位置:null
查询名:null
URLConnection
- URL的方法 openStream():能从网络上读取数据
- 若希望输出数据,例如向服务器端的 CGI (公共网关接口-Common Gateway Interface-的简称,是用户浏览器和服务器端的应用程序进行连接的接口)程序发送一些数据,则必须先与URL建立连接,然后才能对其进行读写,此时需要使用 URLConnection 。
- URLConnection:表示到URL所引用的远程对象的连接。当与一个URL建立连接时,首先要在一个 URL 对象上通过方法 openConnection() 生成对应的 URLConnection 对象。如果连接过程失败,将产生IOException.
- URL netchinaren = new URL ("http://www.atguigu.com/index.shtml");
- URLConnectonn u = netchinaren.openConnection( );
- 通过URLConnection对象获取的输入流和输出流,即可以与现有的CGI程序进行交互。
- public Object getContent( ) throws IOException
- public int getContentLength( )
- public String getContentType( )
- public long getDate( )
- public long getLastModified( )
- public InputStream getInputStream( ) throws IOException
- public OutputSteram getOutputStream( ) throws IOException
只将服务器端的资源读取进来,使用openStream()方法
import java.io.*;
import java.net.URL;
//URL:统一资源定位符,一个URL的对象,对应着互联网上的一个资源
//我们可以通过URL的对象调用其相应的方法,将此资源下载
//访问Tomcat服务器上的资源文件并下载
public class TestUrl {
public static void main(String[] args) throws Exception {
//1.创建一个URL对象
URL url = new URL("http://127.0.0.1:8080/examples/hello.txt");
InputStream inputStream = url.openStream();
FileOutputStream fileOutputStream = new FileOutputStream(new File("3.jpg"));
byte[] buf = new byte[60];
int len;
while ((len = inputStream.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}
fileOutputStream.close();
inputStream.close();
}
}
有数据的输入和数据的输出考虑使用URLConnection类
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class TestUrl1 {
public static void main(String[] args) throws Exception {
URL url = new URL("http://127.0.0.1:8080/examples/1.jpg");
URLConnection urlConnection = url.openConnection();
//有数据的输入和数据的输出考虑使用URLConnection类
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(new File("2.jpg"));
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
fileOutputStream.close();
inputStream.close();
}
}
(七)小结
- 位于网络中的计算机具有唯一的IP地址,这样不同的主机可以互相区分。
- 客户端-服务器是一种最常见的网络应用程序模型。服务器是一个为其客户端提供某种特定服务的硬件或软件。客户机是一个用户应用程序,用于访问某台服务器提供的服务。端口号是对一个服务的访问场所,它用于区分同一物理计算机上的多个服务。套接字用于连接客户端和服务器,客户端和服务器之间的每个通信会话使用一个不同的套接字。TCP协议用于实现面向连接的会话。
- Java 中有关网络方面的功能都定义在 java.net 程序包中。Java 用 InetAddress 对象表示 IP 地址,该对象里有两个字段:主机名(String) 和 IP 地址(int)。
- 类 Socket 和 ServerSocket 实现了基于TCP协议的客户端-服务器程序。Socket是客户端和服务器之间的一个连接,连接创建的细节被隐藏了。这个连接提供了一个安全的数据传输通道,这是因为 TCP 协议可以解决数据在传送过程中的丢失、损坏、重复、乱序以及网络拥挤等问题,它保证数据可靠的传送。
- 类 URL 和 URLConnection 提供了最高级网络应用。URL 的网络资源的位置来同一表示 Internet 上各种网络资源。通过URL对象可以创建当前应用程序和 URL 表示的网络资源之间的连接,这样当前程序就可以读取网络资源数据,或者把自己的数据传送到网络上去。