最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java小白第一次就能看懂的網(wǎng)絡(luò)編程

 更新時間:2021年08月30日 11:56:11   作者:扛麻袋的少年  
網(wǎng)絡(luò)編程是指編寫運行在多個設(shè)備(計算機)的程序,這些設(shè)備都通過網(wǎng)絡(luò)連接起來。本文介紹了一些網(wǎng)絡(luò)編程基礎(chǔ)的概念,并用Java來實現(xiàn)TCP和UDP的Socket的編程,來讓讀者更好的了解其原理

一、網(wǎng)絡(luò)基礎(chǔ)

二、網(wǎng)絡(luò)協(xié)議

 實現(xiàn)TCP的網(wǎng)絡(luò)編程
 例子1:客戶端發(fā)送信息給服務(wù)端,服務(wù)端將數(shù)據(jù)顯示在控制臺上
public class TCPTest1 {
    //客戶端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            //1.創(chuàng)建Socket對象,指明服務(wù)器端的ip和端口號
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 8899);
            //2.獲取一個輸出流,用于輸出數(shù)據(jù)
            os = socket.getOutputStream();
            //3.寫出數(shù)據(jù)的操作
            os.write("你好,我是客戶端mm".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.資源的關(guān)閉
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
    //服務(wù)端
    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1.創(chuàng)建服務(wù)器的ServerSocket,指明自己的端口號
            ss = new ServerSocket(8899);
            //2.調(diào)用accept()表示接收來自于客戶端的socket
            socket = ss.accept();
            //3.獲取輸入流
            is = socket.getInputStream();
            //不建議這樣寫,可能會有亂碼
//        byte[] buffer = new byte[1024];
//        int len;
//        while((len = is.read(buffer)) != -1){
//            String str = new String(buffer,0,len);
//            System.out.println(str);
//        }
            //4.讀取輸入流中的數(shù)據(jù)
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while((len = is.read(buffer)) != -1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.關(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(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 實現(xiàn)TCP的網(wǎng)絡(luò)編程
 例題2:客戶端發(fā)送文件給服務(wù)端,服務(wù)端將文件保存在本地。
public class TCPTest2 {
    //這里異常處理的方式應(yīng)該使用try-catch-finally
    @Test
    public void client() throws IOException {
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
        OutputStream os = socket.getOutputStream();
        FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len = fis.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        fis.close();
        os.close();
        socket.close();
    }
    //這里異常處理的方式應(yīng)該使用try-catch-finally
    @Test
    public void server() throws IOException {
        ServerSocket ss = new ServerSocket(9090);
        Socket socket = ss.accept();
        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream(new File("beauty1.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        fos.close();
        is.close();
        socket.close();
        ss.close();
    }
}
 實現(xiàn)TCP的網(wǎng)絡(luò)編程
 例題3:從客戶端發(fā)送文件給服務(wù)端,服務(wù)端保存到本地,并返回"發(fā)送成功"給客戶端。并關(guān)閉相應(yīng)的連接
 
public class TCPTest3 {
    @Test
    public void client() throws IOException {
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
        OutputStream os = socket.getOutputStream();
        FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len = fis.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        //服務(wù)區(qū)端給予客戶端反饋
        OutputStream os1 = socket.getOutputStream();
        os.write("你好,美女,照片我以收到,非常漂亮!".getBytes());
        fis.close();
        os.close();
        socket.close();
        os1.close();
 
    }
    //這里異常處理的方式應(yīng)該使用try-catch-finally
    @Test
    public void server() throws IOException {
        ServerSocket ss = new ServerSocket(9090);
        Socket socket = ss.accept();
        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream(new File("beauty2.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while((len = is.read(buffer)) != -1){
            fos.write(buffer,0,len);
        }
        //接受來自于服務(wù)器端的數(shù)據(jù),并顯示到控制臺上
        InputStream is1 = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bufferr = new byte[20];
        int len1;
        while((len1 = is1.read(buffer)) != -1){
            baos.write(buffer,0,len1);
        }
        System.out.println(baos.toString());
        fos.close();
        is.close();
        socket.close();
        ss.close();
        baos.close();
    }
}

UDP協(xié)議的網(wǎng)絡(luò)編程
public class UDPTest {
    @Test
    public void sender() throws IOException {
        DatagramSocket socket = new DatagramSocket();
 
        String str = "我是UDP方式發(fā)送的導(dǎo)彈";
        byte[] data = str.getBytes();
        InetAddress inet = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
        socket.send(packet);
        socket.close();
    }
    @Test
    public void receiver() throws IOException {
        DatagramSocket socket = new DatagramSocket(9090);
        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
    }

URL類

  URL網(wǎng)絡(luò)編程
  1.URL:統(tǒng)一資源定位符,對應(yīng)著互聯(lián)網(wǎng)的某一資源地址
  2.格式:
    http://localhost:8080/examples/beauty.jpg?username=Tom
    協(xié)議   主機名     端口號    資源地址       參數(shù)列表
 
public class URLTest {
    public static void main(String[] args) {
        try {
            URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
//            public String getProtocol()       獲取該URL的協(xié)議名
            System.out.println(url.getProtocol());// http
//            public String getHost()           獲取該URL的主機名
            System.out.println(url.getHost());//localhost
//            public String getPort()           獲取該URL的端口號
            System.out.println(url.getPort());// 8080
//            public String getPath()           獲取該URL的文件路徑
            System.out.println(url.getPath());//examples/beauty.jpg
//            public String getFile()           獲取該URL的文件名
            System.out.println(url.getFile());//examples/beauty.jpg?username=Tom
//            public String getQuery()          獲取該URL的查詢名
            System.out.println(url.getQuery());//username=Tom
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

到此這篇關(guān)于Java小白第一次就能看懂的網(wǎng)絡(luò)編程的文章就介紹到這了,更多相關(guān)Java網(wǎng)絡(luò)編程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java并發(fā)編程之ReentrantLock解析

    Java并發(fā)編程之ReentrantLock解析

    這篇文章主要介紹了Java并發(fā)編程之ReentrantLock解析,ReentrantLock內(nèi)容定義了一個抽象類Sync,繼承自AQS,而不是自己去繼承AQS,所有對ReentrantLock的操作都會轉(zhuǎn)化為對Sync的操作,需要的朋友可以參考下
    2023-12-12
  • java設(shè)計模式之抽像工廠詳解

    java設(shè)計模式之抽像工廠詳解

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計模式之抽像工廠的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Spring實現(xiàn)IoC和DI的方法詳解

    Spring實現(xiàn)IoC和DI的方法詳解

    IoC全稱Inversion of Control (控制反轉(zhuǎn)) ,這里的控制其實是控制權(quán)的意思,可以理解為對象的獲取權(quán)力和方式發(fā)生了發(fā)轉(zhuǎn),DI依賴注?是?個過程,是指IoC容器在創(chuàng)建Bean時, 去提供運?時所依賴的資源,?資源指的就是對象,本文介紹了Spring實現(xiàn)IoC和DI的方法
    2024-08-08
  • javaweb上傳下載實例完整版解析(下)

    javaweb上傳下載實例完整版解析(下)

    這篇文章主要為大家詳細(xì)解析了javaweb上傳下載實例,本文重點在于文件下載功能的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 從Myeclipse 導(dǎo)入到eclipse中無法識別為 web項目 問題的解決步驟

    從Myeclipse 導(dǎo)入到eclipse中無法識別為 web項目 問題的解決步驟

    這篇文章主要介紹了從Myeclipse 導(dǎo)入到eclipse中無法識別為 web項目 問題的解決步驟,需要的朋友可以參考下
    2018-05-05
  • java 二分法詳解幾種實現(xiàn)方法

    java 二分法詳解幾種實現(xiàn)方法

    這篇文章主要介紹了java 二分法詳解幾種方法的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Java中的關(guān)鍵字synchronized 詳解

    Java中的關(guān)鍵字synchronized 詳解

    這篇文章主要介紹了Java中的關(guān)鍵字synchronized,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 關(guān)于Http持久連接和HttpClient連接池的深入理解

    關(guān)于Http持久連接和HttpClient連接池的深入理解

    眾所周知,httpclient是java開發(fā)中非常常見的一種訪問網(wǎng)絡(luò)資源的方式了,下面這篇文章主要給大家介紹了關(guān)于Http持久連接和HttpClient連接池的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • java高并發(fā)寫入用戶信息到數(shù)據(jù)庫的幾種方法

    java高并發(fā)寫入用戶信息到數(shù)據(jù)庫的幾種方法

    本文主要介紹了java高并發(fā)寫入用戶信息到數(shù)據(jù)庫的幾種方法,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • Java 中實現(xiàn)異步的多種方式

    Java 中實現(xiàn)異步的多種方式

    文章介紹了Java中實現(xiàn)異步處理的幾種常見方式,每種方式都有其特點和適用場景,通過選擇合適的異步處理方式,可以提高程序的性能和可維護性,感興趣的朋友一起看看吧
    2025-03-03

最新評論

西充县| 长宁区| 平度市| 富宁县| 平舆县| 赤城县| 保定市| 马关县| 万荣县| 新安县| 开原市| 隆回县| 北京市| 方城县| 新河县| 桐城市| 黄平县| 清苑县| 准格尔旗| 于田县| 博兴县| 靖边县| 土默特左旗| 凤阳县| 叙永县| 祥云县| 宿松县| 宝应县| 辰溪县| 平利县| 瑞丽市| 平果县| 长沙市| 海南省| 灵川县| 绵阳市| 阳新县| 庆云县| 吴川市| 新巴尔虎右旗| 敦煌市|