网络-Java编程

网络-Java编程

1. 概述

1.1 计算机网络

​ 计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统网络管理软件网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。

1.2 网络编程的目的

  • 传播交流信息
  • 数据交换,通信

可以达到的效果需要什么?

  • 如何定位到网络上的一台主机(ip+port)?
  • 找到主机之后,如何传输数据?

javaweb: 网页编程 B/S

网络编程: TCP/IP C/S

1.3 网络通信的要素

  1. 如何实现网络的通信?

通信双方的地址:

  • ip
  • 端口号

规则:网络通信的协议

2. IP实现

2.1 概述

ip-java:inetAddress

  • 唯一定位一台网络计算机
  • 127.0.0.1: localhost

ip 地址的分类:

  • 公网/私网

    • 公网:(互联网)
    • 私网:(192.168.0.0)
  • ipv4 / ipv6

    • ipv4 : 4个字节组成 0-255 42亿
    • ipv6: 128位 8个无符号整数!(0-9,a-f)
1
2
// 举例
2001.1aaa.1bbb.5888.7666.2eeee.2fab

2.2 代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 测试ip

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestIp {
public static void main(String[] args) {
try {
// 查询本机地址
InetAddress inet1 = InetAddress.getByName("127.0.0.1");
System.out.println(inet1);

InetAddress inet2 = InetAddress.getByName("localhost");
System.out.println(inet2);

InetAddress inet3 = InetAddress.getLocalHost();
System.out.println(inet3);

// 查询网站的ip地址
InetAddress inet4 = InetAddress.getByName("www.baidu.com");

// 常用方法
System.out.println(inet4.getAddress()); // 返回byte数组 [B@74a14482
System.out.println(inet4.getCanonicalHostName()); // 规范的名字
System.out.println(inet4.getHostAddress()); // ip
System.out.println(inet4.getHostName()); // 域名
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

3. 端口实现

3.1 概述

端口表示计算机上的一个程序的进程:

  • 不同的进程有不同的端口号!用来区分软件
  • 按规定分0——65535
  • TCP,UDP : 65535 * 2
    • TCP : 80
    • UDP: 80
    • TCP和UDP都可以占用80端口,但是在单个协议下,端口号不能冲突。
  • 端口分类:
    • 公有端口 (0-1023)
      • HTTP : 80
      • HTTPS : 443
      • FTP : 21
      • Telnet: 23
    • 程序注册端口:(1024–49151,分配用户或者程序)
      • Tomcat:8080
      • MySql:3306
      • Oracle:1521
    • 动态私有: (49152-65535)
1
2
3
netstat -ano # 查看所有的窗口
netstat -ano|findstr "8080" # 查看端口进程
tasklist|findstr "8080" # 查看指定端口的进程

3.2 代码实现

使用socket类:InetSocketAddress

源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Creates a socket address where the IP address is the wildcard address
* and the port number a specified value.
* <p>
* A valid port value is between 0 and 65535.
* A port number of {@code zero} will let the system pick up an
* ephemeral port in a {@code bind} operation.
* <p>
* @param port The port number
* @throws IllegalArgumentException if the port parameter is outside the specified
* range of valid port values.
*/
public InetSocketAddress(int port) {
this(InetAddress.anyLocalAddress(), port);
}

/**
*
* Creates a socket address from an IP address and a port number.
* <p>
* A valid port value is between 0 and 65535.
* A port number of {@code zero} will let the system pick up an
* ephemeral port in a {@code bind} operation.
* <P>
* A {@code null} address will assign the <i>wildcard</i> address.
* <p>
* @param addr The IP address
* @param port The port number
* @throws IllegalArgumentException if the port parameter is outside the specified
* range of valid port values.
*/
public InetSocketAddress(InetAddress addr, int port) {
holder = new InetSocketAddressHolder(
null,
addr == null ? InetAddress.anyLocalAddress() : addr,
checkPort(port));
}

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.net.InetSocketAddress;

// 测试端口
public class TestSocket {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
System.out.println(socketAddress);


System.out.println(socketAddress.getAddress()); // 返回ip地址
System.out.println(socketAddress.getHostName()); // 返回端口
}
}

4. 通信协议

  • TCP : 用户传输协议(打电话)

    • 三次握手

    mark

    1
    2
    3
    A : 你瞅啥?
    B : 瞅你咋地
    A : 干一场!
    • 四次挥手

    mark

    1
    2
    3
    4
    A : 我要走了
    B : 你真的要走了吗?
    B : 你真的真的要走了吗?
    A : 我真的要走了
    • 客户端/ 服务端
    • 建立连接
  • UDP: 用户数据报协议(发短信)

    • 客户端 / 服务端 :(没有明确的界限)
    • 不建立连接
  • IP : 网络互连协议

5. TCP (Java实现)

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2019-2022 Zhuuu
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信