网络编程详解
1.1、概述
计算机网络:
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协助下,实现资源共享和信息传递的计算机系统。
网络编程的目的:
传播交流信息,数据交换,通信
主要因素(想要达到效果需要什么):
- 如何准确地定位网络上的一台主机 IP : 端口,定位到这个计算机上的某个资源
- 找到了这个主机,如何传输数据
javaweb:网页编程 B/S
网络编程:TCP/IP C/S
1.2、网络通信的要素
如何实现网络的通信?
通信双方的地址:
- IP
- 端口号
规则:网络通信的协议
http, ftp, smtp, udp……
TCP/IP四层概念模型、OSI七层参考协议
小结:
- 网络编程中有两个主要的问题
- 如何准确地定位到网络上的一台或多台主机
- ping 域名
- 找到主机之后如何进行通信
- 如何准确地定位到网络上的一台或多台主机
- 网络编程中的要素
- IP 和端口号
- 网络通信协议:udp,tcp
- java中万物皆对象
1.3、IP
IP地址:InetAddress
-
唯一定位仪态网络上的计算机
-
127.0.0.1:本机 localhost
-
ip地址分类
-
ipv4/ipv6
-
IPV4 127.0.0.1 4个字节组成。0~255,42亿
-
IPV6 fe80::e4f6:12e8:4576:afdf%7 128位,8个无符号整数
2001:obb2:aaaa:0015:0000:0000:1aaa:1312
-
-
公网(互联网)-私网
- ABCD类地址
- A:1.0.0.0 ~ 127.255.255.255
- B : 128.0.0.0 ~ 191.255.255.255
- C : 192.0.0.0 ~ 223.255.255.255
- D :224.0.0.0 ~ 239.255.255
- 192.168.xx.xx,专门给组织内部使用的
- ABCD类地址
-
import java.net.InetAddress;
import java.net.UnknownHostException;
//测试ID
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3 = InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress inetAddress4 = InetAddress.getLocalHost();
System.out.println(inetAddress4);
//查询网站IP地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
//常用方法
System.out.println(inetAddress2.getAddress()); //字节地址
System.out.println(inetAddress2.getCanonicalHostName()); //规范名称
System.out.println(inetAddress2.getHostAddress()); //主机地址
System.out.println(inetAddress2.getHostName()); //域名或自己电脑名称
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
结果:
/127.0.0.1
localhost/127.0.0.1
LAPTOP-AMMU2M9G/172.168.2.72
www.baidu.com/110.242.68.4
[B@7f31245a
110.242.68.4
110.242.68.4
www.baidu.com
1.4、端口
端口表示计算机上的一个程序的进程:
-
不同的进程有不同的端口号!用来区分软件!
-
被规定0-65535
-
TCP端口,UDP端口:65535*2,单个写一下,端口号不能冲突
-
端口分类
-
公有端口 0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
-
程序注册接口:1024~49151,分配用户或程序
- Tomcat:8080
- MySQL:3306
- Oracle:1521
-
动态、私有:49152~65535
netstat -ano #查看所有端口 netstat -ano | findstr "5900" #查看制定端口 tasklist|finder "8696" #查看制定端口的进程 ctrl+shift+ESC #打开任务管理器
-
public class TestInetSocketAddress { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080); InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080); System.out.println(socketAddress); System.out.println(socketAddress2); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName()); System.out.println(socketAddress.getPort()); } }
-
1.5、通信协议
协议:约定
**网络通信协议:**速率,传输码率,代码结构,传输控制……
**问题:**非常复杂
大事化小:分层概念!
TCP/IP协议(簇)
重要:
- TCP:用户传输协议
- UDP:用户数据报协议
出名的协议:
- TCP:
- IP:网络互联协议
TCP UDP对比
TCP:类似打电话
-
连接,稳定
-
三次握手、四次挥手
最少需要三次,保证稳定链接! A:你瞅啥? B:瞅你咋地? A:干一场! 四次链接,确定断开 A:我要断开 B:我知道你要断开了 B:真的断开了吗?(确认) A:我真的要断开了
-
客户端、服务端
-
传输完成,释放连接,效率低
UDP:类似发短信
- 不连接,不稳定
- 客户端、服务端:没有明确界限
- 不管有没有准备好,都可以发送
- 导弹,不需要对面接收
- DDOS:洪水攻击(饱和攻击)
1.6、TCP
客户端
- 链接服务器 Scoket
- 发送消息
//客户端
public class TcpClientDemo01 {
public static void main(String[] args) throws IOException {
Socket socket = null;
OutputStream os = null;
try {
//1.要知道副武器的地址,端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int prot = 9999;
//2.创建一个socket链接
socket = new Socket(serverIP, prot);
//3.发送消息 IO流
os = socket.getOutputStream();
os.write("你好,欢迎学习".getBytes());
} catch (UnknownHostException 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();
}
}
}
}
}
服务器
- 监理服务的端口 ServerSocket
- 等待用户的链接 accept
- 接收用户消息
//服务端
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket =null;
InputStream is = null;
ByteArrayOutputStream baos =null;
try {
//1.我得有一个地址
serverSocket = new ServerSocket(9999);
while (true){ //不增加while(true)则只接收一次客户端发送的消息,增加while(true)则持续接收客户端发送的消息---客户端启动一次,服务端完成输出一次并继续监听下一次
//2.等待客户端连接
socket = serverSocket.accept();
//3.读取客户端的消息
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());
/*
//暴力方法(IO流在缓冲中取数据,循环展示,会存在问题)
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
String msg = new String(buffer, 0, len);
System.out.println(msg);
}
*/
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源,增加null判断,确保程序不会报错
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();
}
}
}
}
}
注:需要先启动服务端,然后再启动客户端
1.7、文件上传
客户端
//客户端发送文件
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception {
//1.1.创建一个Socket链接
Socket socket = new Socket(InetAddress.getByName("127.0.0.10"), 9000); //服务器地址和端口
//1.2.创建一个输出流
OutputStream clientOutPutStream = socket.getOutputStream();
//1.3.文件流(读取文件)
FileInputStream clientFileInputStream = new FileInputStream(new File("kuang.png")); //获取根目录下的kuang.png文件
//1.4.文件流(写出文件)
byte[] buffer = new byte[1024]; //定义缓存
int len;
while((len = clientFileInputStream.read(buffer)) != -1){ //读取文件流的内容数量,不为空时执行
clientOutPutStream.write(buffer, 0, len); //将文件流从0到全部长度,写入到输出流中
}
//1.5通知服务器,我已经结束了(不增加,则socket不结束,客户端一直发送,服务器一直监听)
socket.shutdownOutput(); //已经传输完成结束发送
//3.1确定服务器接收完毕,然后再断开链接
InputStream clientInputSteam = socket.getInputStream(); //用于接收服务端完成后发送回来的内容
//String byte[] 类型的数组,发送回来的为 String 类型,通过byte[]数组接收
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //用于接收服务端返回字段
byte[] buffer2 = new byte[1024]; //定于缓存
int len2;
while ((len2 = clientInputSteam.read(buffer2)) != -1){ //判断服务端返回内容是否为空
byteArrayOutputStream.write(buffer2, 0, len2); //将服务端返回内容写入byteArrayOutputStream
}
System.out.println(byteArrayOutputStream.toString()); //打印出返回内容
//3.2.关闭资源
//先关闭服务器返回的
byteArrayOutputStream.close();
clientInputSteam.close();
//3.3再关闭客户端发送的
clientFileInputStream.close();
clientOutPutStream.close();
socket.close();
}
}
服务端
//服务端读取文件
public class TcpServerDemo02 {
public static void main(String[] args) throws IOException {
//先启动服务端,创建端口,并持续监听客户端的连接
//2.1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.2.监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
/* 作用等同于监听键盘输入
Scanner scanner = new Scanner(System.in);
scanner.next();
*/
//客户端启动后从1.1到1.5执行完成,服务端监听到客户端连接(文件流),文件流发送到服务端,从2.3开始执行
//2.3.获取输入流(客户端发送的文件流)
InputStream is = socket.getInputStream();
//2.4.文件输出
FileOutputStream fos = new FileOutputStream(new File("recelve.png")); //文件命名
byte[] buffer = new byte[1024]; //定义缓存
int len;
while ((len=is.read(buffer)) != -1){ //判断在客户端取得的文件流内容不为空
fos.write(buffer, 0, len); //从第0位到len位,获取文件流写入fos
}
//2.5通知客户端接收完毕
OutputStream os = socket.getOutputStream();
os.write("接收完毕".getBytes()); //发动给客户端,表示已完成
//2.6.关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
注意:
1. 程序需要先启动服务端,然后再启动客户端
2.客户端需要增加 1.5 ,用于通知服务端已经完成文件流传输,避免一直监听
3.文件需要放在项目根目录下,并在客户端 1.3 中标明文件名称(kuang.png)
结果:在客户端结果处打印出服务端的 “接收完毕”
1.8、Tomcat
上文中的文件上传为自定义服务器和客户端(C/S)
通常使用Tomcat 和 浏览器作为服务器和客户端(B/S)
服务端
- 自定义 S
- Tomcat服务器 S
客户端
- 自定义 C
- 浏览器 B
注:Tomcat启动运行需要配置java环境(JDK和JRE)
下载地址: http://tomcat.apache.org/
1.9、UDP
类似发送短信,不需要服务器
发送端
//不需要连接服务器,类似于发短信
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
// DatagramSocket socket = new DatagramSocket(8080); //定义端口后,可以接收消息,既可以当客户端发送消息,又可以当服务端接收消息
//2.建个包
String msg = "你好,服务器!";
//发送给谁
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//数据,数据的长度起、始,要发送给谁,端口
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();
}
}
接收端
//等待客户端的链接
public class UdpServerDemo01 {
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); //定义packet用于接收buffer从0到buffer长度的内容
socket.receive(packet);//阻塞接收,监听客户端传值
System.out.println(packet.getAddress().getHostAddress()); //展示packet地址
System.out.println(new String(packet.getData(), 0, packet.getLength())); //展示packet内容
//关闭链接
socket.close();
}
}
1.10、UDP聊天实现
在线聊天(一方发送,另一方接收)
发送端
public class UpdSenderDemo01 {
public static void main(String[] args) throws Exception {
//创建聊天端口,可接收数据
DatagramSocket socket = new DatagramSocket(8888);
//准备聊天数据,从控制台读取 System.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //从控制台读取数据写入buffer
while (true){ //循环用于监听控制台输入内容
String data = reader.readLine(); //从buffer中读取输入的数据
byte[] datas = data.getBytes(); //通过datas获取具体的数据,byte[]用于包发送
DatagramPacket packet = new DatagramPacket(datas, 0, data.getBytes().length, new InetSocketAddress("localhost", 6666)); //将datas中的数据写入包中,数据内容,数据起、始,地址和端口
//发送包内数据
socket.send(packet);
//发送 bye 结束循环监听
if (data.equals("bye")){
break;
}
}
//结束关闭
socket.close();
}
}
接收端
public class UpdReceiveDemo01 {
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); //从byte[]中读取数据放入包中
socket.receive(packet); //阻塞式接收包
//输出包中数据
byte[] data = packet.getData(); //获取包中数据
String receiveData = new String(data, 0, packet.getLength()); //从0开始取长度,注意是获取传过来的包的数据长度,而不是定义的byte[]长度
System.out.println(receiveData); //打印数据
//判断是否结束对话
if (receiveData.equals("bye")){
break; //跳出循环
}
}
socket.close();
}
}
实现既可以发送,又可以接收
一:发送类方法
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); //通过TalkSend方法传入的值,打开发送端口
reader = new BufferedReader(new InputStreamReader(System.in)); //获取控制台输入内容
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (true){
String data = reader.readLine(); //从reader中读取数据(控制台输入的数据)
byte[] datas = data.getBytes(); //通过datas获取具体的数据(byte[]类型数据)
DatagramPacket packet = new DatagramPacket(datas, 0, data.getBytes().length, new InetSocketAddress(this.toIP, this.toPort)); //将datas中的数据写入包中, 通过传入的变量发送包数据
//发送包内数据
socket.send(packet);
//结束聊天的方式
if (data.equals("bye")){
break;
}
}
}catch (Exception e){
e.printStackTrace();
}
//结束聊天后关闭资源
socket.close();
}
}
二:接收类方法
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){ //判断为真,一直执行下方阻塞式接受包
try {
//准备接受包
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
//从byte[]中读取数据放入包中
socket.receive(packet); //阻塞式接收包
//结束对话
byte[] data = packet.getData(); //获取包中数据
String receiveData = new String(data, 0, packet.getLength()); //从0开始取长度
System.out.println(msgFrom + ":" + receiveData); //通过变量的方式,确定发送者和发送的内容
if (receiveData.equals("bye")){
break; //跳出循环
}
}catch(Exception e){
e.printStackTrace();
}
}
//关闭资源
socket.close();
}
}
三:聊天类实体1
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程,既可能是接收端,也可能是发送端
new Thread(new TalkSend(7777, "localhost", 9999)).start(); //TalkSend(发送者端口,发送地址,接收者端口)
new Thread(new TalkReceive(8888, "老师")).start(); //TalkReceive(接收消息的端口,接收谁的消息)
}
}
四:聊天类实体2
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555, "localhost", 8888)).start(); //TalkSend(发送者端口,发送地址,接收者端口)
new Thread(new TalkReceive(9999, "学生")).start(); //TalkReceive(接收消息的端口,接收谁的消息)
}
}
注:9999为老师接收消息的端口,所以学生需要向9999端口发送消息,发送端口不做限制;
new Thread(new TalkSend(7777, “localhost”, 9999)).start();
8888为学生接收消息的端口,所以老师需要向8888端口发送消息,发送端口不做限制。
new Thread(new TalkSend(5555, “localhost”, 8888)).start();
两个实体类启动后,在下方控制台发送消息,对方可以在控制台中看到发送的消息,并随时可以向对方发送消息,直到发送 bye 后,程序结束
存在的问题:任何一方发送 bye 后,如果得不到对方发送的 bye ,对方线程未停止,可以看到对方发送的消息,但我方无法发送消息,直到对方发送 bye 程序结束
1.11、URL下载网络资源
统一资源定位符:定位资源的,定位互联网上的某一个资源
DNS域名解析:www.***.com --> xxx.x…x…x…
协议://ip地址:端口/项目名/具体资源地址
查询网络路径
public class URLDemo01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloword/index.jsp?username=kuang&password=123");
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()); //参数
}
}
下载网络文件
public class UrlDown {
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL("http://localhost:8080/zhao/SecurityFile.txt");//网络文件地址(网址)
//2.连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("SecurityFile.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(); //断开链接
}
}
文件下载后,保存在项目根目录下