国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

適合個人做的網(wǎng)站有哪些東西嗎百度權(quán)重批量查詢

適合個人做的網(wǎng)站有哪些東西嗎,百度權(quán)重批量查詢,網(wǎng)站后臺怎么修改代碼,手機(jī)價格大全網(wǎng)絡(luò)通信協(xié)議 java.net 包中提供了兩種常見的網(wǎng)絡(luò)協(xié)議的支持: UDP:用戶數(shù)據(jù)報(bào)協(xié)議(User Datagram Protocol)TCP:傳輸控制協(xié)議(Transmission Control Protocol) TCP協(xié)議與UDP協(xié)議 TCP協(xié)議 TCP協(xié)議進(jìn)行通信的兩個應(yīng)用進(jìn)程:客戶端、服務(wù)端 …

網(wǎng)絡(luò)通信協(xié)議

在這里插入圖片描述

java.net 包中提供了兩種常見的網(wǎng)絡(luò)協(xié)議的支持:

  • UDP:用戶數(shù)據(jù)報(bào)協(xié)議(User Datagram Protocol)
  • TCP:傳輸控制協(xié)議(Transmission Control Protocol)

TCP協(xié)議與UDP協(xié)議

TCP協(xié)議

  • TCP協(xié)議進(jìn)行通信的兩個應(yīng)用進(jìn)程:客戶端、服務(wù)端
  • 使用TCP協(xié)議前,須先建立TCP連接,形成基于字節(jié)流的傳輸數(shù)據(jù)通道
  • 傳輸前,采用“三次握手”方式,點(diǎn)對點(diǎn)通信,是可靠
    • TCP協(xié)議使用重發(fā)機(jī)制,當(dāng)一個通信實(shí)體發(fā)送一個消息給另一個通信實(shí)體后,需要收到另一個通信實(shí)體確認(rèn)信息,如果沒有收到另一個通信實(shí)體確認(rèn)信息,則會再次重復(fù)剛才發(fā)送的消息
  • 在連接中可進(jìn)行大數(shù)據(jù)量的傳輸
    傳輸完畢,需釋放已建立的連接,效率低

UDP協(xié)議

  • UDP協(xié)議進(jìn)行通信的兩個應(yīng)用進(jìn)程:發(fā)送端、接收端
  • 將數(shù)據(jù)、源、目的封裝成數(shù)據(jù)包(傳輸?shù)幕締挝?,不需要建立連接
  • 發(fā)送不管對方是否準(zhǔn)備好,接收方收到也不確認(rèn),不能保證數(shù)據(jù)的完整性,故是不可靠
  • 每個數(shù)據(jù)報(bào)的大小限制在64K內(nèi)
  • 發(fā)送數(shù)據(jù)結(jié)束時無需釋放資源,開銷小,通信效率高
  • 適用場景:音頻、視頻和普通數(shù)據(jù)的傳輸。例如視頻會議

生活案例

TCP生活案例:打電話

UDP生活案例:發(fā)送短信、發(fā)電報(bào)

區(qū)別

  • TCP:可靠的連接(發(fā)送數(shù)據(jù)前,需要三次握手、四次揮手),進(jìn)行大數(shù)據(jù)量的傳輸,效率低。
  • UDP:不可靠的連接(發(fā)送前,不需要確認(rèn)對方是否在)、使用數(shù)據(jù)報(bào)傳輸(限制在64kb以內(nèi))、效率高。

TCP協(xié)議

三次握手

TCP協(xié)議中,在發(fā)送數(shù)據(jù)的準(zhǔn)備階段,客戶端與服務(wù)器之間的三次交互,以保證連接的可靠。
在這里插入圖片描述
完成三次握手,連接建立后,客戶端和服務(wù)器就可以開始進(jìn)行數(shù)據(jù)傳輸了。由于這種面向連接的特性,TCP協(xié)議可以保證傳輸數(shù)據(jù)的安全,所以應(yīng)用十分廣泛,例如下載文件、瀏覽網(wǎng)頁等。

四次揮手

