網(wǎng)絡(luò)編程
1.1、概述
計算機網(wǎng)絡(luò):
網(wǎng)絡(luò)編程的目的:
無線電臺...傳播交流信息,數(shù)據(jù)交換。通信
想要達到這個效果的需求是什么:
-
如何準確的定位網(wǎng)絡(luò)上的一臺主機。192.168.16.124:端口,定位到這個計算機上的某個資源
-
找到了這個主機,如何傳輸數(shù)據(jù)呢?
Javaweb:網(wǎng)頁編程 B/S (通過瀏覽器訪問)
網(wǎng)絡(luò)編程:TCP/IP C/S
1.2、網(wǎng)絡(luò)通信的要素
TCP/IP參考模型:
小結(jié):
-
網(wǎng)絡(luò)編程中的兩個主要的問題
-
如何準確的定位到網(wǎng)絡(luò)上的一臺或多臺主機
-
找到主機之后如何進行通信
-
-
網(wǎng)絡(luò)編程中的要素
-
IP 和 端口號
-
網(wǎng)絡(luò)通信協(xié)議 udp,tcp
-
-
萬物皆對象
1.3、IP
public class TestInetAddress {
public static void main(String[] args) {
try {
//查詢本機地址
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress);
InetAddress inetAddress1 = InetAddress.getByName("localhost");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getLocalHost();
System.out.println(inetAddress2);
?
//查詢網(wǎng)站ip地址
InetAddress inetAddress3 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress3);
?
//常用方法
// System.out.println(inetAddress3);
System.out.println(inetAddress3.getCanonicalHostName());//規(guī)范名字
System.out.println(inetAddress3.getHostAddress());//ip
System.out.println(inetAddress3.getHostName());//域名,或者電腦自己的名字
?
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
1.4、端口
端口表示計算機上的一個程序的進程:
-
不同的進程有不同的端口號!用來區(qū)分軟件!
-
被規(guī)定0~65535
-
TCP,UDP:65535*2 單個寫一下端口號不能沖突
-
端口分類
-
共有端口0~1023
-
HTTP:80
-
HTTPS:443
-
FTP:21
-
Telent:23
-
-
程序注冊端口:1024~49151,分給用戶或者程序
-
Tomcat : 8080
-
MySQL :3306
-
Oracle :1521
-
-
動態(tài)、私有:49152~65535
-
netstat -ano # 查看搜有點端口
netstat -ano|findstr "5900" # 查看指定的端口
tasklist|findstr "8689" #查看指定端口的進程
Ctrl + shift + Esc #打開任務(wù)管理器
1.5、通信協(xié)議
協(xié)議:約定,就好比我們現(xiàn)在的通話
網(wǎng)絡(luò)通信協(xié)議:速率,傳輸碼率,代碼結(jié)構(gòu),傳輸控制...
問題:非常的復(fù)雜?
大事化?。?/strong>分層!
TCP/IP協(xié)議簇:
重要:
-
TCP:用戶傳輸協(xié)議
-
UDP:用戶數(shù)據(jù)報協(xié)議
出名的協(xié)議:
-
TCP:
-
IP:網(wǎng)絡(luò)互聯(lián)協(xié)議
TCP UDP對比
TCP:打電話
-
連接,穩(wěn)定
-
三次握手,四次揮手
最少需要三次,保證穩(wěn)定連接!
A:你瞅啥?
B:瞅你咋地?
A:干一場!
?
A:我要走了
B:真的要走了嗎?
B:你真的真的要走了嗎?
A:我真的真的要走了
-
客戶端、服務(wù)器
-
傳輸完成,連接釋放,效率低
UDP:發(fā)短信
-
不連接,不穩(wěn)定
-
客戶端、服務(wù)端:沒有明確的界限
-
不管有沒有準備好,都可以發(fā)給你~
-
導(dǎo)彈
-
DDOS:洪水攻擊?。柡凸簦?/span>
1.6、TCP
客戶端
-
連接服務(wù)器Socket
-
發(fā)送消息\
//客戶端
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1.要知道服務(wù)器的地址,端口號
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.創(chuàng)建一個socket連接
socket = new Socket(serverIP,port);
//3.發(fā)送消息
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();
}
}
}
}
}
服務(wù)器
-
建立服務(wù)器的端口ServerSocket
-
等待用戶的連接accept
-
接受用戶的消息
//服務(wù)器端
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();
//管道流
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 {
//關(guān)閉資源
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 TcpClientDemo02 {
public static void main(String[] args) throws Exception {
//1.創(chuàng)建一個Socket連接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.創(chuàng)建一個輸出流
OutputStream os = socket.getOutputStream();
//3.文件流
FileInputStream fis = new FileInputStream(new File("Fischl.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
//告訴服務(wù)器傳輸完了
socket.shutdownOutput();
?
?
//確定服務(wù)器接收完畢,才能斷開連接
InputStream inputStream = socket.getInputStream();
//Sting byte[]
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());
?
?
//關(guān)閉資源
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
?
?
}
}
//服務(wù)器
public class TcpServerDemo02 {
public static void main(String[] args) throws Exception {
//1.創(chuàng)建服務(wù)
ServerSocket serverSocket = new ServerSocket(9000);
//2.監(jiān)聽客戶端連接
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 outputStream = socket.getOutputStream();
outputStream.write("我接收完畢了,你可以斷開了".getBytes());
?
//5.關(guān)閉資源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
Tomcat
服務(wù)端
-
自定義 S
-
Tomcat服務(wù)器 S
客戶端
-
自定義 C
-
瀏覽器 B
1.7、UDP
發(fā)短信:不用連接,需要知道對方的地址!
//發(fā)送端
//不需要連接服務(wù)器
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//1.建立一個Socket
DatagramSocket socket = new DatagramSocket();
?
//2.建個包
String msg = "你好啊,服務(wù)器!";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
?
//數(shù)據(jù),數(shù)據(jù)的長度起始,要發(fā)送給誰
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length,localhost,port);
?
//3.發(fā)送包
socket.send(packet);
?
//4.關(guān)閉流
socket.close();
}
}
//接收端
//還是要等待客戶端的連接!
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
//開放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收數(shù)據(jù)包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);//接收
socket.receive(packet);//阻塞接收
System.out.println(new String(packet.getData()));
System.out.println(packet.getAddress());
System.out.println(packet.getLength());
System.out.println(packet.getPort());
//關(guān)閉連接
socket.close();
}
}
循環(huán)發(fā)送消息
public class UdpSenderDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//準備數(shù)據(jù)
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if (datas.equals("bye")){
break;
}
}
socket.close();
}
}
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();
}
}
1.8、URL
統(tǒng)一資源定位符:定位資源的,定位互聯(lián)網(wǎng)上的某一個資源
DNS域名解析:https://www.baidu.com/ xxx.xxx.xxx.xxx
浙公網(wǎng)安備 33010602011771號