1.1 概述
网络编程的目的:
无限电台。。。。传播交流信息,数据交换。通信
想要达到这个效果需要什么:
1.如何准确的定位网络上的一台珠玑 ip地址 端口,定位到这个计算机上的某个资源
2.找到了这个珠玑,如何传输数据
Javaweb: 网页编程 B/s
网络编程:TCP/IP C/S
1.2 网络通信的要素
通信双方地址:
- IP
- 端口号
- 192.168.16.124:5900
- 规则:网络通信的协议
TCP/IP
小结:
- 网络编程中有两个主要的问题
- 如何准确的定位到网络上的一台或多台主机
- 找到主机之后如何进行通信
- 网络编程中高端要素
- IP和端口号 IP
- 网络通信协议 udp tcp
- 万物皆对象
1.3 ip
ip地址:inteAddress 查看本地 cmd ipconfig
- 唯一定位一台网络上计算机
- 127.0.0.1:本机localhost
- ip地址的分类
- ipv4/Ipv6
- ipv4 127.0.0.1 4个字节组成。 0~255 42亿个
- ipv6 fe80::60f8:dddd:6bea:17a7%5 ,128位。8个无符号整数!
- ipv4/Ipv6
package com.jay;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @description:
* @author: jyf
* @create: 2021-02-20 21:52
**/
public class InetAddressTest {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com"); // 查询网络地址
System.out.println(inetAddress4);
System.out.println(inetAddress4.getAddress());
System.out.println(inetAddress4.getCanonicalHostName());
System.out.println(inetAddress4.getHostAddress());
System.out.println(inetAddress4.getHostName());
}
}
1.4端口
端口表示计算机上得一个程序得进程
- 不同的进程有不同的端口号!用来区分软件的
- 被规定 0~65535
- TCP,UDP : 65535 * 2 tcp:80 udp:80 单个嫌疑下端口号不能冲突
- 端口分类
- 公有端口 0~1024
- http:80
- https:443
- ftp:21
- telent:23
- 程序注册端口:1024~49151,分配用户或者程序
- tomcat:8080
- mysql:3306
- oracle:1521
- 动态、私有:49152~65535
- 公有端口 0~1024
netstat -ano //查看所有的端口
netstat -ano |findstr “端口号” //查看指定的端口号
tasklist|findstr "端口号" //查看指定端口号的进程
package com.jay;
import java.net.InetSocketAddress;
/**
* @description:
* @author: jyf
* @create: 2021-02-20 22:15
**/
public class InetSocketAddressTest {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 8080);
System.out.println(inetSocketAddress);
System.out.println(inetSocketAddress2);
System.out.println(inetSocketAddress.getAddress());//获取地址
System.out.println(inetSocketAddress.getHostName());//获取主机名字
System.out.println(inetSocketAddress.getPort());//获取端口
}
}
1.5通信协议
网络通信协议:速率、传输码率、代码结构、传输控制…
TCP/IP协议簇:是一组协议
- TCP:用户传输协议
- UDP:用户数据报协议
- IP:网络互连协议
TCP与UDP的对比:
TCP:打电话
- 连接稳定
- 三次握手、四次挥手
//最少需要三次,保证稳定连接!<连接>
A:你瞅啥?
B:瞅你咋地?
A:干一场
//四次挥手 <断开>
A:我要走了
B:你真的要走了吗?
B:你真的真的要走了吗?
A:我真的要走了
- 客户端、服务器
- 传输完成、释放连接、效率低
UDP:发短信
- 不连接、不稳定
- 客户端、服务器:没有明确的界限
- 不管有没有准备好,都可以发送给你
- DDOS:洪水攻击(发送大量的信息,堵塞端口,饱和攻击)
1.6 TCP
文字传输
1.客户端:
- 连接服务器 (Socket)
- 发送消息
package com.lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @description: 客户端
* @author: jyf
* @create: 2021-02-20 22:37
**/
public class TestClientSocket {
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("你好,世界!".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();
}
}
}
}
}
2.服务器:
- 建立服务器的端口号 ServerSocket
- 等待用户的链接 accept
- 接受用户的信息
package com.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @description: 服务器端
* @author: jyf
* @create: 2021-02-20 22:37
**/
public class TestServersSocket {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream baos = null;
try {
//1、给自己新建一个端口号
serverSocket = new ServerSocket(9999);
while (true) {
//2、等待客户端进行连接
socket = serverSocket.accept();
//3、读取客户端的消息
inputStream = socket.getInputStream();
//4、管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
}
/*
while ((len = inputStream.read(buffer)) != -1) {
String string = new String(buffer,0,len);
System.out.println(string);
}
*/
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、关闭资源(后开先关)
if (baos != null) {
try {
baos.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();
}
}
}
}
}
文件上传
客户端:
package com.lesson02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
/**
* @description:
* @author: jyf
* @create: 2021-02-20 23:03
**/
public class TestClientSocket2 {
public static void main(String[] args) throws Exception {
//1、创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
//2、创建一个输出流
OutputStream os = socket.getOutputStream();
//3、读取文件
FileInputStream fis = new FileInputStream(new File("1.png"));
//4、写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
//同知服务端已经传输完了
socket.shutdownOutput();//传输完毕
//5、确定服务器接受完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = inputStream.read(buffer2)) != -1) {
baos.write(buffer2, 0, len2);
}
System.out.println(baos.toString());
//6、关闭资源
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
服务端
package com.lesson02;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @description: 文件上传: 服务端
* @author: jyf
* @create: 2021-02-20 23:03
**/
public class TestServersSocket2 {
public static void main(String[] args) throws IOException {
//1、创建服务
ServerSocket serverSocket = new ServerSocket(9999);
//2、监听客户端得连接
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待
//3、获取输入流
InputStream is = socket.getInputStream();
//4、文件输出
FileOutputStream fos = new FileOutputStream(new File("2.png"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
//5、通知客户端,我接受完毕了
OutputStream os = socket.getOutputStream();
os.write("我接受完了".getBytes());
//6、关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
1.7Tomcat
服务端:
- 自定义 S
- Tomcat服务器 S
客户端:
- 自定义 C
- 浏览器 B
1.7、UDP
发送消息
发送端
package com.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @description: 发送端
* @author: jyf
* @create: 2021-02-20 23:51
**/
public class UdpTest02 {
public static void main(String[] args) throws Exception {
// 1.建立一个socket
DatagramSocket socket = new DatagramSocket();
// 2.建个包
InetAddress inetAddress = InetAddress.getByName("localhost");
String msg = "你好";
int port = 9999;
DatagramPacket packet = new DatagramPacket
(msg.getBytes(), 0, msg.getBytes().length,
inetAddress, port);
// 3.发送包
socket.send(packet);
// 4.关闭流
socket.close();
}
}
接受端
package com.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @description: 接收端
* @author: jyf
* @create: 2021-02-20 23:51
**/
public class UdpTest01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9999);
//接收数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
//阻塞接收
socket.receive(packet);
System.out.println(packet.getAddress());
System.out.println(new String(packet.getData(), 0, packet.getLength()));
//关闭链接
socket.close();
}
}
咨询
循环发送消息
package com.lesson04;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @description: 发送方
* @author: jyf
* @create: 2021-02-21 00:12
**/
public class UdpSin01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9998);
while (true) {
//准备数据,控制台读取System.in
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
//读取控制台输入的一行并把它转化为字符
String string = bufferedReader.readLine();
byte[] data = string.getBytes();
DatagramPacket packet = new DatagramPacket(data, 0, data.length,
InetAddress.getByName("localhost"), 9999);
socket.send(packet);
if (string.equals("bye")) {
break;
}
}
socket.close();
}
}
package com.lesson04;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @description: 接收方
* @author: jyf
* @create: 2021-02-21 00:13
**/
public class UdpSin02 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9999);
while (true) {
byte[] buffer = new byte[1024];
//准备接收包裹
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);//阻塞时接收包裹
byte[] data = packet.getData();
String string = new String(data, 0, data.length);
System.out.println(string);
if (string.equals("bye")) {
break;
}
}
socket.close();
}
}
在线咨询:两个人都可以发送
package com.lesson04;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
/**
* @description: 发送线程
* @author: jyf
* @create: 2021-02-21 00:31
**/
public class TalkSend implements Runnable {
private DatagramSocket socket = null;
private BufferedReader bufferedReader = null;
private int port;
private String toIp;
private int toPort;
public TalkSend(int port, String toIp, int toPort) {
this.port = port;
this.toIp = toIp;
this.toPort = toPort;
try {
socket = new DatagramSocket(port);
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
String string = bufferedReader.readLine();
byte[] data = string.getBytes();
DatagramPacket packet = new DatagramPacket(data, 0, data.length,
new InetSocketAddress(this.toIp,this.toPort));
socket.send(packet);
if (string.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package com.lesson04;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* @description: 接收线程
* @author: jyf
* @create: 2021-02-21 00:31
**/
public class TalkReceive implements Runnable {
DatagramSocket socket = null;
int port;
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[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);//阻塞时接收包裹
byte[] data = packet.getData();
String string = new String(data, 0, data.length);
System.out.println(msgFrom + " : " + string);
if (string.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
package com.lesson04;
/**
* @description: 学生端
* @author: jyf
* @create: 2021-02-21 00:42
**/
public class TalkStudent {
public static void main(String[] args) {
// 开启两个线程 学生9999发 9998收
new Thread(new TalkSend(9999, "localhost", 9996)).start();
new Thread(new TalkReceive(9998, "老师")).start();
}
}
package com.lesson04;
/**
* @description: 老师端
* @author: jyf
* @create: 2021-02-21 00:42
**/
public class TalkTeacher {
public static void main(String[] args) {
// 开启两个线程 老师9997发 9996收
new Thread(new TalkSend(9997, "localhost", 9998)).start();
new Thread(new TalkReceive(9996, "学生")).start();
}
}
1.8、URL
https://www.baidu.com/
统一资源定位符:定位资源,定位互联网上某一个资源
DNS域名解析 WWW.baidu
package com.lesson05;
import java.net.URL;
/**
* @description:
* @author: jyf
* @create: 2021-02-21 01:08
**/
public class UrlDemo01 {
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=jay&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()); // 参数
}
}
package com.lesson05;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @description:
* @author: jyf
* @create: 2021-02-21 01:08
**/
public class UrlDown {
public static void main(String[] args) throws Exception {
// 1.下载地址
URL url = new URL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F30%2F29%2F01300000201438121627296084016.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1616433331&t=7eba2de97b643c69dc1bb7cc0d57d6d2");
// 2. 连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("3.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer,0,len);
}
fileOutputStream.close();
inputStream.close();
urlConnection.disconnect();
}
}