网络编程
1.1、概述
-
计算机网络:将地理位置不同具有独立功能的多台计算机及外部设备,通过通信线路连接起来,在网络操作系统、网络管理软件和网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
-
网络编程的目的:数据交换,通信,无线电台
-
条件:
- 如何准确定位网络上的一台计主机?地址和端口,资源定位
- 找到了主机如何传输数据?
javaweb:网页编程 B/S
网络编程:TCP/IP C/S
1.2、网络通信的要素
如何实现网络通信?
-
双方通信地址:
- ip
- 端口
-
规则:网络通信的协议
TCP/IP参考模型:
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 2001:0bb2:aaaa:0015:0000:0000:1aaa:1312:128位,8个无符号整数
- 公网(互联网)/私网(局域网)
- ABCD地址
- 192.168.xx.xx,组织专用
- ipv4/ipv6
-
域名:记忆Ip
//测试IP public class A_InetAddress { public static void main(String[] args) { try { //查询本机地址 InetAddress address1 = InetAddress.getByName("127.0.0.1"); System.out.println(address1); InetAddress address3 = InetAddress.getByName("localhost"); System.out.println(address3); InetAddress address4 = InetAddress.getLocalHost(); System.out.println(address4); //查询网站地址 InetAddress address2 = InetAddress.getByName("www.baidu.com"); System.out.println(address2); System.out.println(address2.getHostAddress()); //ip System.out.println(address2.getCanonicalHostName()); //规范域名 System.out.println(address2.getHostName()); //域名或者电脑名字 } catch (UnknownHostException e) { e.printStackTrace(); } } }
1.4、端口
端口表示计算机上一个程序的进程
-
不同的进程有不同的端口号!用来区分软件
-
被规定 0~65535
-
TCP端口、UDP端口:65532*2,TCP用了80端口,UDP仍然可以用,但是同一协议只能用一个端口
-
端口分类
-
公有端口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|findstr "14724" #查看指定端口进程 ctrl+shift+esc 打开任务管理器
//测试端口 public class B_InetSocketAddress { public static void main(String[] args) { InetSocketAddress address1 = new InetSocketAddress("127.0.0.1",8080); InetSocketAddress address2= new InetSocketAddress("LocalHost",8080); System.out.println(address1); System.out.println(address2); System.out.println(address1.getAddress()); System.out.println(address1.getHostName()); System.out.println(address1.getPort()); } }
-
扩展概念:套接字和端口
套接字是传输层和应用层的接口
和网络层和传输层使用的端口是有区别的。
套接字可理解为:IP+端口。两个都是传输层以上的概念
套接字是支持TCP/IP网络通信的基本操作单元。多个TCP连接或多个应用程序进程可能需要通过同一个 TCP协议端口传输数据。为了区别不同的应用程序进程和连接,许多计算机操作系统为应用程序与TCP/IP协议交互提供了称为套接字(Socket)的接口。
套接字是用网络节点的IP地址和端口号来标识的。
扩展概念:套接字和端口
套接字是传输层和应用层的接口和网络层和传输层使用的端口是有区别的。
套接字可理解为:IP+端口。两个都是传输层以上的概念
套接字是支持TCP/IP网络通信的基本操作单元。多个TCP连接或多个应用程序进程可能需要通过同一个 TCP协议端口传输数据。为了区别不同的应用程序进程和连接,许多计算机操作系统为应用程序与TCP/IP协议交互提供了称为套接字(Socket)的接口。套接字是用网络节点的IP地址和端口号来标识的。
1.5、通信协议
-
协议:约束
-
网络通信协议:速率,传输码率,代码结构,传输控制等
-
TCP/IP协议簇:实际上是一组协议
重要:
- TCP:用户传输协议
- UDP:用户数据报协议
- IP:网络互联协议
-
TCP/UDP对比
TCP:打电话
1.连接,稳定
2.三次握手、四次挥手
3.客户端、服务端
4.传输完成,释放连接,效率低
UDP:发短信
1.不连接,不稳定
2.客户端、服务端:没有明确的界限
3.不管有没有准备好,都可以发送
4.DDOS:洪水攻击!(饱和攻击)
1.6、TCP
客户端
- 通过socket连接服务器
- 发送消息
//客户端
public class A_TcpClient {
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.发送消息
os = socket.getOutputStream();
os.write("Hello,I'm Darwin".getBytes(StandardCharsets.UTF_8));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException 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等待用户连接
- 接收用户消息
- 输出消息通过管道流 ByteArrayOutputStream,
//服务器
public class A_TcpServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
InputStream is = null;
Socket socket = null;
ByteArrayOutputStream baos =null;
try {
//1.需要有一个地址
serverSocket = new ServerSocket(9999);
//2.等待客户端链接过来
socket = serverSocket.accept();
//3.读取客户端消息
is = socket.getInputStream();
//管道流,通过ByteArrayOutputStream写出去
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) {
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();
}
}
}
}
}
文件上传
//测试文件上传
public class B_TcpClient {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
FileInputStream fis =null;
ByteArrayOutputStream baos = null;
try {
//1.创建一个Socket连接
socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
//2.创建一个输出流
os = socket.getOutputStream();
//3.文件流
fis = new FileInputStream(new File("Bill.jpg"));
//4.写出文件
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//通知服务器发送结束
socket.shutdownOutput();
//确定服务器接收完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
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());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class B_TcpServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream os = null;
try {
//1.创建服务
serverSocket = new ServerSocket(9000);
//2.监听客户端连接
socket = serverSocket.accept();//阻塞时监听,会一直等待客户端,直到连接
//3.获取输入流
is = socket.getInputStream();
//4.管道输出流
fos = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer)) != -1){
fos.write(buffer);
}
//通知客户端接收完毕
os = socket.getOutputStream();
os.write("我接收完毕了".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fos.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Tomcat
服务端
- 自定义 S
- Tomcat S
客户端
- 自定义 C
- 浏览器 B
由此可得C/S,B/S分别是什么
1.7、UDP
发送消息
不需要建立连接
- 发送端
//UDP测试
// 不需要连接服务器
public class A_UDPClient {
public static void main(String[] args) throws Exception {
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.建个包
String msg = "Hello Darwin";
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.close
socket.close();
}
}
- 接收端
public class A_UDPServer {
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();
}
}
咨询
- 发送方1
public class A_UDPSender {
public static void main(String[] args)throws Exception {
DatagramSocket socket = new DatagramSocket(9999);
//准备数据,控制台读取system.in
while (true) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String data = reader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("Localhost", 6666));
socket.send(packet);
if (data.equals("Bye")){
break;
}
}
socket.close();
}
}
- 发送方2
public class B_UDPSender {
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); //阻塞式接收包裹
//断开连接
byte[] data = packet.getData();
String receiveData = new String(data,0, packet.getLength());
System.out.println(receiveData);
if (receiveData.equals("Bye")){
break;
}
}
在线咨询:两方既是发送方也是接收方
通过线程实现,编写两个线程,一个用来发送一个用来接收,然后两个类分别对应两个用户
- 接收
public class TalkReceive implements Runnable {
DatagramSocket socket = null;
private String msgFrom;
private int port;
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);
socket.receive(packet); //阻塞式接收包裹
//断开连接
byte[] data = packet.getData();
String receiveData = new String(data,0, packet.getLength());
System.out.println(msgFrom+":"+receiveData);
if (receiveData.equals("Bye")){
break;
}
}catch(Exception e){
e.printStackTrace();
}
}
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);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try{
String data = reader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress(this.toIP, this.toPort));
socket.send(packet);
if (data.equals("Bye")){
break;
}
}catch(Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
1.8、URL
统一资源定位符:定位互联网上的某一资源
//下载网络资源
public class URLDown {
public static void main(String[] args) throws Exception {
//1.地址
URL url = new URL("https://m701.music.126.net/20211103224405/5b0fde3424de7eb8c3c46920f6998f4e/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/11193656416/5d72/24c3/ca30/95c8d9b01c8679bb6f4eda260bc09954.m4a");
//2.链接到资源
HttpURLConnection con = (HttpURLConnection) url.openConnection();
InputStream is = con.getInputStream();
FileOutputStream fos = new FileOutputStream("4.m4a");
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
con.disconnect();
}
}