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

java UDP通信客戶端與服務(wù)器端實(shí)例分析

 更新時(shí)間:2020年01月19日 10:28:16   作者:qq_42412646  
這篇文章主要介紹了java UDP通信客戶端與服務(wù)器端,結(jié)合實(shí)例形式分析了java基于UDP通信的客戶端與服務(wù)器端具體實(shí)現(xiàn)技巧及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了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的傳輸用了一個(gè)容器類的東西,用來接收字節(jié)

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

實(shí)例:傳輸一個(gè)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ù)模砸玫紹yteArray的輸入和輸出流用來進(jìn)行數(shù)據(jù)的轉(zhuǎn)換。

另外,相較于Output流,Input流在構(gòu)建的時(shí)候需要一個(gè)數(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é)類型,直接打印(System.out.println)是以字符串類型輸出的,都需要通過Data的數(shù)據(jù)流進(jìn)行轉(zhuǎn)換。

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

奉节县| 毕节市| 红桥区| 遂川县| 秦皇岛市| 科技| 蕉岭县| 随州市| 华池县| 会理县| 象山县| 德江县| 朝阳市| 临武县| 绥中县| 台东县| 普兰店市| 会东县| 女性| 宁化县| 布拖县| 塔城市| 乐陵市| 河津市| 铜陵市| 云林县| 萝北县| 延川县| 民乐县| 滦平县| 左权县| 通辽市| 大连市| 沽源县| 二手房| 灯塔市| 左云县| 长岭县| 江华| 托克逊县| 五常市|