文章目录
如何实现网络通信:网络编程三要素
- IP
- 端口号
- 网络通信协议
- OSI参考模型
- TCP/IP
网络分层
- OSI网络通信协议模型,是一个参考模型,而TCP/IP协议是事实上的标准
- TCP/IP是一个协议族,按照层次划分为四层:应用层,传输层,互连网络层,接口层(物理+数据链路层)
TCP/IP参考模型 | TCP/IP协议栈 |
---|---|
应用层 | HTTP、FTP、DNS、Telnet… |
传输层 | TCP、UDP |
网络层 | IP、ICMP、ARP… |
物理+数据链路层 | Ethernet、ATM、Frame Relay |
IP地址:InetAddress
- IPV4:4个字节组成,4个0-255;
- IPV6:128位(16个字节),写成8个无符号整数,每个整数用四个十六进制位表示,
数之间用冒号(:)分开,如:3ffe:3201:1401:1280:c8ff:fe4d:db39:1984 - 本地回环地址(hostAddress):127.0.0.1 主机名(hostName):localhost
InetAddress类
- 此类表示Internet协议(IP)地址,用于封装计算机的IP地址和DNS(没有端口信息)
- static InetAddress getByName(String host):确定主机名称的IP地址
- static InetAddress getLocalHost(String host):获取本机的IP
- String getHostName():获取此IP地址的主机名
- String getHostAddress():返回文本显示中的IP地址字符串
public class InetAddressTest {
public static void main(String[] args) {
try {
//确定主机名称的IP地址
InetAddress inetAddress = InetAddress.getByName("192.168.10.123");
InetAddress inetAddress1 = InetAddress.getByName("baidu.com");
System.out.println(inetAddress1);
// 获取此IP地址的主机名
System.out.println(inetAddress1.getHostName());
//返回文本显示中的IP地址字符串
System.out.println(inetAddress1.getHostAddress());
//获取本机的IP
InetAddress inetAddress2 = InetAddress.getLocalHost();
System.out.println(inetAddress2);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}
端口号
- 标识正在计算机上运行的进程(程序)
- 不同的进程有不同的端口号
- 一个 16 位的整数 0~65535
- 端口号与IP地址的组合得出一个网络套接字:Socket
协议
TCP协议
- 建立TCP连接
- 可靠的:采用三次握手的方式,点对点通信
- 两个应用进程:客户端、服务端
- 在连接中可进行大数据量的传输
- 传输完毕,需要释放连接,效率低
TCP协议重要字段
- 序号(sequence number):seq序号,占32位,用来标识从TCP源端向目的端发送的字节流
- 确认号(acknowledgement number):ack序号,占32位,只有ACK标志位为1时,确认序号字段才有效,ack=seq+1。
- 标志位(Flags):
URG:紧急指针(urgent pointer)有效。
ACK:确认序号标志;1:表示确认号有效; 0:表示报文中不含确认信息,忽略确认号字段.
PSH:接收方应该尽快将这个报文交给应用层。
RST:重置连接。
SYN:同步序号,用于发起一个新连接。
FIN:finish标志,用于释放连接,为1时表示关闭本方数据流。
- 在连接请求中,SYN=1和ACK=0表示该数据段没有使用捎带的确认域,而连接应答捎带一个确认,即SYN=1和ACK=1。
三次握手
- 客户端给服务端发送一个SYN报文,并指明客户端的初始化序列号:(SYN=1,seq=x);
- 客户端状态: SYN_SENT
- 服务端收到客户端报文,以自己的SYN报文作为应答,并指定服务端初始化序列号(SYN=1,seq=y);同时用x+1作为确认号ack发送给客户端,确认收到了客户端的SYN(ACK=1,ack=x+1);
- 服务端状态:SYN_RCVD
- 客户端收到SYN报文后,指定客户端序列号(seq=x+1)(初始为seq=x,第二个报文+1),会用y+1作为值作为确认号ack发给服务器,确认收到了服务端的SYN(ACK=1,ack=y+1),服务器与客户端建立连接。
- 客户端状态:ESTABLISHED
- 服务端状态:ESTABLISHED
四次挥手
- 客户端发送一个FIN报文,指定序列号(FIN=1,seq=u);并主动关闭TCP连接
客户端状态:FIN_WAIT1 - 服务端收到客户端FIN报文后,指定序列号v(seq=v),并发送一个确认报文段(ACK=1,seq=u+1),确认收到客户端的关闭连接请求;
服务端状态:CLOSE_WAIT - 服务端给客服发送一个FIN报文,指定序列号w(FIN=1,seq=w),并确认收到了客户端的关闭连接请求(ACK=1,seq=u+1)
服务端状态:LAST_ACK - 客户端收到服务端的FIN报文,指定序列号u+1(seq=u+1),并确认收到了服务端的报文段(ACK=1,ack=w+1)
UDP协议
- 无连接通信协议
- 不可靠的:发送方直接发送,接收方也不确认
- 通信结束时,不需要释放资源,消耗资源少,通信效率高,用于音频、视频和普通数据的传输
Socket
- 网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标
识符套接字 - Socket的分类:
- 流套接字(stream socket):使用TCP提供可依赖的字节流服务
- 数据报套接字(datagram socket):使用UDP提供“尽力而为”的数据报服务
URL
- 表示统一资源定位符,指向万维网上的“资源”的指针。用于区分、定位资源
- 一个标准的URL必须包括:protocol(方案或协议)、host(主机)、port(端口)、path(路径)、parameter( 查询参数)、anchor(锚点)
TCP网络编程
- 客户端发送信息给服务端,服务端将数据显示在控制台上
public class TCPClient {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress, 8900);
os = socket.getOutputStream();
os.write(1234);
os.write("您好,我是梅尝酥!".getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
public class TCPServer {
public static void main(String[] args) {
ServerSocket ss = null;
Socket socket=null;
InputStream is = null;
ByteArrayOutputStream baos=null;
try {
ss= new ServerSocket(8900);
socket = ss.accept();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if(ss != null){
try {
ss.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(ss != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(ss != null){
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(ss != null){
try {
baos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
- 从客户端发送文件给服务端,服务端保存到本地。并返回内容给客户端。
public class TCPClient1 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress, 9091);
os = socket.getOutputStream();
fis= new FileInputStream(new File("day03\\hadeng.png"));
byte[] buffer=new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//关闭数据的输出
socket.shutdownOutput();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
int len1;
byte[] buffer1=new byte[1024];
while ((len1 = is.read(buffer1)) != -1){
baos.write(buffer1,0,len1);
}
System.out.println(baos);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
public class TCPServer1 {
public static void main(String[] args) {
ServerSocket ss = null;
Socket socket = null;
FileOutputStream fos=null;
InputStream is=null;
OutputStream os = null;
try {
ss = new ServerSocket(9091);
socket = ss.accept();
is = socket.getInputStream();
fos=new FileOutputStream(new File("day03\\lily.png"));
byte[] buff=new byte[20];
int len;
while ((len = is.read(buff)) != -1){
fos.write(buff,0,len);
}
os = socket.getOutputStream();
os.write("已收到,谢谢".getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if(ss != null){
try {
ss.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
UDP网络编程
public class UDPSender {
public static void main(String[] args) {
DatagramSocket socket = null;
try {
InetAddress inetAddress = InetAddress.getLocalHost();
socket = new DatagramSocket();
String str="这是一个图片";
byte[] data=str.getBytes();
DatagramPacket packet = new DatagramPacket(data, 0, data.length, inetAddress, 9100);
socket.send(packet);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if(socket != null){
socket.close();
}
}
}
}
public class UDPReceiver {
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(9100);
byte[] buffer=new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
byte[] data = packet.getData();
String dataStr = new String(data, 0, packet.getLength());
System.out.println(dataStr);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
URL网络编程
URL常用的方法
- getProtocol():URL的协议名
- getHost():URL的主机名
- getPort():URL的端口号
- getFile():URL的文件路径
- getQuery():URL的查询名
public class URLTest1 {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/refine/hadeng.png");
System.out.println("URL的协议名:\t"+url.getProtocol());
System.out.println("URL的主机名:\t"+url.getHost());
System.out.println("URL的端口号:\t"+url.getPort());
System.out.println("URL的文件路径:\t"+url.getPath());
System.out.println("URL的文件名:\t"+url.getFile());
System.out.println("URL的查询名:\t"+url.getQuery());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}
/*
URL的协议名: http
URL的主机名: localhost
URL的端口号: 8080
URL的文件路径: /refine/hadeng.png
URL的文件名: /refine/hadeng.png
URL的查询名: null
*/
- 从服务器下载图片,并保存到本地。
public class URLTest {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/refine/hadeng.png");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream(new File("day03\\jim.png"));
byte[] buff = new byte[1024];
int len;
while ((len = is.read(buff)) != -1){
fos.write(buff,0,len);
}
System.out.println("Download successfully!");
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(urlConnection != null){
urlConnection.disconnect();
}
}
}
}