TCP協(xié)議中,在發(fā)送數(shù)據(jù)結(jié)束后,釋放連接時需要經(jīng)過四次揮手。

  • 第一次揮手:客戶端向服務(wù)器端提出結(jié)束連接,讓服務(wù)器做最后的準(zhǔn)備工作。此時,客戶端處于半關(guān)閉狀態(tài),即表示不再向服務(wù)器發(fā)送數(shù)據(jù)了,但是還可以接受數(shù)據(jù)。
  • 第二次揮手:服務(wù)器接收到客戶端釋放連接的請求后,會將最后的數(shù)據(jù)發(fā)給客戶端。并告知上層的應(yīng)用進(jìn)程不再接收數(shù)據(jù)。
  • 第三次揮手:服務(wù)器發(fā)送完數(shù)據(jù)后,會給客戶端發(fā)送一個釋放連接的報(bào)文。那么客戶端接收后就知道可以正式釋放連接了。
  • 第四次揮手:客戶端接收到服務(wù)器最后的釋放連接報(bào)文后,要回復(fù)一個徹底斷開的報(bào)文。這樣服務(wù)器收到后才會徹底釋放連接。這里客戶端,發(fā)送完最后的報(bào)文后,會等待2MSL,因?yàn)橛锌赡芊?wù)器沒有收到最后的報(bào)文,那么服務(wù)器遲遲沒收到,就會再次給客戶端發(fā)送釋放連接的報(bào)文,此時客戶端在等待時間范圍內(nèi)接收到,會重新發(fā)送最后的報(bào)文,并重新計(jì)時。如果等待2MSL后,沒有收到,那么徹底斷開。
    在這里插入圖片描述

網(wǎng)絡(luò)編程API

Socket類

基本介紹

在這里插入圖片描述

在這里插入圖片描述

示意圖:

在這里插入圖片描述

理解客戶端、服務(wù)端

  • 客戶端:

    • 自定義
    • 瀏覽器(browser — server)
  • 服務(wù)端:

    • 自定義
    • Tomcat服務(wù)器

TCP網(wǎng)絡(luò)編程

通信模型

在這里插入圖片描述

例題1

客戶端發(fā)送內(nèi)容給服務(wù)端,服務(wù)端將內(nèi)容打印到控制臺上。

public class TCPTest1 {//客戶端@Testpublic void client() {Socket socket = null;OutputStream os = null;try {// 1.創(chuàng)建一個SocketInetAddress inetAddress = InetAddress.getByName("127.0.0.1"); //聲明ip地址int port = 8989; //聲明端口號socket = new Socket(inetAddress,port);// 2.發(fā)送數(shù)據(jù)os = socket.getOutputStream();os.write("你好,我是客戶端,請多多關(guān)照".getBytes());} catch (IOException e) {e.printStackTrace();} finally {// 3.關(guān)閉socket、流try {if (socket != null)socket.close();} catch (IOException e) {e.printStackTrace();}try {if (os != null)os.close();} catch (IOException e) {e.printStackTrace();}}}//服務(wù)端@Testpublic void server() {ServerSocket serverSocket = null;Socket socket = null; //阻塞式方法InputStream is = null;try {// 1.創(chuàng)建一個ServerSocketint port = 8989;serverSocket = new ServerSocket(port);// 2.調(diào)用accept(),接收客戶端的Socketsocket = serverSocket.accept();System.out.println("服務(wù)器端已開啟");System.out.println("收到了來自于" + socket.getInetAddress().getHostAddress() + "的連接");// 3.接收數(shù)據(jù)is = socket.getInputStream();byte[] buffer = new byte[1024];ByteArrayOutputStream baos = new ByteArrayOutputStream(); //內(nèi)部維護(hù)了一個byte[]int len;while ((len = is.read(buffer)) != -1) {// 錯誤的,可能會出現(xiàn)亂碼
//                String str = new String(buffer,0,len);
//                System.out.println(str);//正確的baos.write(buffer,0,len);}System.out.println(baos.toString());System.out.println("\n數(shù)據(jù)接收完畢");} catch (IOException e) {e.printStackTrace();} finally {// 4.關(guān)朗Socket、ServerSocket、流try {if (socket != null) {socket.close();}} catch (IOException e) {e.printStackTrace();}try {if (serverSocket != null) {serverSocket.close();}} catch (IOException e) {e.printStackTrace();}try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}}
}

例題2

客戶端發(fā)送文件給服務(wù)端,服務(wù)端將文件保存在本地。

