文章目錄
- 1 概述
- 2 網路通信要素
- 3 IP地址
- 4 埠
- 5 TCP
- 6 檔案上傳
- 7 UDP
- 7.1 基礎應用
- 7.2 UDP聊天室
1 概述
計算機網路是將地理位置不同的具有獨立功能的多臺計算機及其外部設備,通過通信線路鏈接起來,在網路作業系統、網路管理軟體及網路通信協議的管理和協調下,實作資源共享和資訊傳遞的計算機系統,
2 網路通信要素
通信雙方地址
- ip
- 埠號
通訊協議
- 應用層: HTTP
- 表示層
- 會話層
- 傳輸層:TCP UDP
- 網路層: IP
- 資料鏈路層
- 物理層
思考
- 如何準確的定位到網路的一臺或者多臺主機? 通過IP找到不同的主機
- 找到主機之后如何進行通信? 通過不同的埠區分不同的程式
3 IP地址
對應的java類:InetAddress
- 唯一定位一臺網路上的計算機
- 127.0.0.1 :本機localhost
ip地址分類
-
IPV4/ IPV6
- IPV4 32位 , 4個位元組組成,一共42個億,30億在北美,亞洲4億,2011年用盡
- IPV6 128位, 8個無符號整數
-
公網(互聯網)/私網(局域網)
- 公網
- 私網
- ABCD類
域名:為了記憶方便,網站一般都通過域名系結IP地址
域名 ----》 DNS服務器決議出IP ----》 通過IP進行訪問
4 埠
埠表示計算機上一個程式的行程
-
不同的行程有不同的埠號!用來區分軟體
-
被規定0-65535
-
TCP UDP: 65535*2 同一協議埠不能相同
-
公共埠 0 ~1023
-
HTTP:80
-
HTTPS:443
-
FTP:21
-
Telent:23
-
程式注冊埠:1024~49151
-
動態~私有 49152 - 65535
查看埠 netstat -ano #查看所有的埠
windows:netstat -ano | findstr “5900”
linux: netstat -ano | grep"5900"
5 TCP
客戶端
- 鏈接服務器socket
- 發送訊息
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
// tcp客戶端
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");
int port = 9999;
// 2 創建一個socket鏈接
socket = new Socket(serverIP, port);
// 3 發送資訊 IO流
os = socket.getOutputStream();
os.write("hello server".getBytes(StandardCharsets.UTF_8));
} 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();
}
}
}
}
}
服務器
- 建立服務的埠
- 等待用戶的鏈接accept
- 接收用戶的訊息
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
// tcp服務器
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){
// 2 等待客戶端鏈接過來
socket = serverSocket.accept();
// 3 讀取客戶端得訊息
is = socket.getInputStream();
// 4 管道流
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();
}
}
}
}
}
6 檔案上傳
客戶端
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo02 {
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("A.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();
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());
// 5 關閉資源
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
}
}
服務器
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class TcpServerDemo02 {
public static void main(String[] args) throws Exception {
// 1 創建服務
ServerSocket serverSocket = new ServerSocket(9000);
// 2 監聽客戶端鏈接,
Socket socket = serverSocket.accept(); // 阻塞監聽,會一直等待客戶端鏈接
// 3 獲取輸入流
InputStream is = socket.getInputStream();
// 4 檔案輸出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
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(StandardCharsets.UTF_8));
// 5 關閉資源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
7 UDP
7.1 基礎應用
UDP沒有客戶端、服務器的概念,只有發送端和接收端
發送端代碼
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
// 1 建立一個socket
DatagramSocket socket = new DatagramSocket();
// 2 需要一個包
String msg = "hello server";
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, InetAddress.getByName("localhost"), 9090);
// 3 發送包
socket.send(packet);
// 4 關閉資源
socket.close();
}
}
接收端代碼
import java.net.DatagramPacket;
import java.net.DatagramSocket;
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); // 阻塞接收
// 列印接收引數
System.out.println(packet.getAddress().getHostAddress() + "--->" + new String(packet.getData(), 0, packet.getLength()));
// 關閉鏈接
socket.close();
}
}
7.2 UDP聊天室
回圈發送端

回圈接收端

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/352201.html
標籤:java
上一篇:Java中Int、Integer、Integer.valueOf()、new Integer()之間的區別
下一篇:java---你真的認識例外嗎?
