<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      Java套接字編程(下)(2)

      [摘要]DatagramSocket類  DatagramSocket類在客戶端創建自尋址套接字與服務器端進行通信連接,并發送和接受自尋址套接字。雖然有多個構造函數可供選擇,但我發現創建客戶端自尋址套接字最...
      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



      日韩精品一区二区三区高清