Java_网络编程
1.1计算机网络
<1>计算机网络:
计算机网络是指将地理位置不同的具有独立功能的多态计算机及其外部设备,通过通信线路连接起来,在网络操作系统,
网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
<2>网络编程的目的:
传播交流信息,数据交换!通信
<3>达到这个效果需要什么:
1.如何准确的定位网络上的一台主机:IP地址、端口:定位到这个计算机上的某个资源
2.找到了这个主机,如何传输数据
javaweb:网页编程 B/S
网络编程:TCP/IP C/S
1.2网络通信的要素
如何实现网络的通信
通信双方的地址:
- IP
- 端口号
- 192.168.1.15:5900
规则:网络通信的协议
http ftp smtp tcp udp
网络编程主要针对:
小结:
1.网络编程中有两个主要的问题
- 如何精准的定位到网络上的一台或多台主机
- 找到主机之后如何进行通信
2.网络编程中的要素 - IP和端口号
- 网络通信协议
3.java编程思想:万物皆对象
JDK帮助文档查找java有关网络的类
1.3、IP
IP地址:InetAddress
- 唯一定位一台网络上的计算机
- 127.0.0.1:本机 localhost
- ip地址分类:
ipv4/ipv6
ipv4: 127.0.0.1 4个字节组成,每一个字节0-255,大约有42亿个地址;30亿都在北美,亚洲4亿。2011年已用尽
ipv6:128位。8个无符号整数(0-9加上a-f)
例如: 2409:8a4c:611:7ef0:f557:7e96:a38e:be50
ipv6是128位的,俗称冒号分十六进制,意思是由十六进制表示的,用冒号分开,共8段,
每段有四个十六进制表示,所以每段是44=16位了,所以是168=128位!
公网(互联网)-私网(局域网)
192.168.xx.xxx,专门给组织内部使用的
ABCDE类地址:
最初设计互联网络时,为了便于寻址以及层次化构造网络,每个IP地址包括两个标识码(ID),即网络ID和主机ID。同一个物理网络上的所有主机都使用同一个网络ID,网络上的一个主机(包括网络上工作站,服务器和路由器等)有一个主机ID与其对应。IP地址根据网络ID的不同分为5种类型,A类地址、B类地址、C类地址、D类地址和E类地址。
IP地址分为A,B,C,D,E五类。
网络号:用于识别主机所在的网络;
主机号:用于识别该网络中的主机。
其中A类分配给政府机关使用,B类地址给大中型企业使用,C类地址给个人使用。这三种是主要的。
IP地址分为五类,A类保留给政府机构,B类分配给中等规模的公司,C类分配给任何需要的人,D类用于组播,E类用于实验,各类可容纳的地址数目不同。
其中A类、B类、和C类这三类地址用于TCP/IP节点,其它两类D类和E类被用于特殊用途。
A、B、C三类IP地址的特征:当将IP地址写成二进制形式时,A类地址的第一位总是O,B类地址的前两位总是10,C类地址的前三位总是110。
1. A类IP地址
一个A类IP地址由1字节的网络地址和3字节主机地址组成,网络地址的最高位必须是“0”, 地址范围从1.0.0.0 到126.0.0.0。可用的A类网络有126个,每个网络能容纳1亿多个主机。
⑴ A类地址第1字节为网络地址,其它3个字节为主机地址。
⑵ A类地址范围:1.0.0.1—126.155.255.254
⑶ A类地址中的私有地址和保留地址:
① 10.X.X.X是私有地址(所谓的私有地址就是在互联网上不使用,而被用在局域网络中的地址)。
② 127.X.X.X是保留地址,用做循环测试用的。
2. B类IP地址
一个B类IP地址由2个字节的网络地址和2个字节的主机地址组成,网络地址的最高位必须是“10”,地址范围从128.0.0.0到191.255.255.255。可用的B类网络有16382个,每个网络能容纳6万多个主机 。
⑴ B类地址第1字节和第2字节为网络地址,其它2个字节为主机地址。
⑵ B类地址范围:128.0.0.1—191.255.255.254。
⑶ B类地址的私有地址和保留地址
① 172.16.0.0—172.31.255.255是私有地址
② 169.254.X.X是保留地址。如果你的IP地址是自动获取IP地址,而你在网络上又没有找到可用的DHCP服务器。就会得到其中一个IP。
3. C类IP地址
一个C类IP地址由3字节的网络地址和1字节的主机地址组成,网络地址的最高位必须是“110”。范围从192.0.0.0到223.255.255.255。C类网络可达209万余个,每个网络能容纳254个主机。
⑴ C类地址第1字节、第2字节和第3个字节为网络地址,第4个个字节为主机地址。另外第1个字节的前三位固定为110。
⑵ C类地址范围:192.0.0.1—223.255.255.254。
⑶ C类地址中的私有地址:
192.168.X.X是私有地址。
4. D类地址用于多点广播(Multicast)。
D类IP地址第一个字节以“lll0”开始,它是一个专门保留的地址。它并不指向特定的网络,目前这一类地址被用在多点广播(Multicast)中。多点广播地址用来一次寻址一组计算机,它标识共享同一协议的一组计算机。
⑴ D类地址不分网络地址和主机地址,它的第1个字节的前四位固定为1110。
⑵ D类地址范围:224.0.0.1—239.255.255.254
5. E类IP地址
以“llll0”开始,为将来使用保留。
全零(“0.0.0.0”)地址对应于当前主机。全“1”的IP地址(“255.255.255.255”)是当前子网的广播地址。
⑴ E类地址也不分网络地址和主机地址,它的第1个字节的前五位固定为11110。
⑵ E类地址范围:240.0.0.1—255.255.255.254
在IP地址3种主要类型里,各保留了3个区域作为私有地址,其地址范围如下:
A类地址:10.0.0.0~10.255.255.255
B类地址:172.16.0.0~172.31.255.255
C类地址:192.168.0.0~192.168.255.255
A类地址的第一组数字为1~126。注意,数字0和 127不作为A类地址,数字127保留给内部回送函数,而数字0则表示该地址是本地宿主机,不能传送。
B类地址的第一组数字为128~191。
C类地址的第一组数字为192~223。
1. A类地址
A类地址的表示范围为:0.0.0.0~126.255.255.255,默认网络掩码为:255.0.0.0;A类地址分配给规模特别大的网络使用。A类网络用第一组数字表示网络本身的地址,后面三组数字作为连接于网络上的主机的地址。分配给具有大量主机(直接个人用户)而局域网络个数较少的大型网络。例如IBM公司的网络。
2. B类地址
B类地址的表示范围为:128.0.0.0~191.255.255.255,默认网络掩码为:255.255.0.0;B类地址分配给一般的中型网络。B类网络用第一、二组数字表示网络的地址,后面两组数字代表网络上的主机地址。
3. C类地址
C类地址的表示范围为:192.0.0.0~223.255.255.255,默认网络掩码为:255.255.255.0;C类地址分配给小型网络,如一般的局域网和校园网,它可连接的主机数量是最少的,采用把所属的用户分为若干的网段进行管理。C类网络用前三组数字表示网络的地址,最后一组数字作为网络上的主机地址。
实际上,还存在着D类地址和E类地址。但这两类地址用途比较特殊,在这里只是简单介绍一下:D类地址称为广播地址,供特殊协议向选定的节点发送信息时用。E类地址保留给将来使用。
package inetip;
import java.net.InetAddress;
import java.net.UnknownHostException;
//测试IP
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddressOne = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddressOne);
//查询网站IP地址
InetAddress inetAddressTwo = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddressTwo);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
InetAddress类常用方法:
package inetip;
import java.net.InetAddress;
import java.net.UnknownHostException;
//测试IP
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddressOne = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddressOne);
InetAddress inetAddressThree = InetAddress.getByName("localhost");
System.out.println(inetAddressThree);
InetAddress inetAddressFour = InetAddress.getLocalHost();
System.out.println(inetAddressFour);
//查询网站IP地址
InetAddress inetAddressTwo = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddressTwo);
//常用方法
System.out.println(inetAddressTwo.getAddress());
System.out.println(inetAddressTwo.getCanonicalHostName());//规范的名字
System.out.println(inetAddressTwo.getHostAddress());
System.out.println(inetAddressTwo.getHostName());//域名,或者自己电脑的名字
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
1.4端口
端口表示计算机上的一个程序的进程:
- 不同的进程有不同的端口号!用来区分软件!
- 被规定0-65535
- TCP,UDP:65535*2 tcp:80端口,udp:80端口,单个(同一个)协议下,端口号不能冲突
- 端口分类:
- 公有端口0-1023
1.HTTP:80
2.HTTPS:443
3.FTP:21
4.Telent:23 - 程序注册端口:1024-49151,分配用户或者程序
1.Tomcat:8080
2.MySQL:3306
3.Oracle:1521
*动态、私有:49152-65535
Dos命令:查看电脑TCP/UDP地址及端口号
netstat -ano
netstat -ano|findstr "5900" #查看指定的端口
tasklist|findstr "8696" #查看指定端口的进程
InetSocketAddress类:
package inetip;
import java.net.InetSocketAddress;
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress socketAddressTwo = new InetSocketAddress("localhost", 8080);
System.out.println(socketAddress);
System.out.println(socketAddressTwo);
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getPort());
}
}
更改localhost名字:
打开C:\Windows\System32\drivers\etc 路径下的hosts文件增加一行:127.0.0.1 wangzhen
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 wangzhen
1.5通信协议
协议:约定,就好比我们现在说的是普通话
网络通信协议:速率,传输码率,代码结构,传输控制...
问题:非常的复杂?
大事化小:分层
TCP/IP协议簇:实际上是一组协议
重要:
- TCP:用户传输协议
- UDP:用户数据报协议
出名的协议:
TCP:
IP:网络互连协议
TCP UDP对比:
TCP:打电话
- 连接,稳定
- 三次握手,四次挥手
- 客户端、服务端
- 传输完成,释放连接,效率低
UDP:发短信 - 不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备好,都可以发给你...
- 导弹
- DDOS:洪水攻击!饱和攻击
服务端代码:
package inetip;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class TcpServerDemoOne {
public static void main(String[] args) {
ByteArrayOutputStream baos =null;
InputStream is = null;
Socket socket = null;
ServerSocket serverSocket = null;
try {
//1.服务端得有一个地址
serverSocket = new ServerSocket(9999);
//循环监听客户端连接
while(true){
//2.等待客户端连接过来
socket = serverSocket.accept();
//3.读取客户端消息
is = socket.getInputStream();
//4.管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while((length=is.read(buffer)) != -1){
baos.write(buffer,0,length);
}
System.out.println(baos.toString());
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源(先开后关)
if(baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
if(is!=null){
try {
is.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();
}
}
}
}
}
}
客户端代码:
package inetip;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class TcpClientDemoOne {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1.要知道服务器的地址,端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建一个socket连接
socket = new Socket(serverIP, port);
//3.发送消息,IO流
os = socket.getOutputStream();
os.write("你好,wangzheng".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.6、TCP
客户端
1.连接服务器Socket
2.发送消息
服务器
1.建立服务的端口ServerSocket
2.等待用户的连接accept
3.接收用户的消息
文件上传服务器端代码:
package inetip;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemoTwo {
public static void main(String[] args) throws IOException {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的连接,阻塞式监听,会一直等待客户端连接
Socket socket = serverSocket.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("jieshou.png"));
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//通知客户端我接收完毕了
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了,你可以断开了".getBytes());
//关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
文件流客户端代码:
package inetip;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TCPClientDemoTwo {
public static void main(String[] args) throws Exception {
//1.创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream(new File("shipin.png"));
//4.写出文件
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer))!= -1){
os.write(buffer,0,len);
}
//通知服务器,我已经结束了
socket.shutdownOutput();//我已经传输完了!
//确定服务器接收完毕,才能够断开连接
InputStream intputstream = socket.getInputStream();
//String byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while((len2=intputstream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
//5.关闭资源
fis.close();
os.close();
socket.close();
}
}
Tomcat
服务端
- 自定义 S
- Tomcat服务器 S:Java后台开发!
客户端 - 自定义 C
- 浏览器 B
1.7、UDP
发短信:不用连接,需要知道对方的地址!
Udp服务器端:
package udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//还是需要等待客户端的连接!
public class UdpServerDemoOne {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);//阻塞式接收
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
//关闭连接
socket.close();
}
}
Udp客户端代码:
package udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//不需要连接服务器
public class UdpClientDemoOne {
public static void main(String[] args) throws Exception {
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.建立包
String msg = "你好,服务器!";
//3.发送给谁
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//数据,数据的长度起始,要发送给谁
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
//4.发送包
socket.send(packet);
//5.关闭流
socket.close();
}
}
循环发送消息:
发送端:
import java.net.InetSocketAddress;
public class UdpsenderDemoOne {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//准备数据:控制台读取System.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,datas.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if(data.equals("bye")){
break;
}
}
socket.close();
}
}
接收端:
package udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemoOne {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while(true){
//准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);//阻塞式接收包裹
//断开连接 bye
byte[] data = packet.getData();
String receiveDate = new String(data,0,data.length);
System.out.println(receiveDate);
if(receiveDate.equals("bye")){
break;
}
}
socket.close();
}
}
在线咨询:两个人都可以是发送方,也都可以是接收方!
学生发消息类:
package udp;
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")) .start();
}
}
老师发消息类:
package udp;
public class TalkTeacher {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")) .start();
}
}
发送消息类:
package udp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort,String toIP,int toPort){
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
socket.send(packet);
if (data.equals("bye")) {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
try {
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
接收消息类:
package udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable {
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom){
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run(){
while (true){
//准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
//断开连接 bye
byte[] data = packet.getData();
String receiveData = new String(data,0,data.length);
System.out.println(msgFrom + ":" + receiveData);
if(receiveData.equals("bye")){
break;
}
}
socket.close();
}
}
1.8、URL
https://www.baidu.com/
统一资源定位符:定位资源的,定位互联网上的某一个资源
DNS域名解析 www.baidu.com xxx.x.x.x
协议://ip地址:端口/项目名/资源
package url;
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/wangzhen/HelloWorld.txt?username=wangzhen&password=123456");
System.out.println(url.getProtocol());//协议
System.out.println(url.getHost());//主机IP
System.out.println(url.getPort());//端口
System.out.println(url.getPath());//文件
System.out.println(url.getFile());//全路径
System.out.println(url.getQuery());//参数
}
}
运行结果:
localhost
8080
/wangzhen/HelloWorld.txt
/wangzhen/HelloWorld.txt?username=wangzhen&password=123456
username=wangzhen&password=123456
下载URL资源:
package url;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @Author:wangzhen
* @Date:Date
*/
public class UrlDown {
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL("http://localhost:8080/wangzhen/HelloWorld.txt");
//2.连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("helloworld.txt");
byte[] buffer = new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//写出这个数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}
本文来自博客园,作者:冷月_1991,转载请注明原文链接:https://www.cnblogs.com/wangzhen1991/p/16644637.html
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术