溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在JAVA項(xiàng)目中實(shí)現(xiàn)一個(gè)UDP網(wǎng)絡(luò)通訊功能

發(fā)布時(shí)間:2020-11-30 16:51:20 來(lái)源:億速云 閱讀:173 作者:Leah 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)怎么在JAVA項(xiàng)目中實(shí)現(xiàn)一個(gè)UDP網(wǎng)絡(luò)通訊功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

具體如下:

UDP協(xié)議全稱是用戶數(shù)據(jù)報(bào)協(xié)議,在網(wǎng)絡(luò)中它與TCP協(xié)議一樣用于處理數(shù)據(jù)包,是一種無(wú)連接的協(xié)議。

在OSI模型中,在第四層——傳輸層,處于IP協(xié)議的上一層。UDP有不提供數(shù)據(jù)包分組、組裝和不能對(duì)數(shù)據(jù)包進(jìn)行排序的缺點(diǎn):

也就是說(shuō),當(dāng)報(bào)文發(fā)送之后,是無(wú)法得知其是否安全完整到達(dá)的。UDP用來(lái)支持那些需要在計(jì)算機(jī)之間傳輸數(shù)據(jù)的網(wǎng)絡(luò)應(yīng)用。

采用UDP協(xié)議要先把數(shù)據(jù)定義成數(shù)據(jù)報(bào)(Datagram)并在數(shù)據(jù)報(bào)中指明數(shù)據(jù)所要達(dá)到的Socket,再進(jìn)行數(shù)據(jù)傳遞。

主要涉及的兩個(gè)類:

DatagramPacket:數(shù)據(jù)報(bào)包類
DatagramSocket:數(shù)據(jù)端對(duì)端通訊類

簡(jiǎn)單demo之UDP服務(wù)端

public class UdpServer {
  public static void main(String[] args) {
    // 實(shí)現(xiàn)步驟1:創(chuàng)建DatagramSokcet
    try {
      DatagramSocket mSocket = new DatagramSocket(9999);
      // 實(shí)現(xiàn)步驟2:創(chuàng)建DatagramPacket
      byte[] data = new byte[1024];
      DatagramPacket mPacket = new DatagramPacket(data, data.length);
      // 實(shí)現(xiàn)步驟3:接收 數(shù)據(jù)
      mSocket.receive(mPacket);
      // 實(shí)現(xiàn)步驟4:處理數(shù)據(jù)
      String result = new String(data, 0, mPacket.getLength());
      System.out.println(result);
      /**** 回復(fù)客戶端 ****************/
      byte[] response = "我是UDP服務(wù)端,已經(jīng)回到你的請(qǐng)求".getBytes();
      mPacket.setData(response);
      mSocket.send(mPacket);
      mSocket.close();
    } catch (SocketException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

簡(jiǎn)單demo之UDP客戶端

public class UdpClient {
  private static String sendData = "我是UDP客戶端,請(qǐng)求連接服務(wù)端";
  public static void main(String[] args) {
    try {
      // 步驟1:指定服務(wù)器的信息
      InetAddress mAddress = InetAddress.getByName("localhost");
      int port = 9999;
      byte[] data = sendData.getBytes();
      // 步驟2:創(chuàng)建DatagramPacket
      DatagramPacket mPacket = new DatagramPacket(data, data.length,
          mAddress, port);
      // 步驟3:創(chuàng)建DatagramSocket
      DatagramSocket mSocket = new DatagramSocket();
      // 步驟4:向服務(wù)端發(fā)送數(shù)據(jù)
      mSocket.send(mPacket);
      /***** 下面接收服務(wù)器返回?cái)?shù)據(jù) ***************************/
      // 實(shí)現(xiàn)步驟3:接收 數(shù)據(jù)
      mSocket.receive(mPacket);
      // 實(shí)現(xiàn)步驟4:處理數(shù)據(jù)
      String response = new String(data, 0, mPacket.getLength());
      System.out.println(response);
      // 關(guān)閉資源
      mSocket.close();
    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SocketException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

看完上述內(nèi)容,你們對(duì)怎么在JAVA項(xiàng)目中實(shí)現(xiàn)一個(gè)UDP網(wǎng)絡(luò)通訊功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI