程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java套接字編程(下)(2)

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

編輯:關於JAVA

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved