• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    數據庫中java的DatagramPacket

    發布: 2010-11-11 09:37 | 作者: 網絡轉載 | 來源: 領測軟件測試網采編 | 查看: 137次 | 進入軟件測試論壇討論

    領測軟件測試網

    注意:IP地址224.0.0.1 到 239.255.255.255(包括)均為保留的多點傳送組地址。

      網絡API通過MulticastSocket類和MulticastSocket,以及一些輔助類(比如NetworkInterface) 支持多點傳送,當一個客戶程序要加入多點傳送組時,就創建一個MulticastSocket對象。MulticastSocket(int port) 構造函數允許應用程序指定端口(通過port參數)接收自尋址包,端口必須與服務程序的端口號相匹配,要加入多點傳送組,客戶程序調用兩個 joinGroup()方法中的一個,同樣要離開傳送組,也要調用兩個leaveGroup()方法中的一個。

      由于MulticastSocket擴展了DatagramSocket類,一個MulticastSocket對象就有權訪問DatagramSocket方法。

      List6是MCClient的源代碼,這段代碼示范了一個客戶端加入多點傳送組的例子。


    Listing 6: MCClient.java
    // MCClient.java

    import java.io.*;
    import java.net.*;

    class MCClient
    {
    public static void main (String [] args) throws IOException
    {
    // Create a MulticastSocket bound to local port 10000. All
    // multicast packets from the server program are received
    // on that port.

      MulticastSocket s = new MulticastSocket (10000);

      // Obtain an InetAddress object that contains the multicast
    // group address 231.0.0.1. The InetAddress object is used by
    // DatagramPacket.

      InetAddress group = InetAddress.getByName ("231.0.0.1");

      // Join the multicast group so that datagram packets can be
    // received.

      s.joinGroup (group);

      // Read several datagram packets from the server program.

      for (int i = 0; i < 10; i++)
    {
    // No line will exceed 256 bytes.

       byte [] buffer = new byte [256];

       // The DatagramPacket object needs no addressing
    // information because the socket contains the address.

       DatagramPacket dgp = new DatagramPacket (buffer,
    buffer.length);

       // Receive a datagram packet.

       s.receive (dgp);

       // Create a second byte array with a length that matches
    // the length of the sent data.

       byte [] buffer2 = new byte [dgp.getLength ()];

       // Copy the sent data to the second byte array.

       System.arraycopy (dgp.getData (),
    0,
    buffer2,
    0,
    dgp.getLength ());

       // Print the contents of the second byte array. (Try
    // printing the contents of buffer. You will soon see why
    // buffer2 is used.)

       System.out.println (new String (buffer2));
    }

      // Leave the multicast group.

      s.leaveGroup (group);

      // Close the socket.

      s.close ();
    }
    }


    MCClient創建了一個綁定端口號10000的MulticastSocket對象,接下來他獲得了一個InetAddress子類對象,該子類 對象包含多點傳送組的IP地址231.0.0.0,然后通過joinGroup(InetAddress addr)方法加入多點傳送組中,接下來 MCClient接收10個自尋址包,同時輸出他們的內容,然后使用leaveGroup(InetAddress addr)方法離開傳送組,最后關閉 套接字。

      也許你對使用兩個字節數組buffer 和 buffer2感到奇怪,當接收到一個自尋址包后,getData()方法返回一個引用,自尋址包 的長度是256個字節,如果要輸出所有數據,在輸出完實際數據后會有很多空格,這顯然是不合理的,所以我們必須去掉這些空格,因此我們創建一個小的字節數 組buffer2,buffer2的實際長度就是數據的實際長度,通過調用DatagramPacket's getLength()方法來得到這個長 度。從buffer 到 buffer2快速復制getLength()的長度的方法是調用System.arraycopy()方法。

      List7 MCServer的源代碼顯示了服務程序是怎樣工作的。


    Listing 7: MCServer.java
    // MCServer.java

    import java.io.*;
    import java.net.*;

    class MCServer
    {
    public static void main (String[] args) throws IOException
    {
    System.out.println ("Server starting...\n");

      // Create a MulticastSocket not bound to any port.

      MulticastSocket s = new MulticastSocket ();

      // Because MulticastSocket subclasses DatagramSocket, it is
    // legal to replace MulticastSocket s = new MulticastSocket ();
    // with the following line.

        // DatagramSocket s = new DatagramSocket ();

      // Obtain an InetAddress object that contains the multicast
    // group address 231.0.0.1. The InetAddress object is used by
    // DatagramPacket.

      InetAddress group = InetAddress.getByName ("231.0.0.1");

      // Create a DatagramPacket object that encapsulates a reference
    // to a byte array (later) and destination address
    // information. The destination address consists of the
    // multicast group address (as stored in the InetAddress object)
    // and port number 10000 -- the port to which multicast datagram
    // packets are sent. (Note: The dummy array is used to prevent a
    // NullPointerException object being thrown from the
    // DatagramPacket constructor.)

      byte [] dummy = new byte [0];

      DatagramPacket dgp = new DatagramPacket (dummy,
    0,
    group,
    10000);

      // Send 30000 Strings to the port.

      for (int i = 0; i < 30000; i++)
    {
    // Create an array of bytes from a String. The platform's
    // default character set is used to convert from Unicode
    // characters to bytes.

       byte [] buffer = ("Video line " + i).getBytes ();

       // Establish the byte array as the datagram packet's
    // buffer.

       dgp.setData (buffer);

       // Establish the byte array's length as the length of the
    // datagram packet's buffer.

       dgp.setLength (buffer.length);

       // Send the datagram to all members of the multicast group
    // that listen on port 10000.

       s.send (dgp);
    }

      // Close the socket.

      s.close ();
    }
    }


    MCServer創建了一個MulticastSocket對象,由于他是DatagramPacket對象的一部分,所以他沒有綁定端口 號,DatagramPacket有多點傳送組的IP地址(231.0.0.0),一旦創建DatagramPacket對象,MCServer就進入一 個發送30000條的文本的循環中,對文本的每一行均要創建一個字節數組,他們的引用均存儲在前面創建的DatagramPacket對象中,通過 send()方法,自尋址包發送給所有的組成員。

      在編譯了MCServer 和 MCClient后,通過輸入java MCServer開始運行MCServer,最后再運行一個或多個MCClient。

      結論

    本文通過研究套接字揭示了Java的網絡API的應用方法,我們介紹了套接自的慨念和套接字的組成,以及流套接字和自尋址套接字,以及如何使用 InetAddress, Socket, ServerSocket, DatagramPacket, DatagramSocket和 MulticastSocket類。在完成本文后就可以編寫基本的底層通訊程序。

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/

    22/2<12

    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>