public class TCPTest2 {//客戶端@Testpublic void client() {// 1.創(chuàng)建Socket// 指明對方(即為服務(wù)器)的ip地址和端口號Socket socket = null;FileInputStream fis = null;OutputStream os = null;try {InetAddress inetAddress = InetAddress.getByName("127.0.0.1");int port = 9090;socket = new Socket(inetAddress,port);// 2.創(chuàng)建File的實(shí)例、FileInputstream的實(shí)例File file = new File("huan.jpg");fis = new FileInputStream(file);// 3.通過Socket,獲取輸出流os = socket.getOutputStream();// 讀寫數(shù)據(jù)byte[] buffer = new byte[1024];int len;while ((len = fis.read(buffer)) != -1) {os.write(buffer,0,len);}System.out.println("數(shù)據(jù)發(fā)送完畢");} catch (IOException e) {e.printStackTrace();} finally {// 4.關(guān)閉Socket和相關(guān)的流try {if (os != null) {os.close();}} catch (IOException e) {e.printStackTrace();}try {if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}try {if (socket != null) {socket.close();}} catch (IOException e) {e.printStackTrace();}}}//服務(wù)端@Testpublic void server() {ServerSocket serverSocket = null;Socket socket = null;InputStream is = null;FileOutputStream fos = null;try {// 1.創(chuàng)建ServerSocketint port = 9090;serverSocket = new ServerSocket(port);// 2.接收來自于客戶端的socket:accept()socket = serverSocket.accept();// 3.通過Socket獲取一個輸入流is = socket.getInputStream();// 4.創(chuàng)建File類的實(shí)例、File0utputstream的實(shí)例File file = new File("huan_copy.jpg");fos = new FileOutputStream(file);// 5.讀寫過程byte[] buffer = new byte[1024];int len;while ((len = is.read(buffer)) != -1) {fos.write(buffer,0,len);}System.out.println("數(shù)據(jù)接收完畢");} catch (IOException e) {e.printStackTrace();} finally {// 6.關(guān)閉相關(guān)的Socket和流try {if (fos != null) {fos.close();}} catch (IOException e) {e.printStackTrace();}try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}try {if (socket != null) {socket.close();}} catch (IOException e) {e.printStackTrace();}try {if (serverSocket != null) {serverSocket.close();}} catch (IOException e) {e.printStackTrace();}try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}}
}

例題3

從客戶端發(fā)送文件給服務(wù)端,服務(wù)端保存到本地。并返回“發(fā)送成功"給客戶端。并關(guān)閉相應(yīng)的連接。

public class TCPTest3 {//客戶端@Testpublic void client() {// 1.創(chuàng)建Socket// 指明對方(即為服務(wù)器)的ip地址和端口號Socket socket = null;FileInputStream fis = null;OutputStream os = null;ByteArrayOutputStream baos = null;InputStream is = null;try {InetAddress inetAddress = InetAddress.getByName("127.0.0.1");int port = 9090;socket = new Socket(inetAddress, port);// 2.創(chuàng)建File的實(shí)例、FileInputstream的實(shí)例File file = new File("huan.jpg");fis = new FileInputStream(file);// 3.通過Socket,獲取輸出流os = socket.getOutputStream();// 4.讀寫數(shù)據(jù)byte[] buffer = new byte[1024];int len;while ((len = fis.read(buffer)) != -1) {os.write(buffer, 0, len);}System.out.println("數(shù)據(jù)發(fā)送完畢");// 5.客戶端表明不再繼續(xù)發(fā)送數(shù)據(jù)socket.shutdownOutput();// 6.接收來自于服務(wù)器端的數(shù)據(jù)is = socket.getInputStream();baos = new ByteArrayOutputStream();byte[] buffer1 = new byte[5];int len1;while ((len1 = is.read(buffer1)) != -1) {baos.write(buffer1, 0, len1);}System.out.println(baos.toString());} catch (IOException e) {e.printStackTrace();} finally {// 7.關(guān)閉Socket和相關(guān)的流try {baos.close();} catch (Exception e) {e.printStackTrace();}try {is.close();} catch (IOException e) {e.printStackTrace();}try {if (os != null) {os.close();}} catch (IOException e) {e.printStackTrace();}try {if (fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}try {if (socket != null) {socket.close();}} catch (IOException e) {e.printStackTrace();}}}//服務(wù)端@Testpublic void server() {ServerSocket serverSocket = null;Socket socket = null;InputStream is = null;FileOutputStream fos = null;OutputStream os = null;try {// 1.創(chuàng)建ServerSocketint port = 9090;serverSocket = new ServerSocket(port);// 2.接收來自于客戶端的socket:accept()socket = serverSocket.accept();// 3.通過Socket獲取一個輸入流is = socket.getInputStream();// 4.創(chuàng)建File類的實(shí)例、File0utputstream的實(shí)例File file = new File("huan_copy2.jpg");fos = new FileOutputStream(file);// 5.讀寫過程byte[] buffer = new byte[1024];int len;while ((len = is.read(buffer)) != -1) {fos.write(buffer, 0, len);}// 6.服務(wù)端發(fā)送數(shù)據(jù)給客戶端os = socket.getOutputStream();os.write("你的圖片很漂亮,我接收到了".getBytes());System.out.println("數(shù)據(jù)接收完畢");} catch (IOException e) {e.printStackTrace();} finally {// 7.關(guān)閉相關(guān)的Socket和流try {os.close();} catch (IOException e) {e.printStackTrace();}try {if (fos != null) {fos.close();}} catch (IOException e) {e.printStackTrace();}try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}try {if (socket != null) {socket.close();}} catch (IOException e) {e.printStackTrace();}try {if (serverSocket != null) {serverSocket.close();}} catch (IOException e) {e.printStackTrace();}try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}}
}

