,Java   工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛,Java
您的位置:软件 > 开发者网络 > 开发工具 > Java > 正文
Java套接字编程(下)
[文章信息]
作者:crystal编译
时间:2003-01-11
出处:yesky
责任编辑:方舟
[文章导读]
有些时候使用流套接字是不可接受的
advertisement
热点推荐
· 深入探讨SQL Server 2000对XML的支持
· Swing(空)
· 3DS Stodio Max 宝典
· 中小企业服务器配置方案--前 言
· 中小企业服务器配置方案--代理接入服务器(3)
[正文]

上一页  1 2 3  下一页

  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 3  下一页

发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
天极社区邀请您:写博客日记  上传相片   论坛聊天  订阅电子杂志  推荐网摘   免费图铃工具
笔名:   请您注意:

 遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。

 天极网拥有管理笔名和留言的一切权利。
评论:
 
,Java相关内容,Java焦点新闻
  • FVD刺激高清碟机加速商业化 抢占商机最重要
  • 3家搜索引擎集体诉讼8848 吕春维未敢出席
  • 杨元庆:没有准备不会获批的备用方案
  • 军队信息化诞生新领域 电子军务呼之欲出
  • 世界经济论坛公布信息化程度全球最新排名
  • 2004政务绩效评估:政府门户尚处于发展阶段
  • 甲骨文出资5.15亿美元 意图收购RetekInc
  • 技术并购:帮你突破传统增长的“天花板”
  • ,JavaAdvertisement