Java實(shí)現(xiàn)Udp網(wǎng)絡(luò)編程

在看到本文之前,如果讀者沒看過筆者的上一個(gè)系列 Java實(shí)現(xiàn)Socket網(wǎng)絡(luò)編程,建議先翻閱。

筆者將在上期Demo的基礎(chǔ)上,進(jìn)一步修改和擴(kuò)展,達(dá)到本次Demo的運(yùn)行效果。

首先展示Demo的演示效果:
初始狀態(tài):1個(gè)服務(wù)器,2個(gè)客戶端

Paste_Image.png

檢測(cè)通信正常:

Paste_Image.png

斷開服務(wù)器,再次檢測(cè)通信正常:

Paste_Image.png

服務(wù)器重新啟動(dòng),自動(dòng)刷新:

Paste_Image.png

添加客戶端:

Paste_Image.png

關(guān)于 C(客戶端)和 S(服務(wù)器)之間的TCP通信,以及 C 檢測(cè) S 狀態(tài),自動(dòng)重連等機(jī)制,筆者在上期Demo的實(shí)現(xiàn)過程中已詳細(xì)闡述,此處就不再贅述。

我們來看看本次案例的實(shí)現(xiàn)需求:
1、服務(wù)器支持多客戶端訪問
2、C和S之間使用TCP連接
3、C和C之間使用UDP直接通信

由于案例需求的步驟1、2已實(shí)現(xiàn),我們對(duì)步驟3作如下設(shè)計(jì)思路:
1、客戶端創(chuàng)建監(jiān)聽線程,建立UDP監(jiān)聽端口,并發(fā)消息告訴服務(wù)器,指定自己的服務(wù)端口。
2、服務(wù)器得知客戶端的服務(wù)端口后,廣播通知其他客戶端,現(xiàn)已登錄的客戶端服務(wù)端口列表。
3、客戶端之間直接通過UDP,向指定服務(wù)端口發(fā)送消息。

值得注意的是,C與C之間要求直接通信,所以必須滿足“在服務(wù)器關(guān)閉的情況下,C與C之間仍能通信”的情況,而不是借助服務(wù)器完成間接通信

首先,我們創(chuàng)建客戶端監(jiān)聽線程,并發(fā)消息告訴服務(wù)器

