1. 程式人生 > 其它 >網路程式設計基礎

網路程式設計基礎

網路程式設計兩大問題(寫一封信給某個人)

  • 如何定位網路上的一臺或多臺主機,定位主機上的應用(寫給人的地址)

  • 如何進行可靠高效進行資料傳輸(需要貼什麼郵票)

解決問題一:IP和埠號

解決問題二:提供網路通訊協議:TCP/IP參考模型

IP和埠

IP地址

1.唯一標識Internet上的計算機(通訊實體)

2.在Java中使用InetAddress類代表IP

3.分類方式1:IPV4和IPV6

分類方式2:全球資訊網 和 區域網

4.域名:代替IP,方便記憶 例如:www.baidu.com

5.本地迴路地址:120.0.0.1 對應:localhost

6.如何例項化:兩種 getByName(String host) getLocalHost()

兩個常用方法:getHostName() 獲取域名

getHostAddress() 獲取地址IP

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
public static void main(String[] args) {
try {
InetAddress byName = InetAddress.getByName("www.baidu.com");
System.out.println(byName.toString());

InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);

System.out.println(byName.getHostName());
System.out.println(byName.getHostAddress());

} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}

埠號

1.標識正在計算機上執行的程序(程式)

2.不同程序要有不同的埠號

3.範圍: 16位的整數 0-65535

埠號和IP地址的組合得出一個網路套接字:Socket

協議

TCP協議

三次握手(建立,確保兩個人都在 客戶端向伺服器發訊息問在不在,伺服器回訊息在,客戶端回覆)

四次揮手(釋放,兩個人都先對方發了訊息說要斷開,各自再回一條資訊,客戶端先,伺服器發的時候,客戶端回訊息只是印證伺服器也沒有關閉)

可靠,

需要釋放連線,效率低

UDP協議

將資料,源,目的封裝在資料包

不可靠

速度快,無需釋放資源,開銷小

TCP

//客戶端
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
try {
// 設定IP和埠
InetAddress inetAddress = InetAddress.getByName(InetAddress.getLocalHost().getHostAddress());
socket = new Socket(inetAddress, 8899);

// 獲得一個輸出流,用於輸出資料
outputStream = socket.getOutputStream();

// 輸出資料,可以是一句話也可以是文件圖片(涉及IO流)
outputStream.write("您好,我是客戶端mm".getBytes());

// socket.shutdownOutput();
// 當然也可以從伺服器收到訊息,那麼上面就要加以句
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


// 伺服器
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {

// 1.建立伺服器端的SeverSocket,指明自己的埠號
serverSocket = new ServerSocket(8899);

// 2.用accept(),表示接收來自客戶端的socket
socket = serverSocket.accept();

// 3.獲取輸出流
inputStream = socket.getInputStream();

// 4.讀取輸入流的資料
byteArrayOutputStream = new ByteArrayOutputStream();

int len;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer,0,len);
}

System.out.println(byteArrayOutputStream.toString());

// 如果想要給客戶端傳送點什麼
// socket.getOutputStream(),獲取輸出流,然後在進行操作
} catch (IOException e) {
e.printStackTrace();
} finally {
// 5.關閉
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.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();
}
}
}
}

UDP

DatagramSocket

DatagramPacket

    s@Test
public void sender() throws IOException {

DatagramSocket datagramSocket = new DatagramSocket();

String str = "UDP傳送方式";
byte[] bytes = str.getBytes();
InetAddress inetAddress = InetAddress.getLocalHost();
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length,inetAddress,8899);

datagramSocket.send(datagramPacket);

datagramSocket.close();
}


@Test
public void receiver() throws IOException {

DatagramSocket datagramSocket = new DatagramSocket(8899);

byte[] bytes = new byte[200];
DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);

datagramSocket.receive(datagramPacket);

System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));

datagramSocket.close();
}

URL

統一資源定位符

@Test
public void downLoad() {

HttpsURLConnection urlContent = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL("https://pic.cnblogs.com/avatar/2183514/20201017110603.png");

// 開啟連線
urlContent = (HttpsURLConnection) url.openConnection();

// 連線
urlContent.connect();

// 獲得輸出流
inputStream = urlContent.getInputStream();

fileOutputStream = new FileOutputStream("1.png");

int len;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes,0,len);
}
System.out.println("下載完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (urlContent != null) {
urlContent.disconnect();
}
}

}