【案例】聊天室

聊天室模型

在這里插入圖片描述

服務(wù)器端

public class ChatSeverTest {//這個集合用來存儲所有在線的客戶端static ArrayList<Socket> online = new ArrayList<Socket>();public static void main(String[] args) throws IOException {//1、啟動服務(wù)器,綁定端口號ServerSocket server = new ServerSocket(8989);//2、接收n多的客戶端同時連接while (true) {Socket accept = server.accept();online.add(accept);//把新連接的客戶端添加到online列表中MessageHandler mh = new MessageHandler(accept);mh.start();}}static class MessageHandler extends Thread {private Socket socket;private String ip;public MessageHandler(Socket socket) {super();this.socket = socket;}@Overridepublic void run() {try {ip = socket.getInetAddress().getHostAddress();//插入:給其他客戶端轉(zhuǎn)發(fā)“我上線了”sendToOther(ip+"上線了");//(1)接收該客戶端的發(fā)送的消息InputStream input = socket.getInputStream();InputStreamReader reader = new InputStreamReader(input);BufferedReader br = new BufferedReader(reader);String str;while ((str = br.readLine()) != null) {//(2)給其他在線客戶端轉(zhuǎn)發(fā)sendToOther(ip + ":" + str);}sendToOther(ip + "下線了");} catch (IOException e) {try {sendToOther(ip + "掉線了");} catch (Exception e1) {e1.printStackTrace();}} finally {//從在線人員中移除我online.remove(socket);}}//封裝一個方法:給其他客戶端轉(zhuǎn)發(fā)xxx消息public void sendToOther(String message) throws IOException {//遍歷所有的在線客戶端,一一轉(zhuǎn)發(fā)for (Socket on : online) {OutputStream every = on.getOutputStream();PrintStream ps = new PrintStream(every);ps.println(message);}}}
}

客戶端

