使用JAVA寫的一個簡易通訊程式(一)__JAVA

來源:互聯網
上載者:User

好記性不如賴筆頭…………

兩種常見的網路傳輸模型:

一、OSI模型分層

專業人員一般不說每一層的名稱二是說第幾層,這是等價的,層的位次得記清楚。
7)應用程式層——為終端使用者提供應用程式服務。
6)展示層——為應用提供資料的表示形式。例如展示層告訴應用程式層哪兒有加密或它是否為一張.jpg圖片。
5)會話層——在使用者間管理會話。
4)傳輸層——在源定義資料區段並編號,傳送資料,並在目的地重組資料。增加(源、目的)進程號。
3)網路層——為能通過在其他網路上的中間裝置進行端到端的發送建立和編址包。增加(源、目的)邏輯網路地址。
2)資料連結層——為在區域網路上主機到主機或廣域網路的裝置間的發送建立和編址幀。增加(源、目的)物理地址。
1)物理層——在裝置間傳送位元資料。物理層協議定義介質規範。定時和同步位。

二、TCP/IP模型分層

注意,不管是什麼模型,低層是物理層,頂層是內容層

4)應用程式層——為使用者表示應用資料。

3)傳輸層——支援裝置間的通訊和執行錯誤校正。

2)網際層——確定通過網路的最佳路徑。

1)網路介面層——控制網路的硬體裝置和介質。

網路傳輸需要知道:地址、連接埠、協議

----------------------------------------------------------用戶端代碼----------------------------------------------------------package com.Ckinghan.socket;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;public class UDPClient {    public static void main(String[] args) {        /**         * 啟動簡易UDP通訊的用戶端程式         */        udpClient();    }    /**     * @描述:簡易UDP通訊的用戶端程式     * @建立時間:     */    public static void udpClient(){        //建立DategramSocket對象        DatagramSocket datagramSocket = null;        //建立輸入資料流,從控制台讀取資料        BufferedReader bufferedReader = null;        //設定伺服器位址字串        String ip = "192.168.0.178";        //設定IP資訊        InetAddress inetAddress;        try {            //執行個體化inteAddress            inetAddress = InetAddress.getByName(ip);            //執行個體化datagramSocket            datagramSocket = new DatagramSocket();            //從控制台讀取資料(預設的字元編碼為GBK)            bufferedReader = new BufferedReader(new InputStreamReader(System.in,"GBK"));            //建立使用者發送的資訊            String sendString = null;            //迴圈等待使用者輸入資訊            while (true) {                //讀取使用者的輸入的資訊                sendString = bufferedReader.readLine().trim();                //將要發送的資訊轉換為位元組數組                byte[] sendBytes = sendString.getBytes("UTF-8");                //建立並執行個體化datagramPacket,指定要發送的資料、發送的資料長度、伺服器IP地址、通訊連接埠                DatagramPacket datagramPacket = new DatagramPacket(sendBytes, sendBytes.length, inetAddress, 10000);                //向伺服器發送資料                datagramSocket.send(datagramPacket);                //如果使用者輸入的是close,則退出聊天用戶端                if("close".equals(sendString))                    break;            }        } catch (UnknownHostException e) {            e.printStackTrace();        } catch (SocketException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            //關閉流            if(datagramSocket != null){                datagramSocket.close();            }            //關閉流            if(bufferedReader != null){                try {                    bufferedReader.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}----------------------------------------------------------服務端代碼----------------------------------------------------------package com.Ckinghan.socket;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;public class UDPServer {    public static void main(String[] args) {        /**         * 啟動簡易UDP通訊的服務端程式         */        socketServer();    }    /**     * @描述:簡易UDP通訊的服務端程式     * @建立時間:     */    public static void socketServer(){        DatagramSocket datagramSocket = null;        try {            //執行個體化DategramSocket            datagramSocket = new DatagramSocket(10000);            //接收資料的byte[]資料            byte[] getDate = new byte[1024];            //用以接收使用者儲存的資料            DatagramPacket datagramPacket = new DatagramPacket(getDate, getDate.length);            //迴圈監聽用戶端發送的資訊            while (true) {                //將使用者發送的資料,放到dategramPacket中                datagramSocket.receive(datagramPacket);                //擷取接收到的資料位元組長度                int len = datagramPacket.getLength();                //擷取接收到的資料                byte[] date = datagramPacket.getData();                //擷取請求來源                InetAddress inetAddress = datagramPacket.getAddress();                //將接收到的位元組轉換為字元(將使用者發送的資訊還原)                String receive = new String(date, 0, len,"UTF-8");                //如果輸入字元為close,則提示使用者已退出迴圈                if("close".equals(receive)){                    System.out.println(inetAddress.getHostAddress()+"的使用者已退出會話。");//                  break;                }                //輸出使用者發送的資訊                System.out.println(inetAddress.getHostAddress()+"  :"+receive);            }        } catch (SocketException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            //如果dategramSocket被執行個體化了,關閉流            if(datagramSocket != null){                datagramSocket.close();            }        }    }}

執行效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.