<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 56,  comments - 12,  trackbacks - 0

    Java套接字編程(上)
    用Java開發網絡軟件非常方便和強大,Java的這種力量來源于他獨有的一套強大的用于網絡的 API,這些API是一系列的類和接口,均位于包java.net和javax.net中。在這篇文章中我們將介紹套接字(Socket)慨念,同時以實 例說明如何使用Network API操縱套接字,在完成本文后,你就可以編寫網絡低端通訊軟件。

      什么是套接字(Socket)?

    Network API是典型的用于基于TCP/IP網絡Java程序與其他程序通訊,Network API依靠Socket進行通訊。Socket可以看成在兩個程序進行通訊連接中的一個端點,一個程序將一段信息寫入Socket中,該Socket將這 段信息發送給另外一個Socket中,使這段信息能傳送到其他程序中。

      我們來分析一下,Host A上的程序A將一段信息寫入Socket中,Socket的內容被Host A的網絡管理軟件訪問,并將這段信息通過Host A的網絡接口卡發送到Host B,Host B的網絡接口卡接收到這段信息后,傳送給Host B的網絡管理軟件,網絡管理軟件將這段信息保存在Host B的Socket中,然后程序B才能在Socket中閱讀這段信息。

      假設在 圖1的網絡中添加第三個主機Host C,那么Host A怎么知道信息被正確傳送到Host B而不是被傳送到Host C中了呢?基于TCP/IP網絡中的每一個主機均被賦予了一個唯一的IP地址,IP地址是一個32位的無符號整數,由于沒有轉變成二進制,因此通常以小數 點分隔,如:198.163.227.6,正如所見IP地址均由四個部分組成,每個部分的范圍都是0-255,以表示8位地址。

      值得注意的是IP地址都是32位地址,這是IP協議版本4(簡稱Ipv4)規定的,目前由于IPv4地址已近耗盡,所以IPv6地址正逐漸代替Ipv4地址,Ipv6地址則是128位無符號整數。

    假設第二個程序被加入圖1的網絡的Host B中,那么由Host A傳來的信息如何能被正確的傳給程序B而不是傳給新加入的程序呢?這是因為每一個基于TCP/IP網絡通訊的程序都被賦予了唯一的端口和端口號,端口是一 個信息緩沖區,用于保留Socket中的輸入/輸出信息,端口號是一個16位無符號整數,范圍是0-65535,以區別主機上的每一個程序(端口號就像房 屋中的房間號),低于256的短口號保留給標準應用程序,比如pop3的端口號就是110,每一個套接字都組合進了IP地址、端口、端口號,這樣形成的整 體就可以區別每一個套接字t,下面我們就來談談兩種套接字:流套接字和自尋址數據套接字。

      流套接字(Stream Socket)

    無論何時,在兩個網絡應用程序之間發送和接收信息時都需要建立一個可靠的連接,流套接字依靠TCP協議來保證信息正確到達目的地,實際上,IP包有可能 在網絡中丟失或者在傳送過程中發生錯誤,任何一種情況發生,作為接受方的 TCP將聯系發送方TCP重新發送這個IP包。這就是所謂的在兩個流套接字之間建立可靠的連接。

      流套接字在C/S程序中扮演一個必需的角色,客戶機程序(需要訪問某些服務的網絡應用程序)創建一個扮演服務器程序的主機的IP地址和服務器程序(為客戶端應用程序提供服務的網絡應用程序)的端口號的流套接字對象。

    客戶端流套接字的初始化代碼將IP地址和端口號傳遞給客戶端主機的網絡管理軟件,管理軟件將IP地址和端口號通過NIC傳遞給服務器端主機;服務器端主 機讀到經過NIC傳遞來的數據,然后查看服務器程序是否處于監聽狀態,這種監聽依然是通過套接字和端口來進行的;如果服務器程序處于監聽狀態,那么服務器 端網絡管理軟件就向客戶機網絡管理軟件發出一個積極的響應信號,接收到響應信號后,客戶端流套接字初始化代碼就給客戶程序建立一個端口號,并將這個端口號 傳遞給服務器程序的套接字(服務器程序將使用這個端口號識別傳來的信息是否是屬于客戶程序)同時完成流套接字的初始化。

      如果服務器程序 沒有處于監聽狀態,那么服務器端網絡管理軟件將給客戶端傳遞一個消極信號,收到這個消極信號后,客戶程序的流套接字初始化代碼將拋出一個異常對象并且不建 立通訊連接,也不創建流套接字對象。這種情形就像打電話一樣,當有人的時候通訊建立,否則電話將被掛起。

      這部分的工作包括了相關聯的三 個類:InetAddress, Socket, 和 ServerSocket。 InetAddress對象描繪了32位或128位IP地址,Socket對象代表了客戶程序流套接字,ServerSocket代表了服務程序流套接 字,所有這三個類均位于包java.net中。

      InetAddress類

      InetAddress類在網絡API套接 字編程中扮演了一個重要角色。參數傳遞給流套接字類和自尋址套接字類構造器或非構造器方法。InetAddress描述了32位或64位IP地址,要完成 這個功能,InetAddress類主要依靠兩個支持類Inet4Address 和 Inet6Address,這三個類是繼承關系,InetAddrress是父類,Inet4Address 和 Inet6Address是子類。

      由于InetAddress類只有一個構造函數,而且不能傳遞參數,所以不能直接創建InetAddress對象,比如下面的做法就是錯誤的:
    InetAddress ia = new InetAddress ();
      但我們可以通過下面的5個工廠方法創建來創建一個InetAddress對象或InetAddress數組:

    . getAllByName(String host)方法返回一個InetAddress對象的引用,每個對象包含一個表示相應主機名的單獨的IP地址,這個IP地址是通過host參數傳遞的,對 于指定的主機如果沒有IP地址存在那么這個方法將拋出一個UnknownHostException 異常對象。

       . getByAddress(byte [] addr)方法返回一個InetAddress對象的引用,這個對象包含了一個Ipv4地址或Ipv6地址,Ipv4地址是一個4字節數組,Ipv6地址 是一個16字節地址數組,如果返回的數組既不是4字節的也不是16字節的,那么方法將會拋出一個UnknownHostException異常對象。

    . getByAddress(String host, byte [] addr)方法返回一個InetAddress對象的引用,這個InetAddress對象包含了一個由host和4字節的addr數組指定的IP地址, 或者是host和16字節的addr數組指定的IP地址,如果這個數組既不是4字節的也不是16位字節的,那么該方法將拋出一個 UnknownHostException異常對象。

       . getByName(String host)方法返回一個InetAddress對象,該對象包含了一個與host參數指定的主機相對應的IP地址,對于指定的主機如果沒有IP地址存在,那么方法將拋出一個UnknownHostException異常對象。

       . getLocalHost()方法返回一個InetAddress對象,這個對象包含了本地機的IP地址,考慮到本地主機既是客戶程序主機又是服務器程序主機,為避免混亂,我們將客戶程序主機稱為客戶主機,將服務器程序主機稱為服務器主機。

    上面講到的方法均提到返回一個或多個InetAddress對象的引用,實際上每一個方法都要返回一個或多個 Inet4Address/Inet6Address對象的引用,調用者不需要知道引用的子類型,相反調用者可以使用返回的引用調用 InetAddress對象的非靜態方法,包括子類型的多態以確保重載方法被調用。

      InetAddress和它的子類型對象處理主機名 到主機IPv4或IPv6地址的轉換,要完成這個轉換需要使用域名系統,下面的代碼示范了如何通過調用getByName(String host)方法獲得InetAddress子類對象的方法,這個對象包含了與host參數相對應的IP地址:
    InetAddress ia = InetAddress.getByName (www.javajeff.com));

    一但獲得了InetAddress子類對象的引用就可以調用InetAddress的各種方法來獲得InetAddress子類對象中的IP地址信息, 比如,可以通過調用getCanonicalHostName()從域名服務中獲得標準的主機名;getHostAddress()獲得IP地址, getHostName()獲得主機名,isLoopbackAddress()判斷IP地址是否是一個loopback地址。

      List1 是一段示范代碼:InetAddressDemo
    // InetAddressDemo.java

    import java.net.*;

    class InetAddressDemo
    {
     public static void main (String [] args) throws UnknownHostException
     {
      String host = "localhost";

      if (args.length == 1)
       host = args [0];

      InetAddress ia = InetAddress.getByName (host);

      System.out.println ("Canonical Host Name = " +
            ia.getCanonicalHostName ());
      System.out.println ("Host Address = " +
            ia.getHostAddress ());
      System.out.println ("Host Name = " +
            ia.getHostName ());
      System.out.println ("Is Loopback Address = " +
            ia.isLoopbackAddress ());
     }
    }

      當無命令行參數時,代碼輸出類似下面的結果:
    Canonical Host Name = localhost
    Host Address = 127.0.0.1
    Host Name = localhost
    Is Loopback Address = true

    InetAddressDemo給了你一個指定主機名作為命令行參數的選擇,如果沒有主機名被指定,那么將使用localhost(客戶機的), InetAddressDemo通過調用getByName(String host)方法獲得一個InetAddress子類對象的引用,通過這個引用獲得了標準主機名,主機地址,主機名以及IP地址是否是loopback地址 的輸出。

     


    Socket類

      當客戶程序需要與服務器程序通訊的時候,客戶程序在客戶機創建一個 socket對象,Socket類有幾個構造函數。兩個常用的構造函數是 Socket(InetAddress addr, int port) 和 Socket(String host, int port),兩個構造函數都創建了一個基于Socket的連接服務器端流套接字的流套接字。對于第一個InetAddress子類對象通過addr參數獲 得服務器主機的IP地址,對于第二個函數host參數包被分配到InetAddress對象中,如果沒有IP地址與host參數相一致,那么將拋出 UnknownHostException異常對象。兩個函數都通過參數port獲得服務器的端口號。假設已經建立連接了,網絡API將在客戶端基于 Socket的流套接字中捆綁客戶程序的IP地址和任意一個端口號,否則兩個函數都會拋出一個IOException對象。

      如果創建了 一個Socket對象,那么它可能通過調用Socket的 getInputStream()方法從服務程序獲得輸入流讀傳送來的信息,也可能通過調用Socket的 getOutputStream()方法獲得輸出流來發送消息。在讀寫活動完成之后,客戶程序調用close()方法關閉流和流套接字,下面的代碼創建了 一個服務程序主機地址為198.163.227.6,端口號為13的Socket對象,然后從這個新創建的Socket對象中讀取輸入流,然后再關閉流和 Socket對象。
    Socket s = new Socket ("198.163.227.6", 13);
    InputStream is = s.getInputStream ();
    // Read from the stream.
    is.close ();
    s.close ();

    接下面我們將示范一個流套接字的客戶程序,這個程序將創建一個Socket對象,Socket將訪問運行在指定主機端口10000上的服務程序,如果訪 問成功客戶程序將給服務程序發送一系列命令并打印服務程序的響應。List2使我們創建的程序SSClient的源代碼:

      Listing 2: SSClient.java
    // SSClient.java

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

    class SSClient
    {
     public static void main (String [] args)
     {
      String host = "localhost";

      // If user specifies a command-line argument, that argument
      // represents the host name.

      if (args.length == 1)
       host = args [0];

      BufferedReader br = null;
      PrintWriter pw = null;
      Socket s = null;

      try
      {
       // Create a socket that attempts to connect to the server
       // program on the host at port 10000.

       s = new Socket (host, 10000);

       // Create an input stream reader that chains to the socket's
       // byte-oriented input stream. The input stream reader
       // converts bytes read from the socket to characters. The
       // conversion is based on the platform's default character
       // set.

       InputStreamReader isr;
       isr = new InputStreamReader (s.getInputStream ());

       // Create a buffered reader that chains to the input stream
       // reader. The buffered reader supplies a convenient method
       // for reading entire lines of text.

       br = new BufferedReader (isr);

       // Create a print writer that chains to the socket's byte-
       // oriented output stream. The print writer creates an
       // intermediate output stream writer that converts
       // characters sent to the socket to bytes. The conversion
       // is based on the platform's default character set.

       pw = new PrintWriter (s.getOutputStream (), true);

       // Send the DATE command to the server.

       pw.println ("DATE");

       // Obtain and print the current date/time.

       System.out.println (br.readLine ());
       // Send the PAUSE command to the server. This allows several
       // clients to start and verifies that the server is spawning
       // multiple threads.

       pw.println ("PAUSE");
       // Send the DOW command to the server.

       pw.println ("DOW");

       // Obtain and print the current day of week.

       System.out.println (br.readLine ());

       // Send the DOM command to the server.
     
       pw.println ("DOM");

       // Obtain and print the current day of month.

       System.out.println (br.readLine ());

       // Send the DOY command to the server.

       pw.println ("DOY");

       // Obtain and print the current day of year.

       System.out.println (br.readLine ());
      }
      catch (IOException e)
      {
       System.out.println (e.toString ());
      }
      finally
      {
       try
       {
        if (br != null)
         br.close ();

        if (pw != null)
         pw.close ();

        if (s != null)
         s.close ();
       }
       catch (IOException e)
       {
        }
      }
     }
    }
      運行這段程序將會得到下面的結果:
    Tue Jan 29 18:11:51 CST 2002
    TUESDAY
    29
    29

    SSClient 創建了一個Socket對象與運行在主機端口10000的服務程序聯系,主機的IP地址由host變量確定。SSClient將獲得Socket的輸入輸 出流,圍繞BufferedReader的輸入流和PrintWriter的輸出流對字符串進行讀寫操作就變得非常容易,SSClient個服務程序發出 各種date/time命令并得到響應,每個響應均被打印,一旦最后一個響應被打印,將執行Try/Catch/Finally結構的Finally子 串,Finally子串將在關閉Socket之前關閉BufferedReader 和 PrintWriter。

      在SSClient源代碼編譯完成后,可以輸入java SSClient 來執行這段程序,如果有合適的程序運行在不同的主機上,采用主機名/IP地址為參數的輸入方式,比如www.sina.com.cn是運行服務器程序的主機,那么輸入方式就是java SSClient www.sina.com.cn

      技巧

    Socket類包含了許多有用的方法。比如getLocalAddress()將返回一個包含客戶程序IP地址的InetAddress子類對象的引 用;getLocalPort()將返回客戶程序的端口號;getInetAddress()將返回一個包含服務器IP地址的InetAddress子類 對象的引用;getPort()將返回服務程序的端口號。

     

    ServerSocket類

      由于 SSClient使用了流套接字,所以服務程序也要使用流套接字。這就要創建一個ServerSocket對象,ServerSocket有幾個構造函 數,最簡單的是ServerSocket(int port),當使用ServerSocket(int port)創建一個ServerSocket對象,port參數傳遞端口號,這個端口就是服務器監聽連接請求的端口,如果在這時出現錯誤將拋出 IOException異常對象,否則將創建ServerSocket對象并開始準備接收連接請求。

      接下來服務程序進入無限循環之中, 無限循環從調用ServerSocket的accept()方法開始,在調用開始后accept()方法將導致調用線程阻塞直到連接建立。在建立連接后 accept()返回一個最近創建的Socket對象,該Socket對象綁定了客戶程序的IP地址或端口號。

      由于存在單個服務程序與 多個客戶程序通訊的可能,所以服務程序響應客戶程序不應該花很多時間,否則客戶程序在得到服務前有可能花很多時間來等待通訊的建立,然而服務程序和客戶程 序的會話有可能是很長的(這與電話類似),因此為加快對客戶程序連接請求的響應,典型的方法是服務器主機運行一個后臺線程,這個后臺線程處理服務程序和客 戶程序的通訊。

      為了示范我們在上面談到的慨念并完成SSClient程序,下面我們創建一個SSServer程序,程序將創建一個 ServerSocket對象來監聽端口10000的連接請求,如果成功服務程序將等待連接輸入,開始一個線程處理連接,并響應來自客戶程序的命令。下面 就是這段程序的代碼:

      Listing 3: SSServer.java
    // SSServer.java

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

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

      // Create a server socket that listens for incoming connection
      // requests on port 10000.

      ServerSocket server = new ServerSocket (10000);

      while (true)
      {
       // Listen for incoming connection requests from client
       // programs, establish a connection, and return a Socket
       // object that represents this connection.

       Socket s = server.accept ();

       System.out.println ("Accepting Connection...\n");

       // Start a thread to handle the connection.

       new ServerThread (s).start ();
      }
     }
    }

    class ServerThread extends Thread
    {
     private Socket s;

     ServerThread (Socket s)
     {
      this.s = s;
     }

     public void run ()
     {
      BufferedReader br = null;
      PrintWriter pw = null;

      try
      {
       // Create an input stream reader that chains to the socket's
       // byte-oriented input stream. The input stream reader
       // converts bytes read from the socket to characters. The
       // conversion is based on the platform's default character
       // set.

       InputStreamReader isr;
       isr = new InputStreamReader (s.getInputStream ());

       // Create a buffered reader that chains to the input stream
       // reader. The buffered reader supplies a convenient method
       // for reading entire lines of text.

       br = new BufferedReader (isr);

       // Create a print writer that chains to the socket's byte-
       // oriented output stream. The print writer creates an
       // intermediate output stream writer that converts
       // characters sent to the socket to bytes. The conversion
       // is based on the platform's default character set.

       pw = new PrintWriter (s.getOutputStream (), true);

       // Create a calendar that makes it possible to obtain date
       // and time information.

       Calendar c = Calendar.getInstance ();

       // Because the client program may send multiple commands, a
       // loop is required. Keep looping until the client either
       // explicitly requests termination by sending a command
       // beginning with letters BYE or implicitly requests
       // termination by closing its output stream.

       do
       {
        // Obtain the client program's next command.

        String cmd = br.readLine ();

        // Exit if client program has closed its output stream.

        if (cmd == null)
         break;
      
        // Convert command to uppercase, for ease of comparison.

        cmd = cmd.toUpperCase ();

        // If client program sends BYE command, terminate.

        if (cmd.startsWith ("BYE"))
         break;

        // If client program sends DATE or TIME command, return
        // current date/time to the client program.

        if (cmd.startsWith ("DATE") || cmd.startsWith ("TIME"))
         pw.println (c.getTime ().toString ());

        // If client program sends DOM (Day Of Month) command,
        // return current day of month to the client program.

        if (cmd.startsWith ("DOM"))
         pw.println ("" + c.get (Calendar.DAY_OF_MONTH));

        // If client program sends DOW (Day Of Week) command,
        // return current weekday (as a string) to the client
        // program.

        if (cmd.startsWith ("DOW"))
         switch (c.get (Calendar.DAY_OF_WEEK))
        {
         case Calendar.SUNDAY : pw.println ("SUNDAY");
          break;

         case Calendar.MONDAY : pw.println ("MONDAY");
          break;

         case Calendar.TUESDAY : pw.println ("TUESDAY");
          break;

         case Calendar.WEDNESDAY: pw.println ("WEDNESDAY");
          break;

         case Calendar.THURSDAY : pw.println ("THURSDAY");
          break;

         case Calendar.FRIDAY : pw.println ("FRIDAY");
          break;

         case Calendar.SATURDAY : pw.println ("SATURDAY");
        }

        // If client program sends DOY (Day of Year) command,
        // return current day of year to the client program.

        if (cmd.startsWith ("DOY"))
         pw.println ("" + c.get (Calendar.DAY_OF_YEAR));

         // If client program sends PAUSE command, sleep for three
         // seconds.
     
        if (cmd.startsWith ("PAUSE"))
        try
        {
         Thread.sleep (3000);
        }
        catch (InterruptedException e)
        {
        }
       }
       while (true);
       {
       catch (IOException e)
       {
        System.out.println (e.toString ());
       }
       finally
       {
        System.out.println ("Closing Connection...\n");

        try
        {
         if (br != null)
          br.close ();

          if (pw != null)
           pw.close ();

          if (s != null)
           s.close ();
        }
        catch (IOException e)
        {
        }
       }
      }
    }
    運行這段程序將得到下面的輸出:

     


    Server starting...
    Accepting Connection...
    Closing Connection...
    SSServer 的源代碼聲明了一對類:SSServer 和ServerThread;SSServer的main()方法創建了一個ServerSocket對象來監聽端口10000上的連接請求,如果成功, SSServer進入一個無限循環中,交替調用ServerSocket的 accept() 方法來等待連接請求,同時啟動后臺線程處理連接(accept()返回的請求)。線程由ServerThread繼承的start()方法開始,并執行 ServerThread的run()方法中的代碼。

      一旦run()方法運行,線程將創建BufferedReader, PrintWriter和 Calendar對象并進入一個循環,這個循環由讀(通過BufferedReader的 readLine())來自客戶程序的一行文本開始,文本(命令)存儲在cmd引用的string對象中,如果客戶程序過早的關閉輸出流,會發生什么呢? 答案是:cmd將得不到賦值。

      注意必須考慮到這種情況:在服務程序正在讀輸入流時,客戶程序關閉了輸出流,如果沒有對這種情況進行處理,那么程序將產生異常。

      一旦編譯了SSServer的源代碼,通過輸入Java SSServer來運行程序,在開始運行SSServer后,就可以運行一個或多個SSClient程序。

    自尋址套接字(Datagram Sockets)

    ,因為使用流套接字的每個連接均要花費一定的時間,要減少這種開銷,網絡API提供了第二種套接字:自尋址套接字(datagram socket),自尋址使用UDP發送尋址信息(從客戶程序到服務程序或從服務程序到客戶程序),不同的是可以通過自尋址套接字發送多IP信息包,自尋址 信息包含在自尋址包中,此外自尋址包又包含在IP包內,這就將尋址信息長度限制在60000字節內。圖2顯示了位于IP包內的自尋址包的自尋址信息。
    與TCP保證信息到達信息目的地的方式不同,UDP提供了另外一種方法,如果自尋址信息包沒有到達目的地,,那么UDP也不會請求發送者重新發送自尋址 包,這是因為UDP在每一個自尋址包中包含了錯誤檢測信息,在每個自尋址包到達目的地之后UDP只進行簡單的錯誤檢查,如果檢測失敗,UDP將拋棄這個自 尋址包,也不會從發送者那里重新請求替代者,這與通過郵局發送信件相似,發信人在發信之前不需要與收信人建立連接,同樣也不能保證信件能到達收信人那里

    自尋址套接字工作包括下面三個類:DatagramPacket, DatagramSocket,和 MulticastSocket。DatagramPacket對象描繪了自尋址包的地址信息,DatagramSocket表示客戶程序和服務程序自尋 址套接字,MulticastSocket描繪了能進行多點傳送的自尋址套接字,這三個類均位于java.net包內。

      DatagramPacket類

      在使用自尋址包之前,你需要首先熟悉DatagramPacket類,地址信息和自尋址包以字節數組的方式同時壓縮入這個類創建的對象中

    DatagramPacket有數個構造函數,即使這些構造函數的形式不同,但通常情況下他們都有兩個共同的參數:byte [] buffer 和 int length,buffer參數包含了一個對保存自尋址數據包信息的字節數組的引用,length表示字節數組的長度。

      最簡單 的構造函數是DatagramPacket(byte [] buffer, int length),這個構造函數確定了自尋址數據包數組和數組的長度,但沒有任何自尋址數據包的地址和端口信息,這些信息可以后面通過調用方法 setAddress(InetAddress addr)和setPort(int port)添加上,下面的代碼示范了這些函數和方法。
    byte [] buffer = new byte [100];
    DatagramPacket dgp = new DatagramPacket (buffer, buffer.length);
    InetAddress ia = InetAddress.getByName ("dgp.setAddress (ia);
    dgp.setPort (6000); // Send datagram packet to port 6000.

     

      如果你更喜歡在調用構造函數的時候同時包括地址和端口號,可以使用DatagramPacket(byte [] buffer, int length, InetAddress addr, int port)函數,下面的代碼示范了另外一種選擇。

     


    byte [] buffer = new byte [100];
    InetAddress ia = InetAddress.getByName ("
    DatagramPacket dgp = new DatagramPacket (buffer, buffer.length, ia,
    6000);

     


    有時候在創建了DatagramPacket對象后想改變字節數組和他的長度,這時可以通過調用setData(byte [] buffer) 和 setLength(int length)方法來實現。在任何時候都可以通過調用getData() 來得到字節數組的引用,通過調用getLength()來獲得字節數組的長度。下面的代碼示范了這些方法:

     


    byte [] buffer2 = new byte [256];
    dgp.setData (buffer2);
    dgp.setLength (buffer2.length);

     


      關于DatagramPacket的更多信息請參考SDK文檔。
    DatagramSocket類

    DatagramSocket類在客戶端創建自尋址套接字與服務器端進行通信連接,并發送和接受自尋址套接字。雖然有多個構造函數可供選擇,但我發現創 建客戶端自尋址套接字最便利的選擇是DatagramSocket()函數,而服務器端則是DatagramSocket(int port)函數,如果未能創建自尋址套接字或綁定自尋址套接字到本地端口,那么這兩個函數都將拋出一個SocketException對象,一旦程序創建 了DatagramSocket對象,那么程序分別調用send(DatagramPacket dgp)和 receive(DatagramPacket dgp)來發送和接收自尋址數據包,

      List4顯示的DGSClient源代碼示范了如何創建自尋址套接字以及如何通過套接字處理發送和接收信息

     


    Listing 4: DGSClient.java
    // DGSClient.java

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

    class DGSClient
    {
     public static void main (String [] args)
     {
      String host = "localhost";

      // If user specifies a command-line argument, that argument
      // represents the host name.
     
      if (args.length == 1)
       host = args [0];

      DatagramSocket s = null;

      try
      {
       // Create a datagram socket bound to an arbitrary port.

       s = new DatagramSocket ();

       // Create a byte array that will hold the data portion of a
       // datagram packet's message. That message originates as a
       // String object, which gets converted to a sequence of
       // bytes when String's getBytes() method is called. The
       // conversion uses the platform's default character set.

       byte [] buffer;
       buffer = new String ("Send me a datagram").getBytes ();

       // Convert the name of the host to an InetAddress object.
       // That object contains the IP address of the host and is
       // used by DatagramPacket.

       InetAddress ia = InetAddress.getByName (host);

       // Create a DatagramPacket object that encapsulates a
       // reference to the byte array and destination address
       // information. The destination address consists of the
       // host's IP address (as stored in the InetAddress object)
       // and port number 10000 -- the port on which the server
       // program listens.

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

       // Send the datagram packet over the socket.

       s.send (dgp);

       // Create a byte array to hold the response from the server.
       // program.

       byte [] buffer2 = new byte [100];

       // Create a DatagramPacket object that specifies a buffer
       // to hold the server program's response, the IP address of
       // the server program's computer, and port number 10000.

       dgp = new DatagramPacket (buffer2,
          buffer.length,
          ia,
          10000);

       // Receive a datagram packet over the socket.

       s.receive (dgp);

       // Print the data returned from the server program and stored
       // in the datagram packet.

       System.out.println (new String (dgp.getData ()));

      }
      catch (IOException e)
      {
       System.out.println (e.toString ());
      }
      finally
      {
       if (s != null)
        s.close (); 
      }
     }
    }


    DGSClient 由創建一個綁定任意本地(客戶端)端口好的DatagramSocket對象開始,然后裝入帶有文本信息的數組buffer和描述服務器主機IP地址的 InetAddress子類對象的引用,接下來,DGSClient創建了一個DatagramPacket對象,該對象加入了帶文本信息的緩沖器的引 用,InetAddress子類對象的引用,以及服務端口號10000, DatagramPacket的自尋址數據包通過方法sent()發送給服務器程序,于是一個包含服務程序響應的新的DatagramPacket對象被 創建,receive()得到響應的自尋址數據包,然后自尋址數據包的getData()方法返回該自尋址數據包的一個引用,最后關閉 DatagramSocket。

      DGSServer服務程序補充了DGSClient的不足,List5是DGSServer的源代碼:

     

    Listing 5: DGSServer.java
    // DGSServer.java

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

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

      // Create a datagram socket bound to port 10000. Datagram
      // packets sent from client programs arrive at this port.

      DatagramSocket s = new DatagramSocket (10000);

      // Create a byte array to hold data contents of datagram
      // packet.

      byte [] data = new byte [100];

      // Create a DatagramPacket object that encapsulates a reference
      // to the byte array and destination address information. The
      // DatagramPacket object is not initialized to an address
      // because it obtains that address from the client program.

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

      // Enter an infinite loop. Press Ctrl+C to terminate program.

      while (true)
      {
       // Receive a datagram packet from the client program.

       s.receive (dgp);

       // Display contents of datagram packet.

       System.out.println (new String (data));

       // Echo datagram packet back to client program.

      s.send (dgp);
     }
    }
    }

     


      DGSServer創建了一個綁定端口10000的自尋址套接字,然后創建一個字節數組容納自尋址信息,并創建自尋址包,下一步,DGSServer進入一個無限循環中以接收自尋址數據包、顯示內容并將響應返回客戶端,自尋址套接沒有關閉,因為循環是無限的。

    在編譯DGSServer 和DGSClient的源代碼后,由輸入java DGSServer開始運行DGSServer,然后在同一主機上輸入Java DGSClient開始運行DGSClient,如果DGSServer與DGSClient運行于不同主機,在輸入時注意要在命令行加上服務程序的主機 名或IP地址,如:java DGSClient www.yesky.com


    多點傳送和MulticastSocket類

    前面的例子顯示了服務器程序線程發送單一的消息(通過流套接字或自尋址套接字)給唯一的客戶端程序,這種行為被稱為單點傳送(unicasting), 多數情況都不適合于單點傳送,比如,搖滾歌手舉辦一場音樂會將通過互聯網進行播放,畫面和聲音的質量依賴于傳輸速度,服務器程序要傳送大約10億字節的數 據給客戶端程序,使用單點傳送,那么每個客戶程序都要要復制一份數據,如果,互聯網上有10000個客戶端要收看這個音樂會,那么服務器程序通過 Internet要傳送10000G的數據,這必然導致網絡阻塞,降低網絡的傳輸速度。

      如果服務器程序要將同一信息發送給多個客戶端, 那么服務器程序和客戶程序可以利用多點傳送(multicasting)方式進行通信。多點傳送就是服務程序對專用的多點傳送組的IP地址和端口發送一系 列自尋址數據包,通過加入操作IP地址被多點傳送Socket注冊,通過這個點客戶程序可以接收發送給組的自尋址包(同樣客戶程序也可以給這個組發送自尋 址包),一旦客戶程序讀完所有要讀的自尋址數據包,那么可以通過離開組操作離開多點傳送組。

      注意: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.

      

    write("");
      // 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類。在完成本文后就可以編寫基本的底層通訊程序。

    posted on 2007-01-18 23:56 苦笑枯 閱讀(480) 評論(0)  編輯  收藏 所屬分類: Java
    收藏來自互聯網,僅供學習。若有侵權,請與我聯系!

    <2007年1月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(2)

    隨筆分類(56)

    隨筆檔案(56)

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲一区二区三区亚瑟| 亚洲色大成WWW亚洲女子| 69天堂人成无码麻豆免费视频| 亚洲狠狠成人综合网| 亚洲AⅤ永久无码精品AA| 国产无遮挡裸体免费视频| 精品一区二区三区免费观看| 久久亚洲精品成人AV| 免费观看亚洲人成网站| 一区二区三区观看免费中文视频在线播放 | 亚洲狠狠ady亚洲精品大秀| 韩国日本好看电影免费看| 三上悠亚电影全集免费| 色婷五月综激情亚洲综合| 久久久久久久亚洲精品| 免费大片黄在线观看yw| 中文字幕久无码免费久久| 亚洲国语在线视频手机在线| 亚洲av无码乱码在线观看野外| 最近中文字幕完整版免费高清| 精品在线视频免费| 日产亚洲一区二区三区| 亚洲成?Ⅴ人在线观看无码| 69堂人成无码免费视频果冻传媒| 国产精品亚洲天堂| 亚洲综合精品香蕉久久网97| 国产免费看插插插视频| 国产精品1024永久免费视频 | 黄色免费在线观看网址| 亚洲电影日韩精品| 91免费国产自产地址入| 亚洲小说图区综合在线| 久久久久亚洲Av片无码v| 日产国产精品亚洲系列| 国产人在线成免费视频| 久久午夜夜伦鲁鲁片免费无码| 免费无码专区毛片高潮喷水| 亚洲精品国偷自产在线| 亚洲国产精品一区二区九九| 日本人护士免费xxxx视频| 久久久高清免费视频 |