public class ChatClientTest {public static void main(String[] args) throws Exception {//1、連接服務(wù)器Socket socket = new Socket("127.0.0.1", 8989);//2、開啟兩個線程//(1)一個線程負(fù)責(zé)看別人聊,即接收服務(wù)器轉(zhuǎn)發(fā)的消息Receive receive = new Receive(socket);receive.start();//(2)一個線程負(fù)責(zé)發(fā)送自己的話Send send = new Send(socket);send.start();send.join();//等我發(fā)送線程結(jié)束了,才結(jié)束整個程序socket.close();}
}class Send extends Thread{private Socket socket;public Send(Socket socket) {super();this.socket = socket;}@Overridepublic void run() {try {OutputStream outputStream = socket.getOutputStream();//按行打印PrintStream ps = new PrintStream(outputStream);Scanner input = new Scanner(System.in);//從鍵盤不斷的輸入自己的話,給服務(wù)器發(fā)送,由服務(wù)器給其他人轉(zhuǎn)發(fā)while (true) {System.out.println("自己的話:");String str = input.nextLine();if ("bye".equals(str)) {break;}ps.println(str);}input.close();} catch (IOException e) {e.printStackTrace();}}
}class Receive extends Thread{private Socket socket;public Receive(Socket socket) {super();this.socket = socket;}@Overridepublic void run() {try {InputStream inputStream = socket.getInputStream();Scanner input = new Scanner(inputStream);while (input.hasNextLine()) {String line = input.nextLine();System.out.println(line);}} catch (IOException e) {e.printStackTrace();}}
}

UDP網(wǎng)絡(luò)編程

通信模型

在這里插入圖片描述

示例

public class UDPTest {//發(fā)送端@Testpublic void sender() throws Exception {//1.創(chuàng)建DatagramSocket的實(shí)例DatagramSocket ds = new DatagramSocket();//2、將數(shù)據(jù)、目的地的ip,目的地的端口號部封裝在DatagramPacket數(shù)據(jù)報(bào)中InetAddress inetAddress = InetAddress.getByName("127.0.0.1");int port = 9090;byte[] bytes = "我是發(fā)送端".getBytes("utf-8");DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, inetAddress, port);//發(fā)送數(shù)據(jù)ds.send(packet);ds.close();}//接收端@Testpublic void receiver() throws IOException {//1.創(chuàng)建DatagramSocket的實(shí)例int port = 9090;DatagramSocket ds = new DatagramSocket(port);//2. 創(chuàng)建數(shù)據(jù)報(bào)的對象,用于接收發(fā)送端發(fā)送過來的數(shù)據(jù)byte[] buffer = new byte[1024 * 64];DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);//3.接收數(shù)據(jù)ds.receive(packet);//4.獲取數(shù)據(jù),并打印到控制臺上String str = new String(packet.getData(), 0, packet.getLength());System.out.println(str);ds.close();}
}
http://aloenet.com.cn/news/30655.html

相關(guān)文章:

  • 英語機(jī)構(gòu)網(wǎng)站建設(shè)方案足球進(jìn)球排行榜
  • 做網(wǎng)站的困難windows優(yōu)化大師會員兌換碼
  • 做網(wǎng)站學(xué)什么專業(yè)防惡意點(diǎn)擊軟件
  • 南寧網(wǎng)站建設(shè)設(shè)計(jì)制作今日最新抗疫數(shù)據(jù)
  • 做代購有哪些網(wǎng)站有哪些seo優(yōu)化培訓(xùn)課程
  • 做聊天網(wǎng)站的視頻教程怎么進(jìn)行網(wǎng)站關(guān)鍵詞優(yōu)化
  • wordpress主題如何修改seo優(yōu)化設(shè)計(jì)
  • 做網(wǎng)站需要注意的點(diǎn)抖音seo供應(yīng)商
  • 山西成寧做的網(wǎng)站運(yùn)營推廣渠道有哪些
  • wordpress下載站源碼營銷推廣公司案例
  • 安徽建筑大學(xué)學(xué)工在線網(wǎng)站蘇州網(wǎng)站制作推廣
  • 廣西代理網(wǎng)站建設(shè)公司百度網(wǎng)址提交入口
  • 做網(wǎng)站建設(shè)站長工具櫻花
  • 甘肅網(wǎng)站建設(shè)制作商競價賬戶托管公司
  • 網(wǎng)站開發(fā)集成環(huán)境seo優(yōu)化工作怎么樣
  • 網(wǎng)站建設(shè)招標(biāo)評分標(biāo)準(zhǔn)福州網(wǎng)站制作推廣
  • wordpress的數(shù)據(jù)庫在那里關(guān)鍵詞優(yōu)化建議
  • dw怎么做網(wǎng)站輪播圖企業(yè)宣傳方式有哪些
  • 網(wǎng)站備案 太煩個人免費(fèi)域名注冊網(wǎng)站
  • 企業(yè)網(wǎng)站建設(shè)與維護(hù)運(yùn)營愛站網(wǎng)能不能挖掘關(guān)鍵詞
  • 徐州哪家做網(wǎng)站好網(wǎng)站的seo方案
  • 海城網(wǎng)站制作建設(shè)高效統(tǒng)籌疫情防控和經(jīng)濟(jì)社會發(fā)展
  • java做網(wǎng)站步驟網(wǎng)推
  • 做兼職的網(wǎng)站企業(yè)網(wǎng)站seo優(yōu)化公司
  • 整站策劃營銷型網(wǎng)站建設(shè)網(wǎng)站優(yōu)化b2b網(wǎng)站有哪些平臺
  • dede自適應(yīng)網(wǎng)站注意事項(xiàng)營銷網(wǎng)站方案設(shè)計(jì)
  • 做電商必須知道的網(wǎng)站短視頻關(guān)鍵詞優(yōu)化
  • 做網(wǎng)站業(yè)務(wù)提成多少it培訓(xùn)
  • 公司名稱大全兩個字引擎搜索優(yōu)化
  • wordpress用戶登陸武漢seo優(yōu)化服務(wù)