溫馨提示×

溫馨提示×

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

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

java中UDP通信客戶端與服務(wù)器端的示例分析

發(fā)布時間:2021-08-23 10:35:26 來源:億速云 閱讀:122 作者:小新 欄目:編程語言

小編給大家分享一下java中UDP通信客戶端與服務(wù)器端的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體如下:

最初Udp是以字節(jié)為單位進(jìn)行傳輸?shù)?,所以有很大的限?/p>

服務(wù)器端:

import java.net.*;
public class TestUdpServer {
    public static void main(String[] args) throws Exception {
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf,buf.length);
//        try {
            DatagramSocket ds = new DatagramSocket(2345);
            while(true) {
                ds.receive(dp);
                System.out.println(new String(buf,0,dp.getLength()));    
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
        }
    }
}

用戶端:

import java.net.*;
public class TestUdpClient {
    public static void main(String[] args) throws Exception {
        byte[] buf = new byte[1024];
        buf = (new String("hello")).getBytes();
        DatagramPacket dp = new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1",2345));
//        try {
            DatagramSocket ds = new DatagramSocket(5679);
            ds.send(dp);
            ds.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}

注:由于必須以字節(jié)為單位進(jìn)行傳輸,Udp的傳輸用了一個容器類的東西,用來接收字節(jié)

先建一個字節(jié)數(shù)組,然后以這個數(shù)組創(chuàng)建容器。用來傳輸數(shù)據(jù)。

實例:傳輸一個Long類型的數(shù)據(jù)

服務(wù)器端:

import java.io.*;
import java.net.*;
public class UdpServer {
    public static void main(String[] args) throws Exception {
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf,buf.length);
        DatagramSocket ds = new DatagramSocket(2345);
        while(true) {
            ByteArrayInputStream is = new ByteArrayInputStream(buf);
            DataInputStream dis = new DataInputStream(is);
            ds.receive(dp);
            System.out.println(dis.readLong());    
        }
    }
}

用戶端:

import java.io.*;
import java.net.*;
public class UdpClient {
    public static void main(String[] args) throws Exception {
        Long n = 10000L;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeLong(n);
        byte[] buf = new byte[1024];
        buf = os.toByteArray();
        System.out.println(buf.length);
        DatagramPacket dp = new DatagramPacket(buf,buf.length,
                new InetSocketAddress("127.0.0.1",2345));
        DatagramSocket ds = new DatagramSocket(5679);
        ds.send(dp);
        ds.close();
    }
}

注:由于Udp是以字節(jié)為單位進(jìn)行傳輸?shù)?,所以要用到ByteArray的輸入和輸出流用來進(jìn)行數(shù)據(jù)的轉(zhuǎn)換。

另外,相較于Output流,Input流在構(gòu)建的時候需要一個數(shù)組作為參數(shù),用來存放數(shù)據(jù)。

在基本的Udp傳輸?shù)幕A(chǔ)上,代碼分為兩部分,一部分是把傳輸或接受的Long類型數(shù)據(jù)轉(zhuǎn)換為byte類型的數(shù)據(jù),然后是基本的數(shù)據(jù)傳輸。

另一方面,直接的字節(jié)流不能轉(zhuǎn)換為Long類型,同理,剛接收的數(shù)據(jù)是字節(jié)類型,直接打?。⊿ystem.out.println)是以字符串類型輸出的,都需要通過Data的數(shù)據(jù)流進(jìn)行轉(zhuǎn)換。

以上是“java中UDP通信客戶端與服務(wù)器端的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

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

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

AI