public void run() {

        try {

            DatagramSocket server = new DatagramSocket(0);// 隨機(jī)分配一個(gè)端口號(hào)

            // 向服務(wù)器發(fā)送接收客戶端的DatagramSocket的端口號(hào)

            String message = Common.SPECIAL;

            String t = "" + server.getLocalPort();

            ClientMain.frame.setTitle("client " + t);
            String c = "" + t.length();
            if (c.length() < 2) {
                c = "000" + c;
            } else if (c.length() < 3) {
                c = "00" + c;
            } else if (c.length() < 4) {
                c = "0" + c;
            }
            message += c + t;

            OutputStreamWriter outstream = null;

            // 將信息發(fā)送給服務(wù)器
            try {
                outstream = new OutputStreamWriter(mSocket.getOutputStream(),
                        "GBK");
                outstream.write(message);
                outstream.flush();

            } catch (IOException e1) {
                ClientMain.jlConnect.setText("Out Of Connect.");
                ClientMain.isConnected = false;
                if (outstream != null)
                    try {
                        outstream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                e1.printStackTrace();
            }

            while (true) {

                byte[] recvBuf = new byte[1024];// 定義接收消息的緩沖區(qū)
                DatagramPacket recvPacket = new DatagramPacket(recvBuf,
                        recvBuf.length);// 數(shù)據(jù)包
                server.receive(recvPacket);

                // 接收到的消息
                String recvStr = new String(recvPacket.getData(), 0,
                        recvPacket.getLength());

                ClientMain.jtaReceivedMessage.append(recvStr + "\n");
                // 滾動(dòng)到底端
                ClientMain.jtaReceivedMessage
                        .setCaretPosition(ClientMain.jtaReceivedMessage
                                .getText().length());
            }

        } catch (Exception e) {

            e.printStackTrace();
        }
}

服務(wù)器得知客戶端的服務(wù)端口后,廣播通知其他客戶端

else if (s.startsWith(Common.SPECIAL) && s.length() > 10
                            && count == Integer.parseInt((s.substring(6, 10)))) {
                        // 存儲(chǔ)客戶端監(jiān)聽端口
                        /**
                         * 一定要注意使用前初始化,否則在IDE在這里檢測(cè)不到空指針錯(cuò)誤
                         */
                        HashMap<Socket, String> map = new HashMap<Socket, String>();
                        map.put(mSocket, s.substring(10));
                        ServerMain.clientMonitorPortList.add(map);
                        // 發(fā)送更新列表信息給客戶端
                        sendUpdateToClient();
                        count = -10;
                        s = "";
                    }

sendUpdateToClient方法如下:

// 發(fā)送更新列表信息給所有客戶端
    private void sendUpdateToClient() {
        String message = Common.SEND_TO_CLIENT;
        String t = "";

        for (int i = 0; i < ServerMain.clientMonitorPortList.size(); i++) {
            HashMap<Socket, String> map = ServerMain.clientMonitorPortList
                    .get(i);
            Iterator iter1 = map.entrySet().iterator();
            Map.Entry entry = (Map.Entry) iter1.next();
            Socket key = (Socket) entry.getKey();
            int localPort = key.getPort();
            String port = (String) entry.getValue();
            if (i != ServerMain.clientMonitorPortList.size() - 1)
                t += localPort + " " + port + " ";
            else
                t += localPort + " " + port;
        }

        String c = "" + t.length();
        if (c.length() < 2) {
            c = "000" + c;
        } else if (c.length() < 3) {
            c = "00" + c;
        } else if (c.length() < 4) {
            c = "0" + c;
        }
        message += c + t;

        OutputStreamWriter outstream = null;

        // 將信息發(fā)送給每個(gè)客戶端
        for (int i = 0; i < ListenThread.clientSockets.size(); i++) {
            try {
                HashMap<Socket, Boolean> map = ListenThread.clientSockets
                        .get(i);
                // 用迭代器獲取HashMap的Key,即所選中的Socket
                Iterator iter = map.entrySet().iterator();
                Map.Entry<Socket, Boolean> entry = (Entry<Socket, Boolean>) iter
                        .next();

                Socket key = (Socket) entry.getKey();
                outstream = new OutputStreamWriter(key.getOutputStream(), "GBK");
                outstream.write(message);
                outstream.flush();

            } catch (IOException e1) {
                if (outstream != null)
                    try {
                        outstream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                e1.printStackTrace();
            }
        }
    }

最后,客戶端通過UDP向指定服務(wù)端口發(fā)送消息
當(dāng)選中JList的項(xiàng)時(shí),向選中的項(xiàng)發(fā)送消息,如果沒有選中項(xiàng),則向服務(wù)器發(fā)送消息

// 設(shè)置監(jiān)聽
        jbSendMessage.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if (jtaSendMessage.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "發(fā)送內(nèi)容不能為空!");
                    return;
                }

                // 取得要發(fā)送的消息
                String message = Common.SIMPLE;

                String t = "client " + Common.IP + ":" + mSocket.getLocalPort()
                        + " " + jtaSendMessage.getText();
                String c = "" + t.length();
                if (c.length() < 2) {
                    c = "000" + c;
                } else if (c.length() < 3) {
                    c = "00" + c;
                } else if (c.length() < 4) {
                    c = "0" + c;
                }
                message += c + t;

                OutputStreamWriter outstream = null;

                // 如果沒有選中,則向服務(wù)器發(fā)送消息
                if (selecteds == null || selecteds.length == 0) {
                    try {
                        outstream = new OutputStreamWriter(mSocket
                                .getOutputStream(), "GBK");
                        outstream.write(message);
                        outstream.flush();
                    } catch (IOException e1) {
                        if (outstream != null)
                            try {
                                outstream.close();
                            } catch (IOException e2) {
                                e2.printStackTrace();
                            }
                        e1.printStackTrace();
                    }
                } else {
                    String sendPort = "";

                    // 檢測(cè)現(xiàn)在進(jìn)行發(fā)送行為的是哪個(gè)客戶端
                    for (int i = 0; i < clientPortList.size(); i++) {
                        HashMap<String, String> map = (HashMap<String, String>) clientPortList
                                .get(i);
                        Iterator iter1 = map.entrySet().iterator();
                        Map.Entry entry = (Map.Entry) iter1.next();
                        String sendSocketPort = (String) entry.getKey();
                        // mSocket.getLocalPort()是int類型,要注意加""
                        if (sendSocketPort.equals(mSocket.getLocalPort() + "")) {
                            sendPort = (String) entry.getValue();
                        }
                    }

                    // 向選中的客戶端發(fā)送消息
                    for (int i = 0; i < selecteds.length; i++) {
                        // 獲取選中的端口
                        HashMap<String, String> map = (HashMap<String, String>) clientPortList
                                .get(selecteds[i]);
                        Iterator iter1 = map.entrySet().iterator();
                        Map.Entry entry = (Map.Entry) iter1.next();
                        String port = (String) entry.getValue();

                        try {
                            // 生成一個(gè)臨時(shí)發(fā)送端口
                            DatagramSocket client = new DatagramSocket(0);
                            // 要發(fā)送的數(shù)據(jù)
                            String sendMessage = "client " + Common.IP + ":"
                                    + sendPort + " " + jtaSendMessage.getText();
                            byte[] buf = sendMessage.getBytes();
                            // 定義發(fā)送信息的目的地
                            InetAddress destination = InetAddress
                                    .getByName(Common.IP);
                            // 生成數(shù)據(jù)包
                            DatagramPacket dp = new DatagramPacket(buf,
                                    buf.length, destination, Integer
                                            .valueOf(port));
                            client.send(dp);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }

                // 清空文本
                jtaSendMessage.setText(null);

            }

        });

