網路通訊的要素
阿新 • • 發佈:2020-08-10
網路通訊的要素
目錄
1. IP地址
InetAddress
-
唯一定位一臺網路上的計算機
-
127.0.0.1:本機 localhost
-
IP地址的分類
-
IPV4/IPV 6
-
IPV4:127.0.0.1 4個位元組組成,0-255,42億個;30億都在北美,亞洲4億
-
IPV6:128位。8個無符號整數(十六進位制,abcde)
2001:0bb2:aaaa:0015:0000:0000:1aaa:1312
-
-
公網(網際網路)-私網(區域網)
-
192.168.XX.XX 專門給組織內部使用的
-
ABCD類地址
-
-
域名:解決記憶IP問題
2. Java中的InetAddress包
package com.wang.lesson01; import java.net.InetAddress; import java.net.UnknownHostException; /** * @author wang * @creatTime 2020/8/7 */ //測試IP public class Test01 { public static void main(String[] args) { //快捷鍵 clt+alt+t 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()); //ip System.out.println(inetAddress2.getHostName()); //域名,或者本機的名字 } catch (UnknownHostException e) { e.printStackTrace(); } } }
3. 埠
埠表示計算機上的一個程式的程序
-
不同的程序有不同的埠號!用來區分軟體!
-
被規定為0~65535
-
TCP,UDP:單個協議下,埠號不能衝突
-
埠分類
-
共有埠0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
-
程式註冊埠:1024~49151,分配給使用者或者程式
- Tomcat:8080
- MySQL:3306
- Orale:1521
-
動態、私有:49152~65535
netstat -ano #檢視所有的埠 netstat -ano|findstr "5900" #檢視指定的埠 tasklist|findstr "8696" #檢視指定埠的程序 Ctrl + Shift + Esc #開啟工作管理員
package com.wang.lesson01; import java.net.InetSocketAddress; /** * @author wang * @creatTime 2020/8/7 */ public class TestIntSocketAddress { public static void main(String[] args) { InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080); System.out.println(inetSocketAddress.toString()); InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 8080); System.out.println(inetSocketAddress2.toString()); System.out.println(inetSocketAddress.getAddress()); System.out.println(inetSocketAddress.getHostName()); //地址 System.out.println(inetSocketAddress.getPort()); //埠 } }
-
4. 通訊協議
協議:約定
網路通訊協議:速率,傳輸位元速率,程式碼結構,傳輸控制
問題:複雜
解決方法:大事化小---->分層
TCP/IP協議簇:實際上是一種協議
重要
- TCP:使用者傳輸協議
- UDP:使用者資料報協議
出名的協議
- TCP
- IP:網路互連協議
TCP UDP對比
TCP:打電話
- 連線,穩定
- 三次握手,四次揮手
- 客戶端,服務端
- 傳輸完成,釋放連線,效率低
UDP:發簡訊
- 不連線,不穩定
- 客戶端,服務端:沒有明確的界限
- 不管有沒有準備好,都可以發給你
- 類比:導彈
- DDOS:飽和攻擊
5. TCP
1. 客戶端
- 連線伺服器Socket
- 傳送訊息
package com.wang.lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author wang
* @creatTime 2020/8/7
*/
//客戶端
public class TCPClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1.要知道伺服器的地址
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
//2. 埠號
int port = 9999;
//3. 建立一個socket連線
socket = new Socket(serverIP, port);
//4. 傳送訊息IO流
os = socket.getOutputStream();
os.write("這是一行字".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2. 伺服器
- 建立服務端的埠
- 等待使用者的連線accept(),返回客戶的socket
- 接收使用者的訊息
package com.wang.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author wang
* @creatTime 2020/8/7
*/
//服務端
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);
//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());
} 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();
}
}
}
}
}
3. 檔案上傳
伺服器端
package com.wang.lesson02;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
/**
* @author wang
* @creatTime 2020/8/7
*/
public class TCPServerDemo02 {
public static void main(String[] args) throws Exception{
//1.建立服務
ServerSocket serverSocket = new ServerSocket(9999);
//2.監聽客戶端的連線
Socket socket = serverSocket.accept();//阻塞式監聽,會一直等待客戶端連線
//3.獲取輸入流
InputStream is = socket.getInputStream();
//4.檔案輸出,由於同名的檔案存在時會丟擲異常,因此先刪除同名檔案
try{
Files.delete(Paths.get("pic_receive.jpg"));
Files.copy(is, Paths.get("pic_receive.jpg"));
} catch (NoSuchFileException e) {
//如果同名檔案不存在,則在NoSuchFileException中直接複製
Files.copy(is, Paths.get("pic_receive.jpg"));
}
//通知客戶端我接收完畢了
OutputStream os = socket.getOutputStream();
os.write("我接收完畢了,你可以斷開了".getBytes());
is.close();
socket.close();
serverSocket.close();
}
}
客戶端
package com.wang.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author wang
* @creatTime 2020/8/7
*/
public class TCPClientDemo02 {
public static void main(String[] args) throws Exception{
//1. 建立一個socket連線
Socket socket = new Socket(InetAddress.getByName("localhost"), 9999);
//2. 建立一個輸出流
OutputStream os = socket.getOutputStream();
//檔案流 Files用copy寫到輸入輸出流,用readAllXXX讀進來
//3. 讀取檔案
Path filePath = Paths.get("pic.jpg");
//4. 寫出檔案
Files.copy(filePath, os);
//通知伺服器,我已經結束了
socket.shutdownOutput(); //我已經傳輸完了
//確定伺服器接收完畢,才能斷開連線
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream 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());
//5.關閉資源
baos.close();
inputStream.close();
os.close();
socket.close();
}
}
6. Tomcat
服務端
- 自定義 S
- Tomcat S:Java後臺開發
客戶端
- 自定義 C
- 瀏覽器 B
7. UDP
發簡訊:不用連線,需要知道對方的地址!
1. UDP實現傳送訊息
package com.wang.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @author wang
* @creatTime 2020/8/10
*/
//不需要連線伺服器
public class UDPClientDemo01 {
public static void main(String[] args) throws Exception {
//1.建立一個socket
DatagramSocket socket = new DatagramSocket();
//2.建個包
String msg = "Hello, Server!";
//傳送給誰
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();
}
}
UDP實現接收訊息
package com.wang.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @author wang
* @creatTime 2020/8/10
*/
//還要等待客戶端的連線
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);
socket.receive(packet); //阻塞接收
//關閉連線
socket.close();
//public String(byte[] bytes,int index,int length) 把位元組陣列的一部分轉成字串
System.out.println(new String(packet.getData(), 0, packet.getData().length));
}
}
2. 諮詢
迴圈傳送訊息
package com.wang.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
* @author wang
* @creatTime 2020/8/10
*/
public class UDPSenderDemo01 {
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[] dataBytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(dataBytes, 0, dataBytes.length, new InetSocketAddress("localhost", 6666));
socket.send(packet);
if (data.equals("bye"))
break;
}
socket.close();
}
}
迴圈接收訊息
package com.wang.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @author wang
* @creatTime 2020/8/10
*/
public class UDPReceiveDemo01 {
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 receiveData = new String(data, 0, data.length);
System.out.println(receiveData);
if (receiveData.equals("bye"))
break;
}
socket.close();
}
}
3. 線上諮詢
兩個人都可以是傳送方,也都可以是接收方:使用多執行緒
迴圈傳送訊息
package com.wang.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
/**
* @author wang
* @creatTime 2020/8/10
*/
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 (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
//準備資料:控制檯讀取System.in
while (true) {
try {
String data = reader.readLine();
byte[] dataBytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(dataBytes, 0, dataBytes.length, new InetSocketAddress(this.toIP, this.toPort));
socket.send(packet);
if (data.equals("bye"))
break;
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
迴圈接收訊息
package com.wang.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* @author wang
* @creatTime 2020/8/10
*/
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() {
try {
while (true) {
//準備接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet); //阻塞式接收包裹
//當接收到"bye",斷開連線
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(msgFrom + ":" + receiveData);
if (receiveData.equals("bye"))
break;
}
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
}
}
老師端
package com.wang.chat;
/**
* @author wang
* @creatTime 2020/8/10
*/
public class TalkTeacher {
public static void main(String[] args) {
//開啟兩個執行緒
new Thread(new TalkSend(5555, "localhost", 8888)).start();
new Thread(new TalkReceive(9999, "學生")).start();
}
}
學生端
package com.wang.chat;
/**
* @author wang
* @creatTime 2020/8/10
*/
public class TalkStudent {
public static void main(String[] args) {
//開啟兩個執行緒
new Thread(new TalkSend(7777, "localhost", 9999)).start();
new Thread(new TalkReceive(8888, "老師")).start();
}
}
8. URL
統一資源定位符:定位網際網路上的某一個資源
DNS域名解析:www.baidu.com ===> XXX.XX.XX.X
組成:
協議://ip地址:埠/專案名/資源
可以少但是不能多
package com.wang.lesson04;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author wang
* @creatTime 2020/8/10
*/
public class URLDemo01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=wang&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()); //引數
}
}
利用URL下載檔案
package com.wang.lesson04;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author wang
* @creatTime 2020/8/10
*/
public class URLDown {
public static void main(String[] args) throws Exception {
//1.下載地址
URL url = new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597050462137&di=e81d42b6c04eeff13b8011bdcdb7b5e7&imgtype=0&src=http%3A%2F%2Ft8.baidu.com%2Fit%2Fu%3D1484500186%2C1503043093%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D1280%26h%3D853");
//2.連線到這個資源 用Http
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
//利用Path和Files簡化檔案的IO
String filePath = "download_pic.jpg";
Path path = Paths.get(filePath);
//考慮到可能存在同名檔案,先刪除同名檔案,再copy
try {
Files.delete(path);
Files.copy(inputStream, path);
} catch (NoSuchFileException e) {
//不存在同名檔案,會丟擲NoSuchFileException,此時直接copy
Files.copy(inputStream, path);
}
inputStream.close();
}
}