六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

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

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



主站蜘蛛池模板: 一区二区三区网站在线免费线观看 | 三级主播 | 日本高清高色 | 日本免费一区二区三区视频 | 青青草原在线免费观看 | 欧美一级视频高清片 | 欧美怡红院免费的全部视频 | 欧美在线资源 | 中文字幕色网站 | 中文天堂在线最新2022更新 | 亚洲一区二区在线 | 日韩日日日 | 欧洲性开放大片免费观看视频 | 青青色综合 | 日韩手机在线 | 亚洲a影院| 网友自拍第一页 | 热综合一本伊人久久精品 | 青青青青手机在线视频观看国产 | 日韩视频免费观看 | 亚洲www.| 香港三日本三级人妇三级99 | 日韩一级欧美一级一级国产 | 特级黄 | 中文字幕波多野不卡一区 | 四虎精品免费永久在线 | 色悠久久久久综合欧美99 | 在线亚洲精品国产波多野结衣 | 伊人婷婷色香五月综合缴激情 | 亚洲第一综合色 | 天天爽夜夜爽天天做夜夜做 | 午夜久久福利 | 亚洲 国产 图片 | 亚洲专区欧美专区 | 日韩欧美一区黑人vs日本人 | 亚洲一欧洲中文字幕在线 | 特黄色一级片 | 视频一区二区三区在线观看 | 天天色综合4 | 天堂v| 天天爱天天做天天爽夜夜揉 |