本次實(shí)驗(yàn)步驟看似簡(jiǎn)單,但也有幾個(gè)不得不注意的地方:
1、在讀寫數(shù)據(jù)的循環(huán)里,是檢測(cè)不到空指針錯(cuò)誤的,只會(huì)檢測(cè)到讀寫錯(cuò)誤后不斷嘗試重連。讀者在開發(fā)過程中一定要注意把相應(yīng)的控件初始化,而發(fā)現(xiàn)不斷重連,重復(fù)讀寫時(shí),應(yīng)首先考慮是否在讀寫循環(huán)里引用了未初始化的控件。
2、mSocket.getLocalPort()方法返回的是int類型,使用equals比較時(shí)要注意加雙引號(hào)"",以轉(zhuǎn)換成String類型,否則IDE不會(huì)編譯報(bào)錯(cuò),但結(jié)果并未如意。
3、使用UDP端口容易混亂:讀者在開發(fā)過程中應(yīng)盡量避免更新UI時(shí)整體刪除再添加剩余項(xiàng),而改用“只刪除關(guān)閉項(xiàng),只增加新增項(xiàng)”,前種方法在開發(fā)過程中容易造成端口混亂。同時(shí),筆者建議讀者在涉及JList操作時(shí),多用ArrayList替代HashMap存儲(chǔ),因?yàn)锳rrayList是插入有序的,能減少混亂的發(fā)生。
4、注意在視圖model中刪除了項(xiàng),也要同時(shí)在列表List中刪除對(duì)應(yīng)項(xiàng),以做到真正的刪除,而不是假刪除。
5、刪除List中的所有項(xiàng):
for(int i=0;i<list.size();)list.remove(i);
注意!這里不能添加i++,因?yàn)槊看蝦emove后,list.size()會(huì)自動(dòng)減小,如果添加了i++,則不能完全刪除List中的元素,從而導(dǎo)致二次混亂

最后,筆者在github上給出了兩次實(shí)驗(yàn)的Demo源碼,供讀者學(xué)習(xí)和思考,感謝關(guān)注!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評(píng)論 19 139
  • 計(jì)算機(jī)網(wǎng)絡(luò)概述 網(wǎng)絡(luò)編程的實(shí)質(zhì)就是兩個(gè)(或多個(gè))設(shè)備(例如計(jì)算機(jī))之間的數(shù)據(jù)傳輸。 按照計(jì)算機(jī)網(wǎng)絡(luò)的定義,通過一定...
    蛋炒飯_By閱讀 1,367評(píng)論 0 10
  • 網(wǎng)絡(luò)編程 網(wǎng)絡(luò)編程對(duì)于很多的初學(xué)者來說,都是很向往的一種編程技能,但是很多的初學(xué)者卻因?yàn)楹荛L(zhǎng)一段時(shí)間無法進(jìn)入網(wǎng)絡(luò)編...
    程序員歐陽閱讀 2,105評(píng)論 1 37
  • 前幾天紫雨老師和錦明老師今天同時(shí)來群里給我們大家做輔導(dǎo),雖然被紫雨老師批評(píng)我也沒有怨言,我知道紫雨老師是被我...
    苦咖啡_0a98閱讀 449評(píng)論 5 4
  • 二零一五年八月的最后一天,我和他相遇在高二十六班的教室里。那時(shí)的我和他雖然在同一個(gè)學(xué)校待了一年,但我和他還...
    瓊晴閱讀 229評(píng)論 0 0

友情鏈接更多精彩內(nèi)容